--- a/crates/jrsonnet-formatter/src/lib.rs +++ b/crates/jrsonnet-formatter/src/lib.rs @@ -12,9 +12,9 @@ collect_lexed_str_block, nodes::{ Arg, ArgsDesc, Assertion, BinaryOperator, Bind, CompSpec, Destruct, DestructArrayPart, - DestructRest, Expr, ExprBase, FieldName, ForSpec, IfSpec, ImportKind, Literal, Member, - Name, Number, ObjBody, ObjLocal, ParamsDesc, SliceDesc, SourceFile, Stmt, Suffix, Text, - TextKind, UnaryOperator, Visibility, + DestructRest, Expr, ExprArray, ExprBase, FieldName, ForSpec, IfSpec, ImportKind, Literal, + Member, Name, Number, ObjBody, ObjLocal, ParamsDesc, SliceDesc, SourceFile, Stmt, Suffix, + Text, TextKind, UnaryOperator, Visibility, }, AstNode, AstToken as _, SyntaxToken, }; @@ -715,6 +715,61 @@ } } } + +impl Printable for ExprArray { + fn print(&self, out: &mut PrintItems) { + let (children, end_comments) = children_between::( + self.syntax().clone(), + self.l_brack_token().map(Into::into).as_ref(), + self.r_brack_token().map(Into::into).as_ref(), + None, + ); + if children.is_empty() && end_comments.is_empty() { + p!(out, str("[ ]")); + return; + } + + let source_is_multiline = + children.iter().any(|c| c.triggers_multiline) || end_comments.should_start_with_newline; + + let start = LineNumber::new("arr start line"); + let end = LineNumber::new("arr end line"); + let multi_line: ConditionResolver = if source_is_multiline { + true_resolver() + } else { + Rc::new(move |ctx: &mut ConditionResolverContext| is_multiple_lines(ctx, start, end)) + }; + + fn gen_elements(children: Vec>, multi_line: ConditionResolver) -> PrintItems { + let mut _out = PrintItems::new(); + let out = &mut _out; + let mut els = children.into_iter().peekable(); + while let Some(el) = els.next() { + if el.should_start_with_newline { + p!(out, nl); + } + format_comments(&el.before_trivia, CommentLocation::AboveItem, out); + p!(out, { el.value }); + let has_more = els.peek().is_some(); + if has_more { + p!(out, str(",")); + } else { + p!(out, if("trailing comma", multi_line, str(","))); + } + format_comments(&el.inline_trivia, CommentLocation::ItemInline, out); + p!(out, if_else("element separator", multi_line, nl)(sonl)) + } + _out + } + + let els_items = new_line_group(gen_elements(children, multi_line.clone())).into_rc_path(); + + let els = with_indent_eoi(multi_line, els_items.into(), end_comments); + + p!(out, str("[") info(start) items(els) str("]") info(end)); + } +} + impl Printable for ExprBase { fn print(&self, out: &mut PrintItems) { match self { @@ -745,11 +800,7 @@ Self::ExprString(s) => p!(out, { s.text() }), Self::ExprNumber(n) => p!(out, { n.number() }), Self::ExprArray(a) => { - p!(out, str("[") >i nl); - for el in a.exprs() { - p!(out, {el} str(",") nl); - } - p!(out, { p!(out, { obj.obj_body() }); --- a/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@basic_array.jsonnet.snap +++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@basic_array.jsonnet.snap @@ -3,10 +3,4 @@ expression: reformat(&input) input_file: crates/jrsonnet-formatter/src/tests/basic_array.jsonnet --- -[ - 1, - 2, - 3, - 4, - 5, -] +[ 1, 2, 3, 4, 5 ] --- a/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@complex_nested.jsonnet.snap +++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@complex_nested.jsonnet.snap @@ -22,9 +22,7 @@ { name: 'myapp', image: 'myapp:latest', - ports: [ - { containerPort: 8080 }, - ], + ports: [ { containerPort: 8080 } ], env: [ { name: 'FOO', value: 'bar' }, { --- a/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@comprehensions.jsonnet.snap +++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@comprehensions.jsonnet.snap @@ -4,26 +4,10 @@ input_file: crates/jrsonnet-formatter/src/tests/comprehensions.jsonnet --- { - arr: [x for x in [ - 1, - 2, - 3, - ]], - filtered: [x for x in [ - 1, - 2, - 3, - 4, - 5, - ] if x > 2], + arr: [x for x in [ 1, 2, 3 ]], + filtered: [x for x in [ 1, 2, 3, 4, 5 ] if x > 2], obj: { [k]: v, - for k in [ - 'a', - 'b', - ]for v in [ - 1, - 2, - ] + for k in [ 'a', 'b' ]for v in [ 1, 2 ] }, } --- a/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@operators.jsonnet.snap +++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@operators.jsonnet.snap @@ -8,11 +8,5 @@ comparison: 1 < 2 && 3 > 2 || false, string_concat: 'hello' + ' ' + 'world', object_concat: { a: 1 } + { b: 2 }, - array_concat: [ - 1, - 2, - ] + [ - 3, - 4, - ], + array_concat: [ 1, 2 ] + [ 3, 4 ], } --- a/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@std_functions.jsonnet.snap +++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@std_functions.jsonnet.snap @@ -4,19 +4,8 @@ input_file: crates/jrsonnet-formatter/src/tests/std_functions.jsonnet --- { - length: std.length( - [ - 1, - 2, - 3, - ], - ), + length: std.length([ 1, 2, 3 ]), type: std.type('hello'), - format: std.format( - 'Hello, %s!', - [ - 'world', - ], - ), + format: std.format('Hello, %s!', [ 'world' ]), manifest: std.manifestJsonEx({ foo: 'bar' }, ' '), }