git.delta.rocks / jrsonnet / refs/commits / 0c7230b6eb21

difftreelog

fix(fmt) multi-line ArgsDesc

nxklpotmYaroslav Bolyukin2026-02-08parent: #36e84a6.patch.diff
in: master

8 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -644,6 +644,8 @@
 dependencies = [
  "dprint-core",
  "hi-doc",
+ "indoc",
+ "insta",
  "jrsonnet-rowan-parser",
 ]
 
modifiedcmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet-fmt/src/main.rs
+++ b/cmds/jrsonnet-fmt/src/main.rs
@@ -1,4 +1,9 @@
-use std::{fs, io};
+use std::{
+	fs,
+	io::{self, Write as _},
+	path::PathBuf,
+	process,
+};
 
 use clap::Parser;
 use jrsonnet_formatter::{format, FormatOptions};
modifiedcrates/jrsonnet-formatter/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-formatter/Cargo.toml
+++ b/crates/jrsonnet-formatter/Cargo.toml
@@ -9,6 +9,8 @@
 [dependencies]
 dprint-core.workspace = true
 hi-doc.workspace = true
+indoc.workspace = true
+insta.workspace = true
 jrsonnet-rowan-parser.workspace = true
 
 [lints]
modifiedcrates/jrsonnet-formatter/src/lib.rsdiffbeforeafterboth
22
3use children::{children_between, trivia_before};3use children::{children_between, trivia_before};
4use dprint_core::formatting::{4use dprint_core::formatting::{
5 condition_helpers::is_multiple_lines, condition_resolvers::true_resolver,5 condition_helpers::is_multiple_lines,
6 ir_helpers::{new_line_group, with_indent},
6 ConditionResolverContext, LineNumber, PrintItems, PrintOptions,7 ConditionResolver, ConditionResolverContext, LineNumber, PrintItems, PrintOptions,
7};8};
8use hi_doc::{Formatting, SnippetBuilder};9use hi_doc::{Formatting, SnippetBuilder};
9use jrsonnet_rowan_parser::{10use jrsonnet_rowan_parser::{
17};18};
1819
19use crate::{20use crate::{
20 children::trivia_after,21 children::{trivia_after, Child},
21 comments::{format_comments, CommentLocation},22 comments::{format_comments, CommentLocation},
22};23};
2324
49 $o.push_signal(dprint_core::formatting::Signal::NewLine);50 $o.push_signal(dprint_core::formatting::Signal::NewLine);
50 pi!(@s; $o: $($t)*);51 pi!(@s; $o: $($t)*);
51 }};52 }};
53 (@s; $o:ident: sonl $($t:tt)*) => {{
54 $o.push_signal(dprint_core::formatting::Signal::SpaceOrNewLine);
55 pi!(@s; $o: $($t)*);
56 }};
52 (@s; $o:ident: tab $($t:tt)*) => {{57 (@s; $o:ident: tab $($t:tt)*) => {{
53 $o.push_signal(dprint_core::formatting::Signal::Tab);58 $o.push_signal(dprint_core::formatting::Signal::Tab);
54 pi!(@s; $o: $($t)*);59 pi!(@s; $o: $($t)*);
65 $o.push_info($v);70 $o.push_info($v);
66 pi!(@s; $o: $($t)*);71 pi!(@s; $o: $($t)*);
67 }};72 }};
73 (@s; $o:ident: ln_anchor($v:expr) $($t:tt)*) => {{
74 $o.push_anchor(LineNumberAnchor::new($v));
75 pi!(@s; $o: $($t)*);
76 }};
68 (@s; $o:ident: if($s:literal, $cond:expr, $($i:tt)*) $($t:tt)*) => {{77 (@s; $o:ident: if($s:literal, $cond:expr, $($i:tt)*) $($t:tt)*) => {{
69 $o.push_condition(dprint_core::formatting::conditions::if_true(78 $o.push_condition(dprint_core::formatting::conditions::if_true(
70 $s,79 $s,
290}299}
291impl Printable for ArgsDesc {300impl Printable for ArgsDesc {
292 fn print(&self, out: &mut PrintItems) {301 fn print(&self, out: &mut PrintItems) {
293 let start = LineNumber::new("start");302 let start = LineNumber::new("args start line");
294 let end = LineNumber::new("end");303 let end = LineNumber::new("args end line");
295 let multi_line = Rc::new(move |condition_context: &mut ConditionResolverContext| {304 let multi_line = Rc::new(move |condition_context: &mut ConditionResolverContext| {
296 is_multiple_lines(condition_context, start, end).map(|v| !v)305 is_multiple_lines(condition_context, start, end)
297 });306 });
298 p!(out, str("(") info(start) if("start args", multi_line, >i nl));307
299 let (children, end_comments) = children_between::<Arg>(308 let (children, end_comments) = children_between::<Arg>(
300 self.syntax().clone(),309 self.syntax().clone(),
301 self.l_paren_token().map(Into::into).as_ref(),310 self.l_paren_token().map(Into::into).as_ref(),
302 self.r_paren_token().map(Into::into).as_ref(),311 self.r_paren_token().map(Into::into).as_ref(),
303 None,312 None,
304 );313 );
314
315 fn gen_args(children: Vec<Child<Arg>>, multi_line: ConditionResolver) -> PrintItems {
316 let mut _out = PrintItems::new();
317 let out = &mut _out;
318
305 let mut args = children.into_iter().peekable();319 let mut args = children.into_iter().peekable();
306 while let Some(ele) = args.next() {320 while let Some(ele) = args.next() {
312 if arg.name().is_some() || arg.assign_token().is_some() {326 if arg.name().is_some() || arg.assign_token().is_some() {
313 p!(out, {arg.name()} str(" = "));327 p!(out, {arg.name()} str(" = "));
314 }328 }
329 p!(out, { arg.expr() });
315 let comma_between = if args.peek().is_some() {330 let has_more = args.peek().is_some();
316 true_resolver()331 if has_more {
317 } else {332 p!(out, str(","));
318 multi_line.clone()333 } else {
319 };
320 p!(out, {arg.expr()} if("arg comma", comma_between, str(",") if_not("between args", multi_line, str(" "))));334 p!(out, if("trailing comma", multi_line, str(",")));
335 }
321 format_comments(&ele.inline_trivia, CommentLocation::ItemInline, out);336 format_comments(&ele.inline_trivia, CommentLocation::ItemInline, out);
337 if has_more {
322 p!(out, if("between args", multi_line, nl));338 p!(out, if_else("arg separator", multi_line, nl)(sonl));
339 }
323 }340 }
341 _out
342 }
343
344 let args_items = new_line_group(gen_args(children, multi_line.clone())).into_rc_path();
345 let args_indented = with_indent(pi!(@i; nl items(args_items.into())));
346
347 p!(out, str("(") info(start));
348 p!(out, if_else("args body", multi_line, items(args_indented) nl)(items(args_items.into())));
324 if end_comments.should_start_with_newline {349 if end_comments.should_start_with_newline {
325 p!(out, nl);350 p!(out, nl);
326 }351 }
327 format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);352 format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);
328 p!(out, if("end args", multi_line, <i info(end)) str(")"));353 p!(out, str(")") info(end));
329 }354 }
330}355}
331impl Printable for SliceDesc {356impl Printable for SliceDesc {
deletedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_fmt__tests__complex_comments_snapshot.snapdiffbeforeafterboth
--- a/crates/jrsonnet-formatter/src/snapshots/jrsonnet_fmt__tests__complex_comments_snapshot.snap
+++ /dev/null
@@ -1,53 +0,0 @@
----
-source: cmds/jrsonnet-fmt/src/tests.rs
-expression: "reformat(indoc!(\"{\n\t\t  comments: {\n\t\t\t_: '',\n\t\t\t//     Plain comment\n\t\t\ta: '',\n\n\t\t\t#    Plain comment with empty line before\n\t\t\tb: '',\n\t\t\t/*Single-line multiline comment\n\n\t\t\t*/\n\t\t\tc: '',\n\n\t\t\t/**Single-line multiline doc comment\n\n\t\t\t*/\n\t\t\tc: '',\n\n\t\t\t/**Multiline doc\n\t\t\tComment\n\t\t\t*/\n\t\t\tc: '',\n\n\t\t\t/*\n\n\tMulti-line\n\n\tcomment\n\t\t\t*/\n\t\t\td: '',\n\n\t\t\te: '', // Inline comment\n\n\t\t\tk: '',\n\n\t\t\t// Text after everything\n\t\t  },\n\t\t  comments2: {\n\t\t\tk: '',\n\t\t\t// Text after everything, but no newline above\n\t\t  },\n          spacing: {\n            a: '',\n\n            b: '',\n          },\n          noSpacing: {\n            a: '',\n            b: '',\n          },\n        }\"))"
----
-{
-	comments: {
-		_: '',
-		// Plain comment
-		a: '',
-
-		# Plain comment with empty line before
-		b: '',
-		/* Single-line multiline comment */
-		c: '',
-
-		/**
-		 * Single-line multiline doc comment
-		 */
-		c: '',
-
-		/**
-		 * Multiline doc
-		 * Comment
-		 */
-		c: '',
-
-		/*
-		Multi-line
-
-		comment
-		*/
-		d: '',
-
-		e: '', // Inline comment
-
-		k: '',
-
-		// Text after everything
-	},
-	comments2: {
-		k: '',
-		// Text after everything, but no newline above
-	},
-	spacing: {
-		a: '',
-
-		b: '',
-	},
-	noSpacing: {
-		a: '',
-		b: '',
-	},
-}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__args.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__args.snap
@@ -0,0 +1,36 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: "reformat(indoc!(\"\n\t\t\t{\n\t\t\t\tshort: aaa(1,2,3,4,5),\n\t\t\t\tlong: bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),\n\t\t\t\tshort_in_long: bbb(aaa(1,2,3,4,5), 123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),\n\t\t\t\tlong_in_short: aaa(1,2,3,4,5,bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123)),\n\t\t\t}\n\t\t\"))"
+---
+{
+	short: aaa(1, 2, 3, 4, 5),
+	long: bbb(
+		123123123123123123123,
+		12312312321123123123,
+		123123123123312123123,
+		123123123123123123312,
+		123123123123312321123,
+	),
+	short_in_long: bbb(
+		aaa(1, 2, 3, 4, 5),
+		123123123123123123123,
+		12312312321123123123,
+		123123123123312123123,
+		123123123123123123312,
+		123123123123312321123,
+	),
+	long_in_short: aaa(
+		1,
+		2,
+		3,
+		4,
+		5,
+		bbb(
+			123123123123123123123,
+			12312312321123123123,
+			123123123123312123123,
+			123123123123123123312,
+			123123123123312321123,
+		),
+	),
+}
addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__complex_comments.snapdiffbeforeafterboth
--- /dev/null
+++ b/crates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__complex_comments.snap
@@ -0,0 +1,53 @@
+---
+source: crates/jrsonnet-formatter/src/tests.rs
+expression: "reformat(indoc!(\"{\n\t\t  comments: {\n\t\t\t_: '',\n\t\t\t//     Plain comment\n\t\t\ta: '',\n\n\t\t\t#    Plain comment with empty line before\n\t\t\tb: '',\n\t\t\t/*Single-line multiline comment\n\n\t\t\t*/\n\t\t\tc: '',\n\n\t\t\t/**Single-line multiline doc comment\n\n\t\t\t*/\n\t\t\tc: '',\n\n\t\t\t/**Multiline doc\n\t\t\tComment\n\t\t\t*/\n\t\t\tc: '',\n\n\t\t\t/*\n\n\tMulti-line\n\n\tcomment\n\t\t\t*/\n\t\t\td: '',\n\n\t\t\te: '', // Inline comment\n\n\t\t\tk: '',\n\n\t\t\t// Text after everything\n\t\t  },\n\t\t  comments2: {\n\t\t\tk: '',\n\t\t\t// Text after everything, but no newline above\n\t\t  },\n          spacing: {\n            a: '',\n\n            b: '',\n          },\n          noSpacing: {\n            a: '',\n            b: '',\n          },\n        }\"))"
+---
+{
+	comments: {
+		_: '',
+		// Plain comment
+		a: '',
+
+		# Plain comment with empty line before
+		b: '',
+		/* Single-line multiline comment */
+		c: '',
+
+		/**
+		 * Single-line multiline doc comment
+		 */
+		c: '',
+
+		/**
+		 * Multiline doc
+		 * Comment
+		 */
+		c: '',
+
+		/*
+		Multi-line
+
+		comment
+		*/
+		d: '',
+
+		e: '', // Inline comment
+
+		k: '',
+
+		// Text after everything
+	},
+	comments2: {
+		k: '',
+		// Text after everything, but no newline above
+	},
+	spacing: {
+		a: '',
+
+		b: '',
+	},
+	noSpacing: {
+		a: '',
+		b: '',
+	},
+}
modifiedcrates/jrsonnet-formatter/src/tests.rsdiffbeforeafterboth
--- a/crates/jrsonnet-formatter/src/tests.rs
+++ b/crates/jrsonnet-formatter/src/tests.rs
@@ -22,7 +22,7 @@
 }
 
 #[test]
-fn complex_comments_snapshot() {
+fn complex_comments() {
 	insta::assert_snapshot!(reformat(indoc!(
 		"{
 		  comments: {
@@ -77,3 +77,17 @@
         }"
 	)));
 }
+
+#[test]
+fn args() {
+	insta::assert_snapshot!(reformat(indoc!(
+		"
+			{
+				short: aaa(1,2,3,4,5),
+				long: bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),
+				short_in_long: bbb(aaa(1,2,3,4,5), 123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),
+				long_in_short: aaa(1,2,3,4,5,bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123)),
+			}
+		"
+	)));
+}