difftreelog
fix(fmt) multi-line ArgsDesc
in: master
8 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -644,6 +644,8 @@
dependencies = [
"dprint-core",
"hi-doc",
+ "indoc",
+ "insta",
"jrsonnet-rowan-parser",
]
cmds/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};
crates/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]
crates/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 {
crates/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: '',
- },
-}
crates/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,
+ ),
+ ),
+}
crates/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: '',
+ },
+}
crates/jrsonnet-formatter/src/tests.rsdiffbeforeafterboth22}22}232324#[test]24#[test]25fn complex_comments_snapshot() {25fn complex_comments() {26 insta::assert_snapshot!(reformat(indoc!(26 insta::assert_snapshot!(reformat(indoc!(27 "{27 "{28 comments: {28 comments: {78 )));78 )));79}79}8081#[test]82fn args() {83 insta::assert_snapshot!(reformat(indoc!(84 "85 {86 short: aaa(1,2,3,4,5),87 long: bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),88 short_in_long: bbb(aaa(1,2,3,4,5), 123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123),89 long_in_short: aaa(1,2,3,4,5,bbb(123123123123123123123,12312312321123123123,123123123123312123123,123123123123123123312,123123123123312321123)),90 }91 "92 )));93}8094