difftreelog
feat format arrays in one line if possible
in: master
6 files changed
crates/jrsonnet-formatter/src/lib.rsdiffbeforeafterboth12 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}718719impl 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 }731732 let source_is_multiline =733 children.iter().any(|c| c.triggers_multiline) || end_comments.should_start_with_newline;734735 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 };742743 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 _out763 }764765 let els_items = new_line_group(gen_elements(children, multi_line.clone())).into_rc_path();766767 let els = with_indent_eoi(multi_line, els_items.into(), end_comments);768769 p!(out, str("[") info(start) items(els) str("]") info(end));770 }771}772718impl 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() });crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@basic_array.jsonnet.snapdiffbeforeafterboth--- 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 ]
crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@complex_nested.jsonnet.snapdiffbeforeafterboth--- 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' },
{
crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@comprehensions.jsonnet.snapdiffbeforeafterboth--- 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 ]
},
}
crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@operators.jsonnet.snapdiffbeforeafterboth--- 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 ],
}
crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__snapshots@std_functions.jsonnet.snapdiffbeforeafterboth--- 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' }, ' '),
}