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
644dependencies = [644dependencies = [
645 "dprint-core",645 "dprint-core",
646 "hi-doc",646 "hi-doc",
647 "indoc",
648 "insta",
647 "jrsonnet-rowan-parser",649 "jrsonnet-rowan-parser",
648]650]
649651
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
9[dependencies]9[dependencies]
10dprint-core.workspace = true10dprint-core.workspace = true
11hi-doc.workspace = true11hi-doc.workspace = true
12indoc.workspace = true
13insta.workspace = true
12jrsonnet-rowan-parser.workspace = true14jrsonnet-rowan-parser.workspace = true
1315
14[lints]16[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

no changes

addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__args.snapdiffbeforeafterboth

no changes

addedcrates/jrsonnet-formatter/src/snapshots/jrsonnet_formatter__tests__complex_comments.snapdiffbeforeafterboth

no changes

modifiedcrates/jrsonnet-formatter/src/tests.rsdiffbeforeafterboth
22}22}
2323
24#[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}
80
81#[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