From 2c0fba5480f5de23784c62c59f7dfcd8bf09335f Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Fri, 25 Dec 2020 20:41:59 +0100 Subject: [PATCH] fmt: keep comment between enum fields (#7566) --- vlib/v/ast/ast.v | 11 ++++++----- vlib/v/fmt/fmt.v | 1 + vlib/v/fmt/tests/comments_keep.vv | 8 ++++++++ vlib/v/parser/parser.v | 3 ++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 6370e28205..22ecbf2eaf 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -760,11 +760,12 @@ pub mut: pub struct EnumField { pub: - name string - pos token.Position - comments []Comment - expr Expr - has_expr bool + name string + pos token.Position + comments []Comment + next_comments []Comment + expr Expr + has_expr bool } pub struct EnumDecl { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index a8b59423ba..7e43e289f5 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -739,6 +739,7 @@ pub fn (mut f Fmt) enum_decl(node ast.EnumDecl) { } f.comments(field.comments, inline: true, has_nl: false, level: .indent) f.writeln('') + f.comments(field.next_comments, inline: false, has_nl: true, level: .indent) } f.writeln('}\n') } diff --git a/vlib/v/fmt/tests/comments_keep.vv b/vlib/v/fmt/tests/comments_keep.vv index 9a648ffa99..ceb0db0c85 100644 --- a/vlib/v/fmt/tests/comments_keep.vv +++ b/vlib/v/fmt/tests/comments_keep.vv @@ -1,3 +1,11 @@ +enum Abc { + a + b // after a value + c + // between values + d +} + struct User { name string // name // middle comment diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index b7ae1b14a2..3abf7ff472 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1965,7 +1965,8 @@ fn (mut p Parser) enum_decl() ast.EnumDecl { pos: pos expr: expr has_expr: has_expr - comments: p.eat_comments() + comments: p.eat_line_end_comments() + next_comments: p.eat_comments() } } p.top_level_statement_end()