From 6e9ec99b50ff3a3fa9bd59f3be71b00560e56391 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 25 Mar 2026 16:42:25 +0300 Subject: [PATCH] vfmt: fix inconsistent wrapping of lines with + and - (fixes #25992) --- vlib/v/fmt/fmt.v | 27 ++++++++++++++----- .../fmt/tests/additive_infix_wrap_expected.vv | 11 ++++++++ vlib/v/fmt/tests/additive_infix_wrap_input.vv | 9 +++++++ .../tests/too_long_infix_expressions_keep.vv | 3 ++- 4 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 vlib/v/fmt/tests/additive_infix_wrap_expected.vv create mode 100644 vlib/v/fmt/tests/additive_infix_wrap_input.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 2b873e828..552abd0bb 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2552,7 +2552,9 @@ pub fn (mut f Fmt) index_expr(node ast.IndexExpr) { pub fn (mut f Fmt) infix_expr(node ast.InfixExpr) { buffering_save := f.buffering - if !f.buffering && node.op in [.logical_or, .and, .plus] { + is_wrappable_additive_minus := node.op == .minus + && (is_additive_infix(node.left) || is_additive_infix(node.right)) + if !f.buffering && (node.op in [.logical_or, .and, .plus] || is_wrappable_additive_minus) { f.buffering = true } is_assign_save := f.is_assign @@ -2625,7 +2627,7 @@ pub fn (mut f Fmt) infix_expr(node ast.InfixExpr) { pub fn (mut f Fmt) wrap_infix(start_pos int, start_len int, is_cond bool) { cut_span := f.out.len - start_pos infix_str := f.out.cut_last(cut_span) - if !infix_str.contains_any_substr(['&&', '||', '+']) { + if !infix_str.contains_any_substr(['&&', '||', '+', '-']) { f.write(infix_str) return } @@ -2637,6 +2639,13 @@ pub fn (mut f Fmt) wrap_infix(start_pos int, start_len int, is_cond bool) { f.write_splitted_infix(conditions, penalties, false, is_cond) } +fn is_additive_infix(expr ast.Expr) bool { + return match expr { + ast.InfixExpr { expr.op in [.plus, .minus] } + else { false } + } +} + fn split_up_infix(infix_str string, ignore_paren bool, is_cond_infix bool) ([]string, []int) { mut conditions := [''] mut penalties := [5] @@ -2654,11 +2663,15 @@ fn split_up_infix(infix_str string, ignore_paren bool, is_cond_infix bool) ([]st conditions << '${p} ' ind++ } - } else if !is_cond_infix && p == '+' { - penalties << 5 - conditions[ind] += '${p} ' - conditions << '' - ind++ + } else if !is_cond_infix && p in ['+', '-'] { + if inside_paren { + conditions[ind] += '${p} ' + } else { + penalties << 5 + conditions[ind] += '${p} ' + conditions << '' + ind++ + } } else { conditions[ind] += '${p} ' if ignore_paren { diff --git a/vlib/v/fmt/tests/additive_infix_wrap_expected.vv b/vlib/v/fmt/tests/additive_infix_wrap_expected.vv new file mode 100644 index 000000000..eef24f3e3 --- /dev/null +++ b/vlib/v/fmt/tests/additive_infix_wrap_expected.vv @@ -0,0 +1,11 @@ +fn main() { + long_var_name := 0 + + x := long_var_name + long_var_name + long_var_name + long_var_name + long_var_name + + long_var_name + long_var_name + + y := long_var_name + long_var_name + long_var_name + long_var_name + long_var_name - + long_var_name - long_var_name + + println(x + y) +} diff --git a/vlib/v/fmt/tests/additive_infix_wrap_input.vv b/vlib/v/fmt/tests/additive_infix_wrap_input.vv new file mode 100644 index 000000000..c857a9d27 --- /dev/null +++ b/vlib/v/fmt/tests/additive_infix_wrap_input.vv @@ -0,0 +1,9 @@ +fn main() { + long_var_name := 0 + + x := long_var_name + long_var_name + long_var_name + long_var_name + long_var_name + long_var_name + long_var_name + + y := long_var_name + long_var_name + long_var_name + long_var_name + long_var_name - long_var_name - long_var_name + + println(x + y) +} diff --git a/vlib/v/fmt/tests/too_long_infix_expressions_keep.vv b/vlib/v/fmt/tests/too_long_infix_expressions_keep.vv index 79c008407..217a03cbb 100644 --- a/vlib/v/fmt/tests/too_long_infix_expressions_keep.vv +++ b/vlib/v/fmt/tests/too_long_infix_expressions_keep.vv @@ -32,7 +32,8 @@ fn s_adjustsoundparams(listener &Abc, source &Abc, vol &int, sep &int) int { if approx_dist > (1200 * (1 << 16)) { approx_dist = (1200 * (1 << 16)) } - *vol = 15 +((snd_SfxVolume - 15) * (((1200 * (1 << 16)) - approx_dist) >> 16)) / (((1200 * (1 << 16)) - (200 * (1 << 16))) >> 16) + *vol = 15 + ((snd_SfxVolume - 15) * (((1200 * (1 << 16)) - + approx_dist) >> 16)) / (((1200 * (1 << 16)) - (200 * (1 << 16))) >> 16) } else { *vol = (snd_SfxVolume * (((1200 * (1 << 16)) - approx_dist) >> 16)) / (((1200 * (1 << 16)) - (200 * (1 << 16))) >> 16) } -- 2.39.5