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
1use std::{fs, io};1use std::{
2 fs,
3 io::{self, Write as _},
4 path::PathBuf,
5 process,
6};
27
3use clap::Parser;8use clap::Parser;
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
--- a/crates/jrsonnet-formatter/src/lib.rs
+++ b/crates/jrsonnet-formatter/src/lib.rs
@@ -2,8 +2,9 @@
 
 use children::{children_between, trivia_before};
 use dprint_core::formatting::{
-	condition_helpers::is_multiple_lines, condition_resolvers::true_resolver,
-	ConditionResolverContext, LineNumber, PrintItems, PrintOptions,
+	condition_helpers::is_multiple_lines,
+	ir_helpers::{new_line_group, with_indent},
+	ConditionResolver, ConditionResolverContext, LineNumber, PrintItems, PrintOptions,
 };
 use hi_doc::{Formatting, SnippetBuilder};
 use jrsonnet_rowan_parser::{
@@ -17,7 +18,7 @@
 };
 
 use crate::{
-	children::trivia_after,
+	children::{trivia_after, Child},
 	comments::{format_comments, CommentLocation},
 };
 
@@ -49,6 +50,10 @@
 		$o.push_signal(dprint_core::formatting::Signal::NewLine);
 		pi!(@s; $o: $($t)*);
 	}};
+	(@s; $o:ident: sonl $($t:tt)*) => {{
+		$o.push_signal(dprint_core::formatting::Signal::SpaceOrNewLine);
+		pi!(@s; $o: $($t)*);
+	}};
 	(@s; $o:ident: tab $($t:tt)*) => {{
 		$o.push_signal(dprint_core::formatting::Signal::Tab);
 		pi!(@s; $o: $($t)*);
@@ -65,6 +70,10 @@
 		$o.push_info($v);
 		pi!(@s; $o: $($t)*);
 	}};
+	(@s; $o:ident: ln_anchor($v:expr) $($t:tt)*) => {{
+		$o.push_anchor(LineNumberAnchor::new($v));
+		pi!(@s; $o: $($t)*);
+	}};
 	(@s; $o:ident: if($s:literal, $cond:expr, $($i:tt)*) $($t:tt)*) => {{
 		$o.push_condition(dprint_core::formatting::conditions::if_true(
 			$s,
@@ -290,42 +299,58 @@
 }
 impl Printable for ArgsDesc {
 	fn print(&self, out: &mut PrintItems) {
-		let start = LineNumber::new("start");
-		let end = LineNumber::new("end");
+		let start = LineNumber::new("args start line");
+		let end = LineNumber::new("args end line");
 		let multi_line = Rc::new(move |condition_context: &mut ConditionResolverContext| {
-			is_multiple_lines(condition_context, start, end).map(|v| !v)
+			is_multiple_lines(condition_context, start, end)
 		});
-		p!(out, str("(") info(start) if("start args", multi_line, >i nl));
+
 		let (children, end_comments) = children_between::<Arg>(
 			self.syntax().clone(),
 			self.l_paren_token().map(Into::into).as_ref(),
 			self.r_paren_token().map(Into::into).as_ref(),
 			None,
 		);
-		let mut args = children.into_iter().peekable();
-		while let Some(ele) = args.next() {
-			if ele.should_start_with_newline {
-				p!(out, nl);
-			}
-			format_comments(&ele.before_trivia, CommentLocation::AboveItem, out);
-			let arg = ele.value;
-			if arg.name().is_some() || arg.assign_token().is_some() {
-				p!(out, {arg.name()} str(" = "));
+
+		fn gen_args(children: Vec<Child<Arg>>, multi_line: ConditionResolver) -> PrintItems {
+			let mut _out = PrintItems::new();
+			let out = &mut _out;
+
+			let mut args = children.into_iter().peekable();
+			while let Some(ele) = args.next() {
+				if ele.should_start_with_newline {
+					p!(out, nl);
+				}
+				format_comments(&ele.before_trivia, CommentLocation::AboveItem, out);
+				let arg = ele.value;
+				if arg.name().is_some() || arg.assign_token().is_some() {
+					p!(out, {arg.name()} str(" = "));
+				}
+				p!(out, { arg.expr() });
+				let has_more = args.peek().is_some();
+				if has_more {
+					p!(out, str(","));
+				} else {
+					p!(out, if("trailing comma", multi_line, str(",")));
+				}
+				format_comments(&ele.inline_trivia, CommentLocation::ItemInline, out);
+				if has_more {
+					p!(out, if_else("arg separator", multi_line, nl)(sonl));
+				}
 			}
-			let comma_between = if args.peek().is_some() {
-				true_resolver()
-			} else {
-				multi_line.clone()
-			};
-			p!(out, {arg.expr()} if("arg comma", comma_between, str(",") if_not("between args", multi_line, str(" "))));
-			format_comments(&ele.inline_trivia, CommentLocation::ItemInline, out);
-			p!(out, if("between args", multi_line, nl));
+			_out
 		}
+
+		let args_items = new_line_group(gen_args(children, multi_line.clone())).into_rc_path();
+		let args_indented = with_indent(pi!(@i; nl items(args_items.into())));
+
+		p!(out, str("(") info(start));
+		p!(out, if_else("args body", multi_line, items(args_indented) nl)(items(args_items.into())));
 		if end_comments.should_start_with_newline {
 			p!(out, nl);
 		}
 		format_comments(&end_comments.trivia, CommentLocation::EndOfItems, out);
-		p!(out, if("end args", multi_line, <i info(end)) str(")"));
+		p!(out, str(")") info(end));
 	}
 }
 impl 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)),
+			}
+		"
+	)));
+}