From 2a135f04c20c645ba1e175a6c917a7137c5d2d62 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 10 Nov 2026 14:42:54 -0500 Subject: [PATCH] fix: keep a multi-line value of a hanging list on the line after the previous value --- src/generation/generate.rs | 59 +++++++++++++++++-- .../ArrayExpression_PreferHanging_Always.txt | 30 ++++++++++ .../CallExpression_PreferHanging_Always.txt | 20 +++++++ 3 files changed, 103 insertions(+), 6 deletions(-) diff --git a/src/generation/generate.rs b/src/generation/generate.rs index 50dd431b..04243c13 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -10354,6 +10354,7 @@ fn gen_separated_values_with_result<'a>(opts: GenSeparatedValuesParams<'a>, cont ListBlankLines::Force => 1, }; let node_sorter = opts.node_sorter; + let is_hanging = opts.prefer_hanging && !opts.force_use_new_lines; // would need to make this take into account the new position of the nodes #[cfg(debug_assertions)] @@ -10459,8 +10460,16 @@ fn gen_separated_values_with_result<'a>(opts: GenSeparatedValuesParams<'a>, cont _ => true, }; + let items = if use_new_line_group { ir_helpers::new_line_group(items) } else { items }; + // a value that's allowed to be inline multi-line is still pulled up beside the previous value (ex. a function) + let items = if is_hanging && i > 0 && use_new_line_group && !allow_inline_multi_line && is_value_on_new_line(&value, context) { + gen_multi_line_hanging_value_on_new_line(items, context) + } else { + items + }; + generated_nodes.push(ir_helpers::GeneratedValue { - items: if use_new_line_group { ir_helpers::new_line_group(items) } else { items }, + items, lines_span, allow_inline_multi_line, allow_inline_single_line, @@ -10505,15 +10514,53 @@ fn gen_separated_values_with_result<'a>(opts: GenSeparatedValuesParams<'a>, cont /// Gets if the first value starts on a later line than the token before it (ex. the open bracket). fn is_first_value_on_new_line<'a>(nodes: &[NodeOrSeparator<'a>], context: &Context<'a>) -> bool { - let Some(first_value) = nodes.first() else { - return false; - }; - match context.token_finder.get_previous_token(first_value) { - Some(previous_token) => node_helpers::get_use_new_lines_for_nodes(&previous_token.range(), first_value, context.program), + nodes.first().is_some_and(|first_value| is_value_on_new_line(first_value, context)) + } + + /// Gets if the value starts on a later line than the token before it (ex. the previous value's comma). + fn is_value_on_new_line<'a>(value: &NodeOrSeparator<'a>, context: &Context<'a>) -> bool { + match context.token_finder.get_previous_token(value) { + Some(previous_token) => node_helpers::get_use_new_lines_for_nodes(&previous_token.range(), value, context.program), None => false, } } + /// Keeps a value of a hanging list on the line after the previous value when it's multi-line. + /// + /// A value that doesn't fit on the previous value's line is moved to the next line before it's + /// broken up. Its line breaks are then kept when formatting again (ex. the ones between the + /// operands of a binary expression), so what's left on its first line may fit on the previous + /// value's line, but it stays where it is. + fn gen_multi_line_hanging_value_on_new_line<'a>(value_items: PrintItems<'a>, context: &Context<'a>) -> PrintItems<'a> { + let end_ln = LineNumber::new("hangingValueEnd"); + let value_items = { + let mut items = value_items; + items.push_info(end_ln); + items + } + .into_rc_path(); + let mut items = gen_line_numbers_region_start(context.arena, &[end_ln]); + items.push_condition(if_true_or( + "newLineIfMultiLineHangingValue", + context.arena.alloc_resolver(move |condition_context| { + // it was already moved to the next line when it didn't fit + if condition_context.writer_info.is_start_of_line() { + return Some(false); + } + let end_ln = condition_context.resolved_line_number(end_ln)?; + Some(end_ln > condition_context.writer_info.line_number) + }), + { + let mut items = PrintItems::new(context.arena); + items.push_signal(Signal::NewLine); + items.extend(with_indent(value_items.into_print_items(context.arena))); + items + }, + value_items.into_print_items(context.arena), + )); + items + } + /// Gets if each value has a value beside it of the same kind. fn get_has_same_kind_neighbor(nodes: &[NodeOrSeparator]) -> Vec { let mut result = vec![false; nodes.len()]; diff --git a/tests/specs/expressions/ArrayExpression/ArrayExpression_PreferHanging_Always.txt b/tests/specs/expressions/ArrayExpression/ArrayExpression_PreferHanging_Always.txt index f3f54521..fc7a6692 100644 --- a/tests/specs/expressions/ArrayExpression/ArrayExpression_PreferHanging_Always.txt +++ b/tests/specs/expressions/ArrayExpression/ArrayExpression_PreferHanging_Always.txt @@ -30,3 +30,33 @@ const t = [other, other, other, test.thisout]; [expect] const t = [other, other, other, test.thisout]; + +== should keep an element on the next line when it's broken up there == +const t = [1, aaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbb + ccccccccccc, 2]; + +[expect] +const t = [1, + aaaaaaaaaaaaaaaaaaa + + bbbbbbbbbbbbbbbbbbbbbb + + ccccccccccc, 2]; + +== should keep a multi-line element on the line it's written on == +const t = [1, aaaa + + bbbb, 2]; +const u = [1, + aaaa + + bbbb, 2]; + +[expect] +const t = [1, aaaa + + bbbb, 2]; +const u = [1, + aaaa + + bbbb, 2]; + +== should not keep an element on the next line when it's on a single line == +const t = [1, + aaaa + bbbb, 2]; + +[expect] +const t = [1, aaaa + bbbb, 2]; diff --git a/tests/specs/expressions/CallExpression/CallExpression_PreferHanging_Always.txt b/tests/specs/expressions/CallExpression/CallExpression_PreferHanging_Always.txt index 6305504f..de6f10b8 100644 --- a/tests/specs/expressions/CallExpression/CallExpression_PreferHanging_Always.txt +++ b/tests/specs/expressions/CallExpression/CallExpression_PreferHanging_Always.txt @@ -23,3 +23,23 @@ call( ), ), ); + +== should keep an argument on the next line when it's broken up there == +call(1, aaaaaaaaaaaaaaaaaaa + bbbbbbbbbbbbbbbbbbbbbb + ccccccccccc, 2); + +[expect] +call(1, + aaaaaaaaaaaaaaaaaaa + + bbbbbbbbbbbbbbbbbbbbbb + + ccccccccccc, 2); + +== should still move a function on the next line beside the previous argument == +call(1, + function() { + test; + }); + +[expect] +call(1, function() { + test; +});