Skip to content

Commit

Permalink
ast, parser, fmt: fix formatting struct declaration with comments (fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jul 28, 2023
1 parent 2fa177e commit 2f2dde8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions vlib/v/ast/ast.v
Expand Up @@ -381,6 +381,7 @@ pub:
language Language
is_union bool
attrs []Attr
pre_comments []Comment
end_comments []Comment
embeds []Embed
pub mut:
Expand Down
11 changes: 8 additions & 3 deletions vlib/v/fmt/struct.v
Expand Up @@ -60,6 +60,9 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
}
}
f.writeln(' {')
if node.pre_comments.len > 0 {
f.comments_before_field(node.pre_comments)
}
for embed in node.embeds {
f.mark_types_import_as_used(embed.typ)
styp := f.table.type_to_str_using_aliases(embed.typ, f.mod2alias)
Expand Down Expand Up @@ -188,11 +191,13 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
f.writeln('')
}
}
f.comments_after_last_field(node.end_comments)
if is_anon {
if is_anon || node.end_comments.len > 0 {
f.write('}')
} else {
f.writeln('}\n')
f.writeln('}')
}
if node.end_comments.len > 0 {
f.comments(node.end_comments, inline: true)
}
}

Expand Down
13 changes: 13 additions & 0 deletions vlib/v/fmt/tests/struct_decl_with_comments_keep.vv
@@ -0,0 +1,13 @@
module main

fn main() {
}

pub struct OperateInfo { // implements IperateInfo
pub mut:
title string // title
// msg
msg string
// id
id string
} // operate info
2 changes: 1 addition & 1 deletion vlib/v/fmt/tests/structs_expected.vv
Expand Up @@ -37,8 +37,8 @@ fn new_user() User {
}

struct SomeStruct {
mut:
// 1
mut:
// 2
// 3
somefield /* 4 */ /* 5 */ int // 6
Expand Down
8 changes: 6 additions & 2 deletions vlib/v/parser/struct.v
Expand Up @@ -105,9 +105,11 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
mut is_field_pub := false
mut is_field_global := false
mut last_line := p.prev_tok.pos().line_nr + 1
mut pre_comments := []ast.Comment{}
mut end_comments := []ast.Comment{}
if !no_body {
p.check(.lcbr)
pre_comments = p.eat_comments()
mut i := 0
for p.tok.kind != .rcbr {
mut comments := []ast.Comment{}
Expand All @@ -118,7 +120,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
}
}
if p.tok.kind == .rcbr {
end_comments = comments.clone()
end_comments = p.eat_comments(same_line: true)
break
}
if p.tok.kind == .key_pub {
Expand Down Expand Up @@ -265,7 +267,6 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
}
}
// Comments after type (same line)
comments << p.eat_comments()
prev_attrs := p.attrs
p.attrs = []
if p.tok.kind == .lsbr {
Expand All @@ -279,6 +280,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
}
p.inside_struct_attr_decl = false
}
comments << p.eat_comments()
mut default_expr := ast.empty_expr
mut has_default_expr := false
if !is_embed {
Expand Down Expand Up @@ -338,6 +340,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
p.top_level_statement_end()
last_line = p.tok.line_nr
p.check(.rcbr)
end_comments = p.eat_comments(same_line: true)
}
is_minify := attrs.contains('minify')
mut sym := ast.TypeSymbol{
Expand Down Expand Up @@ -388,6 +391,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
language: language
is_union: is_union
attrs: if is_anon { []ast.Attr{} } else { attrs } // anon structs can't have attributes
pre_comments: pre_comments
end_comments: end_comments
generic_types: generic_types
embeds: embeds
Expand Down

0 comments on commit 2f2dde8

Please sign in to comment.