git.delta.rocks / jrsonnet / refs/commits / 8aaa082d581c

difftreelog

feat format arrays in one line if possible

lklpxturYaroslav Bolyukin2026-02-12parent: #d32a788.patch.diff
in: master

6 files changed

modifiedcrates/jrsonnet-formatter/src/lib.rsdiffbeforeafterboth
12 collect_lexed_str_block,12 collect_lexed_str_block,
13 nodes::{13 nodes::{
14 Arg, ArgsDesc, Assertion, BinaryOperator, Bind, CompSpec, Destruct, DestructArrayPart,14 Arg, ArgsDesc, Assertion, BinaryOperator, Bind, CompSpec, Destruct, DestructArrayPart,
15 DestructRest, Expr, ExprBase, FieldName, ForSpec, IfSpec, ImportKind, Literal, Member,15 DestructRest, Expr, ExprArray, ExprBase, FieldName, ForSpec, IfSpec, ImportKind, Literal,
16 Name, Number, ObjBody, ObjLocal, ParamsDesc, SliceDesc, SourceFile, Stmt, Suffix, Text,16 Member, Name, Number, ObjBody, ObjLocal, ParamsDesc, SliceDesc, SourceFile, Stmt, Suffix,
17 TextKind, UnaryOperator, Visibility,17 Text, TextKind, UnaryOperator, Visibility,
18 },18 },
716 }716 }
717}717}
718
719impl Printable for ExprArray {
720 fn print(&self, out: &mut PrintItems) {
721 let (children, end_comments) = children_between::<Expr>(
722 self.syntax().clone(),
723 self.l_brack_token().map(Into::into).as_ref(),
724 self.r_brack_token().map(Into::into).as_ref(),
725 None,
726 );
727 if children.is_empty() && end_comments.is_empty() {
728 p!(out, str("[ ]"));
729 return;
730 }
731
732 let source_is_multiline =
733 children.iter().any(|c| c.triggers_multiline) || end_comments.should_start_with_newline;
734
735 let start = LineNumber::new("arr start line");
736 let end = LineNumber::new("arr end line");
737 let multi_line: ConditionResolver = if source_is_multiline {
738 true_resolver()
739 } else {
740 Rc::new(move |ctx: &mut ConditionResolverContext| is_multiple_lines(ctx, start, end))
741 };
742
743 fn gen_elements(children: Vec<Child<Expr>>, multi_line: ConditionResolver) -> PrintItems {
744 let mut _out = PrintItems::new();
745 let out = &mut _out;
746 let mut els = children.into_iter().peekable();
747 while let Some(el) = els.next() {
748 if el.should_start_with_newline {
749 p!(out, nl);
750 }
751 format_comments(&el.before_trivia, CommentLocation::AboveItem, out);
752 p!(out, { el.value });
753 let has_more = els.peek().is_some();
754 if has_more {
755 p!(out, str(","));
756 } else {
757 p!(out, if("trailing comma", multi_line, str(",")));
758 }
759 format_comments(&el.inline_trivia, CommentLocation::ItemInline, out);
760 p!(out, if_else("element separator", multi_line, nl)(sonl))
761 }
762 _out
763 }
764
765 let els_items = new_line_group(gen_elements(children, multi_line.clone())).into_rc_path();
766
767 let els = with_indent_eoi(multi_line, els_items.into(), end_comments);
768
769 p!(out, str("[") info(start) items(els) str("]") info(end));
770 }
771}
772
718impl Printable for ExprBase {773impl Printable for ExprBase {
719 fn print(&self, out: &mut PrintItems) {774 fn print(&self, out: &mut PrintItems) {
745 Self::ExprString(s) => p!(out, { s.text() }),800 Self::ExprString(s) => p!(out, { s.text() }),
746 Self::ExprNumber(n) => p!(out, { n.number() }),801 Self::ExprNumber(n) => p!(out, { n.number() }),
747 Self::ExprArray(a) => {802 Self::ExprArray(a) => {
748 p!(out, str("[") >i nl);
749 for el in a.exprs() {
750 p!(out, {el} str(",") nl);803 p!(out, { a })
751 }
752 p!(out, <i str("]"));
753 }804 }
754 Self::ExprObject(obj) => {805 Self::ExprObject(obj) => {
755 p!(out, { obj.obj_body() });806 p!(out, { obj.obj_body() });
modifiedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@basic_array.jsonnet.snapdiffbeforeafterboth
3expression: reformat(&input)3expression: reformat(&input)
4input_file: crates/jrsonnet-formatter/src/tests/basic_array.jsonnet4input_file: crates/jrsonnet-formatter/src/tests/basic_array.jsonnet
5---5---
6[6[ 1, 2, 3, 4, 5 ]
7 1,
8 2,
9 3,
10 4,
11 5,
12]
137
modifiedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@complex_nested.jsonnet.snapdiffbeforeafterboth
22 {22 {
23 name: 'myapp',23 name: 'myapp',
24 image: 'myapp:latest',24 image: 'myapp:latest',
25 ports: [25 ports: [ { containerPort: 8080 } ],
26 { containerPort: 8080 },
27 ],
28 env: [26 env: [
29 { name: 'FOO', value: 'bar' },27 { name: 'FOO', value: 'bar' },
30 {28 {
modifiedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@comprehensions.jsonnet.snapdiffbeforeafterboth
4input_file: crates/jrsonnet-formatter/src/tests/comprehensions.jsonnet4input_file: crates/jrsonnet-formatter/src/tests/comprehensions.jsonnet
5---5---
6{6{
7 arr: [x for x in [7 arr: [x for x in [ 1, 2, 3 ]],
8 1,
9 2,
10 3,
11 ]],
12 filtered: [x for x in [8 filtered: [x for x in [ 1, 2, 3, 4, 5 ] if x > 2],
13 1,
14 2,
15 3,
16 4,
17 5,
18 ] if x > 2],
19 obj: {9 obj: {
20 [k]: v,10 [k]: v,
21 for k in [11 for k in [ 'a', 'b' ]for v in [ 1, 2 ]
22 'a',
23 'b',
24 ]for v in [
25 1,
26 2,
27 ]
28 },12 },
29}13}
3014
modifiedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@operators.jsonnet.snapdiffbeforeafterboth
8 comparison: 1 < 2 && 3 > 2 || false,8 comparison: 1 < 2 && 3 > 2 || false,
9 string_concat: 'hello' + ' ' + 'world',9 string_concat: 'hello' + ' ' + 'world',
10 object_concat: { a: 1 } + { b: 2 },10 object_concat: { a: 1 } + { b: 2 },
11 array_concat: [11 array_concat: [ 1, 2 ] + [ 3, 4 ],
12 1,
13 2,
14 ] + [
15 3,
16 4,
17 ],
18}12}
1913
modifiedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@std_functions.jsonnet.snapdiffbeforeafterboth
4input_file: crates/jrsonnet-formatter/src/tests/std_functions.jsonnet4input_file: crates/jrsonnet-formatter/src/tests/std_functions.jsonnet
5---5---
6{6{
7 length: std.length(7 length: std.length([ 1, 2, 3 ]),
8 [
9 1,
10 2,
11 3,
12 ],
13 ),
14 type: std.type('hello'),8 type: std.type('hello'),
15 format: std.format(9 format: std.format('Hello, %s!', [ 'world' ]),
16 'Hello, %s!',
17 [
18 'world',
19 ],
20 ),
21 manifest: std.manifestJsonEx({ foo: 'bar' }, ' '),10 manifest: std.manifestJsonEx({ foo: 'bar' }, ' '),
22}11}
2312