diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 9ef18e2887..5432870929 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -14,7 +14,7 @@ pub type Expr = AnonFn | ArrayInit | AsCast | Assoc | BoolLiteral | CallExpr | C IfGuardExpr | IndexExpr | InfixExpr | IntegerLiteral | Likely | LockExpr | MapInit | MatchExpr | None | OrExpr | ParExpr | PostfixExpr | PrefixExpr | RangeExpr | SelectorExpr | SizeOf | SqlExpr | StringInterLiteral | StringLiteral | StructInit | Type | TypeOf | UnsafeExpr - + pub type Stmt = AssertStmt | AssignStmt | Block | BranchStmt | CompFor | CompIf | ConstDecl | DeferStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | ForStmt | GlobalDecl | @@ -162,6 +162,7 @@ pub: pos token.Position pub mut: fields []ConstField + end_comments []Comment } pub struct StructDecl { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index d024083ca5..d4f0b67486 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -677,8 +677,13 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { } f.write('\n') } + f.comments_after_last_field(node.end_comments) + f.writeln('}\n') +} + +pub fn (mut f Fmt) comments_after_last_field(comments []ast.Comment) { // Handle comments after last field - for comment in node.end_comments { + for comment in comments { f.indent++ f.empty_line = true f.comment(comment, { @@ -687,7 +692,6 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { f.writeln('') f.indent-- } - f.writeln('}\n') } pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) { @@ -1814,6 +1818,7 @@ pub fn (mut f Fmt) const_decl(it ast.ConstDecl) { f.expr(field.expr) f.writeln('') } + f.comments_after_last_field(it.end_comments) f.indent-- f.writeln(')\n') } diff --git a/vlib/v/fmt/tests/consts_with_comments_keep.vv b/vlib/v/fmt/tests/consts_with_comments_keep.vv new file mode 100644 index 0000000000..2f8a2218e8 --- /dev/null +++ b/vlib/v/fmt/tests/consts_with_comments_keep.vv @@ -0,0 +1,4 @@ +const ( + fsm_state_array = ['init', 'state_a', 'state_b', 'state_c', 'exit'] // use as a first half key for map see fsm_state_ev_fn, the same order as in enum FSM_state + fsm_event_array = ['ev1', 'ev2', 'ev3', 'ev4', 'ev5'] // use as a second half key for map see fsm_state_ev_fn, the same order as in enum FSM_event +) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 99008a78f5..ae3d086d31 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1602,13 +1602,11 @@ fn (mut p Parser) const_decl() ast.ConstDecl { } p.next() // ( mut fields := []ast.ConstField{} - for p.tok.kind != .rpar { - mut comments := []ast.Comment{} - for p.tok.kind == .comment { - comments << p.comment() - if p.tok.kind == .rpar { - break - } + mut comments := []ast.Comment{} + for { + comments = p.eat_comments() + if p.tok.kind == .rpar { + break } pos := p.tok.position() name := p.check_name() @@ -1630,6 +1628,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl { } fields << field p.global_scope.register(field.name, field) + comments = [] } p.top_level_statement_end() p.check(.rpar) @@ -1637,6 +1636,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl { pos: start_pos.extend(end_pos) fields: fields is_pub: is_pub + end_comments: comments } }