fmt: fix removal of comment before embed in struct (#11384)
parent
91f7be98d0
commit
19a43db2de
|
@ -57,11 +57,16 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
|
||||||
for embed in node.embeds {
|
for embed in node.embeds {
|
||||||
f.mark_types_import_as_used(embed.typ)
|
f.mark_types_import_as_used(embed.typ)
|
||||||
styp := f.table.type_to_str_using_aliases(embed.typ, f.mod2alias)
|
styp := f.table.type_to_str_using_aliases(embed.typ, f.mod2alias)
|
||||||
if embed.comments.len == 0 {
|
|
||||||
|
pre_comments := embed.comments.filter(it.pos.pos < embed.pos.pos)
|
||||||
|
comments := embed.comments[pre_comments.len..]
|
||||||
|
|
||||||
|
f.comments_before_field(pre_comments)
|
||||||
|
if comments.len == 0 {
|
||||||
f.writeln('\t$styp')
|
f.writeln('\t$styp')
|
||||||
} else {
|
} else {
|
||||||
f.write('\t$styp')
|
f.write('\t$styp')
|
||||||
f.comments(embed.comments, level: .indent)
|
f.comments(comments, level: .indent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mut field_align_i := 0
|
mut field_align_i := 0
|
||||||
|
|
|
@ -5,7 +5,9 @@ struct Foo {
|
||||||
struct Test {}
|
struct Test {}
|
||||||
|
|
||||||
struct Bar {
|
struct Bar {
|
||||||
|
// comment before Fooo
|
||||||
Foo // comment for Foo
|
Foo // comment for Foo
|
||||||
|
// comment before Test
|
||||||
Test // comment for Test
|
Test // comment for Test
|
||||||
// another comment for Test
|
// another comment for Test
|
||||||
y int
|
y int
|
||||||
|
|
|
@ -181,7 +181,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||||
// struct embedding
|
// struct embedding
|
||||||
type_pos = p.tok.position()
|
type_pos = p.tok.position()
|
||||||
typ = p.parse_type()
|
typ = p.parse_type()
|
||||||
ecomments := p.eat_comments()
|
comments << p.eat_comments()
|
||||||
type_pos = type_pos.extend(p.prev_tok.position())
|
type_pos = type_pos.extend(p.prev_tok.position())
|
||||||
if !is_on_top {
|
if !is_on_top {
|
||||||
p.error_with_pos('struct embedding must be declared at the beginning of the struct body',
|
p.error_with_pos('struct embedding must be declared at the beginning of the struct body',
|
||||||
|
@ -203,7 +203,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||||
embeds << ast.Embed{
|
embeds << ast.Embed{
|
||||||
typ: typ
|
typ: typ
|
||||||
pos: type_pos
|
pos: type_pos
|
||||||
comments: ecomments
|
comments: comments
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// struct field
|
// struct field
|
||||||
|
|
Loading…
Reference in New Issue