From d474f6901d952e20f6baedc68bb7754a6dc8eab3 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 19 Dec 2023 15:29:11 +0800 Subject: [PATCH] parser: fix formatting enum and interface decl with comments (#20216) --- vlib/v/fmt/tests/enum_decl_with_comment_expected.vv | 4 ++++ vlib/v/fmt/tests/enum_decl_with_comment_input.vv | 5 +++++ vlib/v/fmt/tests/interface_decl_with_comment_expected.vv | 4 ++++ vlib/v/fmt/tests/interface_decl_with_comment_input.vv | 5 +++++ vlib/v/parser/parser.v | 3 ++- vlib/v/parser/struct.v | 4 ++-- 6 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 vlib/v/fmt/tests/enum_decl_with_comment_expected.vv create mode 100644 vlib/v/fmt/tests/enum_decl_with_comment_input.vv create mode 100644 vlib/v/fmt/tests/interface_decl_with_comment_expected.vv create mode 100644 vlib/v/fmt/tests/interface_decl_with_comment_input.vv diff --git a/vlib/v/fmt/tests/enum_decl_with_comment_expected.vv b/vlib/v/fmt/tests/enum_decl_with_comment_expected.vv new file mode 100644 index 000000000..a00e34a3a --- /dev/null +++ b/vlib/v/fmt/tests/enum_decl_with_comment_expected.vv @@ -0,0 +1,4 @@ +enum ABC { // enum ABC + // aaa + aaa +} diff --git a/vlib/v/fmt/tests/enum_decl_with_comment_input.vv b/vlib/v/fmt/tests/enum_decl_with_comment_input.vv new file mode 100644 index 000000000..abf424cdf --- /dev/null +++ b/vlib/v/fmt/tests/enum_decl_with_comment_input.vv @@ -0,0 +1,5 @@ +enum ABC // enum ABC +{ + // aaa + aaa +} diff --git a/vlib/v/fmt/tests/interface_decl_with_comment_expected.vv b/vlib/v/fmt/tests/interface_decl_with_comment_expected.vv new file mode 100644 index 000000000..0d0717a14 --- /dev/null +++ b/vlib/v/fmt/tests/interface_decl_with_comment_expected.vv @@ -0,0 +1,4 @@ +interface Abc { // interface Abc + a int + get_info() string +} diff --git a/vlib/v/fmt/tests/interface_decl_with_comment_input.vv b/vlib/v/fmt/tests/interface_decl_with_comment_input.vv new file mode 100644 index 000000000..5d2e58be2 --- /dev/null +++ b/vlib/v/fmt/tests/interface_decl_with_comment_input.vv @@ -0,0 +1,5 @@ +interface Abc // interface Abc +{ + a int + get_info() string +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 71384ffd7..cc907b451 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -4059,8 +4059,9 @@ fn (mut p Parser) enum_decl() ast.EnumDecl { typ_pos = p.tok.pos() enum_type = p.parse_type() } + mut enum_decl_comments := p.eat_comments() p.check(.lcbr) - enum_decl_comments := p.eat_comments() + enum_decl_comments << p.eat_comments() senum_type := p.table.get_type_name(enum_type) mut vals := []string{} // mut default_exprs := []ast.Expr{} diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 999312c3f..9748de70f 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -533,9 +533,9 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { interface_name = p.prepend_mod(modless_name) } generic_types, _ := p.parse_generic_types() - // println('interface decl $interface_name') + mut pre_comments := p.eat_comments() p.check(.lcbr) - pre_comments := p.eat_comments() + pre_comments << p.eat_comments() if modless_name in p.imported_symbols { p.error_with_pos('cannot register interface `${interface_name}`, this type was already imported', name_pos) -- 2.39.5