From 31f567734213bba020f9a9793da6567cec1e9865 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 24 May 2026 23:22:24 +0300 Subject: [PATCH] fmt: keep single-line struct init inline inside single-line array literal (#27239) --- vlib/v/fmt/fmt.v | 10 ++++++++++ vlib/v/fmt/struct.v | 6 ++++++ vlib/v/fmt/tests/array_init_struct_inline_expected.vv | 11 +++++++++++ vlib/v/fmt/tests/array_init_struct_inline_input.vv | 11 +++++++++++ .../structs/struct_init_short_in_for_expr_test.v | 4 +--- 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 vlib/v/fmt/tests/array_init_struct_inline_expected.vv create mode 100644 vlib/v/fmt/tests/array_init_struct_inline_input.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 80ea3c2e1..29dcfc65c 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2074,7 +2074,17 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) { if !is_new_line && i > 0 { f.write(' ') } + // When the array stays on a single source line, keep nested + // struct inits on a single line too (instead of expanding their + // named fields onto multiple lines). + keep_struct_inline := !line_break && expr is ast.StructInit + && expr.pos.line_nr == expr.pos.last_line && expr.pre_comments.len == 0 + old_single_line_fields := f.single_line_fields + if keep_struct_inline { + f.single_line_fields = true + } f.expr(expr) + f.single_line_fields = old_single_line_fields } mut last_comment_was_inline := false mut has_comments := node.ecmnts[i].len > 0 diff --git a/vlib/v/fmt/struct.v b/vlib/v/fmt/struct.v index 01157db45..46ce4616b 100644 --- a/vlib/v/fmt/struct.v +++ b/vlib/v/fmt/struct.v @@ -367,7 +367,13 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) { if !single_line_fields { f.write(' '.repeat(value_align.max_len(init_field.pos.line_nr) - init_field.name.len)) } + if single_line_fields { + // Keep nested struct inits inline when the outer struct + // init is being formatted on a single line. + f.single_line_fields = true + } f.expr(init_field.expr) + f.single_line_fields = false if init_field.end_comments.len > 0 { f.write(' '.repeat(comment_align.max_len(init_field.pos.line_nr) - init_field.expr.str().len + 1)) diff --git a/vlib/v/fmt/tests/array_init_struct_inline_expected.vv b/vlib/v/fmt/tests/array_init_struct_inline_expected.vv new file mode 100644 index 000000000..cec3a2205 --- /dev/null +++ b/vlib/v/fmt/tests/array_init_struct_inline_expected.vv @@ -0,0 +1,11 @@ +struct Int { + val int +} + +fn test() { + for x in [Int{}, Int{5}, Int{ val: 6 }] { + println(x.val) + } + a := [Int{ val: 1 }, Int{ val: 2 }] + println(a) +} diff --git a/vlib/v/fmt/tests/array_init_struct_inline_input.vv b/vlib/v/fmt/tests/array_init_struct_inline_input.vv new file mode 100644 index 000000000..20cb1202d --- /dev/null +++ b/vlib/v/fmt/tests/array_init_struct_inline_input.vv @@ -0,0 +1,11 @@ +struct Int { + val int +} + +fn test() { + for x in [Int{}, Int{5}, Int{val: 6}] { + println(x.val) + } + a := [Int{val: 1}, Int{val: 2}] + println(a) +} diff --git a/vlib/v/tests/structs/struct_init_short_in_for_expr_test.v b/vlib/v/tests/structs/struct_init_short_in_for_expr_test.v index f8fcf612a..7b7f29973 100644 --- a/vlib/v/tests/structs/struct_init_short_in_for_expr_test.v +++ b/vlib/v/tests/structs/struct_init_short_in_for_expr_test.v @@ -28,9 +28,7 @@ fn test_short_struct_init_multi_fields_in_for_in_array() { fn test_mixed_struct_init_styles_in_for_in_array() { mut got := []int{} - for x in [Int{}, Int{5}, Int{ - val: 6 - }] { + for x in [Int{}, Int{5}, Int{ val: 6 }] { got << x.val } assert got == [0, 5, 6] -- 2.39.5