diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 7b049e2d86..30c3bbedf2 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1460,7 +1460,8 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) { if cmt.is_inline { f.write(' ') f.comment(cmt, iembed: true) - if cmt.pos.line_nr == expr_pos.last_line && cmt.pos.pos < expr_pos.pos { + if !set_comma && cmt.pos.line_nr == expr_pos.last_line + && cmt.pos.pos < expr_pos.pos { f.write(',') set_comma = true } else { @@ -1471,9 +1472,12 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) { } } } else { - f.write(', ') + if !set_comma { + f.write(',') + set_comma = true + } + f.write(' ') f.comment(cmt, iembed: false) - set_comma = true } } last_comment_was_inline = cmt.is_inline diff --git a/vlib/v/fmt/tests/array_init_comments_expected.vv b/vlib/v/fmt/tests/array_init_comments_expected.vv new file mode 100644 index 0000000000..ac6a345de4 --- /dev/null +++ b/vlib/v/fmt/tests/array_init_comments_expected.vv @@ -0,0 +1,9 @@ +v := [ + 0, // 0 + 1, // 1 + 2, // 2 + // 2-3 + 3, // 3 + // 4 + // 5 +] diff --git a/vlib/v/fmt/tests/array_init_comments_input.vv b/vlib/v/fmt/tests/array_init_comments_input.vv new file mode 100644 index 0000000000..a2918d108e --- /dev/null +++ b/vlib/v/fmt/tests/array_init_comments_input.vv @@ -0,0 +1,9 @@ +v := [ + 0// 0 + 1 // 1 + 2,// 2 + // 2-3 + 3, // 3 + // 4 + /* 5 */ +]