From 2964855d1511583ae1bd9126cbf13f2e020aa964 Mon Sep 17 00:00:00 2001 From: Jose Mendoza <56417208+StunxFS@users.noreply.github.com> Date: Mon, 27 Nov 2023 10:00:05 -0400 Subject: [PATCH] vfmt: fix the formatting of comments after `const` declarations (#20005) --- vlib/v/fmt/fmt.v | 5 +++-- vlib/v/fmt/tests/consts_expected.vv | 6 ++++++ vlib/v/fmt/tests/consts_input.vv | 8 +++++++- vlib/v/parser/parser.v | 6 ++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 0089de38059e18..6994c3bce226ba 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -913,7 +913,7 @@ pub fn (mut f Fmt) const_decl(node ast.ConstDecl) { } else { ast.Node(ast.NodeError{}) } - for _, field in node.fields { + for fidx, field in node.fields { if field.comments.len > 0 { if f.should_insert_newline_before_node(ast.Expr(field.comments[0]), prev_field) { f.writeln('') @@ -936,7 +936,8 @@ pub fn (mut f Fmt) const_decl(node ast.ConstDecl) { f.write('= ') f.expr(field.expr) f.comments(field.end_comments, same_line: true) - if node.is_block && field.end_comments.len == 0 { + if node.is_block && fidx < node.fields.len - 1 && node.fields.len > 1 { + // old style grouped consts, converted to the new style ungrouped const f.writeln('') } else { // Write out single line comments after const expr if present diff --git a/vlib/v/fmt/tests/consts_expected.vv b/vlib/v/fmt/tests/consts_expected.vv index a28d6f45d1d7f9..e9a1d134166a73 100644 --- a/vlib/v/fmt/tests/consts_expected.vv +++ b/vlib/v/fmt/tests/consts_expected.vv @@ -25,6 +25,12 @@ const i_am_a_very_long_constant_name_so_i_stand_alone_and_my_length_is_over_90_c pub const i_am_pub_const = true +const abc = 1 // abc + +const def = 2 // def + +const ghi = 3 // ghi + fn main() { a := [ [3, 5, 6], diff --git a/vlib/v/fmt/tests/consts_input.vv b/vlib/v/fmt/tests/consts_input.vv index e16942ad532c25..5d64906fbf1a0d 100644 --- a/vlib/v/fmt/tests/consts_input.vv +++ b/vlib/v/fmt/tests/consts_input.vv @@ -17,7 +17,7 @@ another_const = ['a', 'b' ] multiline_const = [ 'first line', 'second line','third line', - 'fourth line'] + 'fourth line'] ) const ( @@ -28,6 +28,12 @@ pub const ( i_am_pub_const=true ) +const ( + abc = 1 // abc + def = 2 // def + ghi = 3 // ghi +) + fn main() { a := [ [3, diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 6a2fd2c259e355..393cae437f3ea4 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -3838,6 +3838,9 @@ fn (mut p Parser) const_decl() ast.ConstDecl { p.vet_notice('use a fixed array, instead of a dynamic one', pos.line_nr, vet.FixKind.unknown, .default) } + if is_block { + end_comments << p.eat_comments(same_line: true) + } field := ast.ConstField{ name: full_name mod: p.mod @@ -3851,6 +3854,9 @@ fn (mut p Parser) const_decl() ast.ConstDecl { fields << field p.table.global_scope.register(field) comments = [] + if is_block { + end_comments = [] + } if !is_block { break }