difftreelog
fix(fmt) differentiate between nodes of the same type
in: master
4 files changed
crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__asserts.snapdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__asserts.snap
@@ -0,0 +1,9 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: "reformat(indoc!(\"\n\t\t\t{\n\t\t\t\tassert 1 > 0 : 'one should be greater than zero',\n\t\t\t\tassert true,\n\t\t\t\tvalue: 42,\n\t\t\t}\n\t\t\"))"
+---
+{
+ assert 1 > 0: 'one should be greater than zero',
+ assert true,
+ value: 42,
+}
crates/jrsonnet-formatter/src/tests.rsdiffbeforeafterboth1use dprint_core::formatting::{PrintItems, PrintOptions};2use indoc::indoc;34use crate::Printable;56fn reformat(input: &str) -> String {7 let (source, _) = jrsonnet_rowan_parser::parse(input);89 dprint_core::formatting::format(10 || {11 let mut out = PrintItems::new();12 source.print(&mut out);13 out14 },15 PrintOptions {16 indent_width: 2,17 max_width: 100,18 use_tabs: true,19 new_line_text: "\n",20 },21 )22}2324#[test]25fn complex_comments() {26 insta::assert_snapshot!(reformat(indoc!(27 "{28 comments: {29 _: '',30 // Plain comment31 a: '',3233 # Plain comment with empty line before34 b: '',35 /*Single-line multiline comment3637 */38 c: '',3940 /**Single-line multiline doc comment4142 */43 c: '',4445 /**Multiline doc46 Comment47 */48 c: '',4950 /*5152 Multi-line5354 comment55 */56 d: '',5758 e: '', // Inline comment5960 k: '',6162 // Text after everything63 },64 comments2: {65 k: '',66 // Text after everything, but no newline above67 },68 spacing: {69 a: '',7071 b: '',72 },73 noSpacing: {74 a: '',75 b: '',76 },77 }"78 )));79}8081#[test]82fn args() {83 insta::assert_snapshot!(reformat(indoc!(84 "85 {86 short: aaa(1,2,3,4,5),87 long: bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),88 short_in_long: bbb(aaa(1,2,3,4,5), 123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),89 long_in_short: aaa(1,2,3,4,5,bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123)),90 }91 "92 )));93}1use dprint_core::formatting::{PrintItems, PrintOptions};2use indoc::indoc;34use crate::Printable;56fn reformat(input: &str) -> String {7 let (source, _) = jrsonnet_rowan_parser::parse(input);89 dprint_core::formatting::format(10 || {11 let mut out = PrintItems::new();12 source.print(&mut out);13 out14 },15 PrintOptions {16 indent_width: 2,17 max_width: 100,18 use_tabs: true,19 new_line_text: "\n",20 },21 )22}2324#[test]25fn complex_comments() {26 insta::assert_snapshot!(reformat(indoc!(27 "{28 comments: {29 _: '',30 // Plain comment31 a: '',3233 # Plain comment with empty line before34 b: '',35 /*Single-line multiline comment3637 */38 c: '',3940 /**Single-line multiline doc comment4142 */43 c: '',4445 /**Multiline doc46 Comment47 */48 c: '',4950 /*5152 Multi-line5354 comment55 */56 d: '',5758 e: '', // Inline comment5960 k: '',6162 // Text after everything63 },64 comments2: {65 k: '',66 // Text after everything, but no newline above67 },68 spacing: {69 a: '',7071 b: '',72 },73 noSpacing: {74 a: '',75 b: '',76 },77 }"78 )));79}8081#[test]82fn args() {83 insta::assert_snapshot!(reformat(indoc!(84 "85 {86 short: aaa(1,2,3,4,5),87 long: bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),88 short_in_long: bbb(aaa(1,2,3,4,5), 123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),89 long_in_short: aaa(1,2,3,4,5,bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123)),90 }91 "92 )));93}9495#[test]96fn asserts() {97 insta::assert_snapshot!(reformat(indoc!(98 "99 {100 assert 1 > 0 : 'one should be greater than zero',101 assert true,102 value: 42,103 }104 "105 )));106}crates/jrsonnet-rowan-parser/src/generated/nodes.rsdiffbeforeafterboth--- a/crates/jrsonnet-rowan-parser/src/generated/nodes.rs
+++ b/crates/jrsonnet-rowan-parser/src/generated/nodes.rs
@@ -14,7 +14,7 @@
}
impl SourceFile {
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -27,7 +27,7 @@
support::children(&self.syntax)
}
pub fn expr_base(&self) -> Option<ExprBase> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn suffixs(&self) -> AstChildren<Suffix> {
support::children(&self.syntax)
@@ -46,7 +46,7 @@
support::token(&self.syntax, T![.])
}
pub fn index(&self) -> Option<Name> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -75,7 +75,7 @@
support::token(&self.syntax, T!['['])
}
pub fn index(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn r_brack_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![']'])
@@ -88,7 +88,7 @@
}
impl SuffixSlice {
pub fn slice_desc(&self) -> Option<SliceDesc> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -101,16 +101,16 @@
support::token(&self.syntax, T!['['])
}
pub fn from(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn colon_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![:])
}
pub fn end(&self) -> Option<SliceDescEnd> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn step(&self) -> Option<SliceDescStep> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn r_brack_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![']'])
@@ -123,7 +123,7 @@
}
impl SuffixApply {
pub fn args_desc(&self) -> Option<ArgsDesc> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn tailstrict_kw_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![tailstrict])
@@ -168,7 +168,7 @@
}
impl StmtAssert {
pub fn assertion(&self) -> Option<Assertion> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn semi_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![;])
@@ -184,13 +184,13 @@
support::token(&self.syntax, T![assert])
}
pub fn condition(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn colon_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![:])
}
pub fn message(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).nth(1usize)
}
}
@@ -200,13 +200,13 @@
}
impl ExprBinary {
pub fn lhs(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn binary_operator(&self) -> Option<BinaryOperator> {
support::token_child(&self.syntax)
}
pub fn rhs(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).nth(1usize)
}
}
@@ -219,7 +219,7 @@
support::token_child(&self.syntax)
}
pub fn rhs(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -229,7 +229,7 @@
}
impl ExprObjExtend {
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -242,7 +242,7 @@
support::token(&self.syntax, T!['('])
}
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn r_paren_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![')'])
@@ -301,7 +301,7 @@
}
impl ExprObject {
pub fn obj_body(&self) -> Option<ObjBody> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -314,7 +314,7 @@
support::token(&self.syntax, T!['['])
}
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn comma_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![,])
@@ -346,7 +346,7 @@
}
impl ExprVar {
pub fn name(&self) -> Option<Name> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -359,19 +359,19 @@
support::token(&self.syntax, T![if])
}
pub fn cond(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn then_kw_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![then])
}
pub fn then(&self) -> Option<TrueExpr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn else_kw_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![else])
}
pub fn else_(&self) -> Option<FalseExpr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -381,7 +381,7 @@
}
impl TrueExpr {
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -391,7 +391,7 @@
}
impl FalseExpr {
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -407,13 +407,13 @@
support::token(&self.syntax, T!['('])
}
pub fn params_desc(&self) -> Option<ParamsDesc> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn r_paren_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![')'])
}
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -442,7 +442,7 @@
support::token(&self.syntax, T![error])
}
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -452,7 +452,7 @@
}
impl SliceDescEnd {
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -462,7 +462,7 @@
}
impl SliceDescStep {
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -472,13 +472,13 @@
}
impl Arg {
pub fn name(&self) -> Option<Name> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn assign_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![=])
}
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -523,7 +523,7 @@
}
impl MemberBindStmt {
pub fn obj_local(&self) -> Option<ObjLocal> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -536,7 +536,7 @@
support::token(&self.syntax, T![local])
}
pub fn bind(&self) -> Option<Bind> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -546,7 +546,7 @@
}
impl MemberAssertStmt {
pub fn assertion(&self) -> Option<Assertion> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -556,7 +556,7 @@
}
impl MemberFieldNormal {
pub fn field_name(&self) -> Option<FieldName> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn plus_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![+])
@@ -565,7 +565,7 @@
support::token_child(&self.syntax)
}
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -575,16 +575,16 @@
}
impl MemberFieldMethod {
pub fn field_name(&self) -> Option<FieldName> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn params_desc(&self) -> Option<ParamsDesc> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn visibility(&self) -> Option<Visibility> {
support::token_child(&self.syntax)
}
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -594,7 +594,7 @@
}
impl FieldNameFixed {
pub fn id(&self) -> Option<Name> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn text(&self) -> Option<Text> {
support::token_child(&self.syntax)
@@ -610,7 +610,7 @@
support::token(&self.syntax, T!['['])
}
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn r_brack_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![']'])
@@ -626,13 +626,13 @@
support::token(&self.syntax, T![for])
}
pub fn bind(&self) -> Option<Destruct> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn in_kw_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![in])
}
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -645,7 +645,7 @@
support::token(&self.syntax, T![if])
}
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -655,13 +655,13 @@
}
impl BindDestruct {
pub fn into(&self) -> Option<Destruct> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn assign_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![=])
}
pub fn value(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -671,16 +671,16 @@
}
impl BindFunction {
pub fn name(&self) -> Option<Name> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn params(&self) -> Option<ParamsDesc> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn assign_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![=])
}
pub fn value(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -690,13 +690,13 @@
}
impl Param {
pub fn destruct(&self) -> Option<Destruct> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn assign_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![=])
}
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -706,7 +706,7 @@
}
impl DestructFull {
pub fn name(&self) -> Option<Name> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -748,7 +748,7 @@
support::children(&self.syntax)
}
pub fn destruct_rest(&self) -> Option<DestructRest> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn comma_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![,])
@@ -764,19 +764,19 @@
}
impl DestructObjectField {
pub fn field(&self) -> Option<Name> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn colon_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![:])
}
pub fn destruct(&self) -> Option<Destruct> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
pub fn assign_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![=])
}
pub fn expr(&self) -> Option<Expr> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -789,7 +789,7 @@
support::token(&self.syntax, T![...])
}
pub fn into(&self) -> Option<Name> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
@@ -799,7 +799,7 @@
}
impl DestructArrayElement {
pub fn destruct(&self) -> Option<Destruct> {
- support::child(&self.syntax)
+ support::children(&self.syntax).next()
}
}
xtask/src/sourcegen/mod.rsdiffbeforeafterboth--- a/xtask/src/sourcegen/mod.rs
+++ b/xtask/src/sourcegen/mod.rs
@@ -1,4 +1,4 @@
-use std::path::PathBuf;
+use std::{collections::HashMap, path::PathBuf};
use anyhow::Result;
use ast::{lower, AstSrc};
@@ -202,7 +202,15 @@
quote!(impl ast::#trait_name for #name {})
});
- let methods = node.fields.iter().map(|field| {
+ let mut type_positions: HashMap<String, usize> = HashMap::new();
+ let field_positions: Vec<_> = node.fields.iter().map(|field| {
+ let ty_str = field.ty().to_string();
+ let pos = *type_positions.get(&ty_str).unwrap_or(&0);
+ type_positions.insert(ty_str, pos + 1);
+ pos
+ }).collect();
+
+ let methods = node.fields.iter().zip(field_positions.iter()).map(|(field, &pos)| {
let method_name = field.method_name(kinds);
let ty = field.ty();
@@ -224,10 +232,16 @@
support::token_child(&self.syntax)
}
}
+ } else if pos == 0 {
+ quote! {
+ pub fn #method_name(&self) -> Option<#ty> {
+ support::children(&self.syntax).next()
+ }
+ }
} else {
quote! {
pub fn #method_name(&self) -> Option<#ty> {
- support::child(&self.syntax)
+ support::children(&self.syntax).nth(#pos)
}
}
}