vfmt: properly parse comments on lines after init fields (#7202)
parent
a9c71a89cf
commit
3b6b5b8090
|
@ -207,6 +207,7 @@ pub:
|
||||||
expr Expr
|
expr Expr
|
||||||
pos token.Position
|
pos token.Position
|
||||||
comments []Comment
|
comments []Comment
|
||||||
|
next_comments []Comment
|
||||||
pub mut:
|
pub mut:
|
||||||
name string
|
name string
|
||||||
typ table.Type
|
typ table.Type
|
||||||
|
|
|
@ -1866,6 +1866,7 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
|
||||||
} else {
|
} else {
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
|
f.comments(field.next_comments, inline: false, has_nl: true, level: .keep)
|
||||||
}
|
}
|
||||||
f.indent--
|
f.indent--
|
||||||
if !use_short_args {
|
if !use_short_args {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
struct User {
|
struct User {
|
||||||
name string // name
|
name string // name
|
||||||
|
// middle comment
|
||||||
|
age int
|
||||||
// last comment
|
// last comment
|
||||||
// last comment2
|
// last comment2
|
||||||
}
|
}
|
||||||
|
@ -15,4 +17,11 @@ fn main() {
|
||||||
// else
|
// else
|
||||||
// else {
|
// else {
|
||||||
// }
|
// }
|
||||||
|
_ := User{
|
||||||
|
name: 'Henry' // comment after name
|
||||||
|
// on the next line
|
||||||
|
age: 42
|
||||||
|
// after age line
|
||||||
|
// after line2
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -584,7 +584,7 @@ pub fn (mut p Parser) eat_comments() []ast.Comment {
|
||||||
return comments
|
return comments
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut p Parser) eat_lineend_comments() []ast.Comment {
|
pub fn (mut p Parser) eat_line_end_comments() []ast.Comment {
|
||||||
mut comments := []ast.Comment{}
|
mut comments := []ast.Comment{}
|
||||||
for {
|
for {
|
||||||
if p.tok.kind != .comment || p.tok.line_nr != p.prev_tok.line_nr {
|
if p.tok.kind != .comment || p.tok.line_nr != p.prev_tok.line_nr {
|
||||||
|
@ -2002,7 +2002,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
|
||||||
// function type: `type mycallback fn(string, int)`
|
// function type: `type mycallback fn(string, int)`
|
||||||
fn_name := p.prepend_mod(name)
|
fn_name := p.prepend_mod(name)
|
||||||
fn_type := p.parse_fn_type(fn_name)
|
fn_type := p.parse_fn_type(fn_name)
|
||||||
comments = p.eat_lineend_comments()
|
comments = p.eat_line_end_comments()
|
||||||
return ast.FnTypeDecl{
|
return ast.FnTypeDecl{
|
||||||
name: fn_name
|
name: fn_name
|
||||||
is_pub: is_pub
|
is_pub: is_pub
|
||||||
|
@ -2049,7 +2049,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
|
||||||
}
|
}
|
||||||
is_public: is_pub
|
is_public: is_pub
|
||||||
})
|
})
|
||||||
comments = p.eat_lineend_comments()
|
comments = p.eat_line_end_comments()
|
||||||
return ast.SumTypeDecl{
|
return ast.SumTypeDecl{
|
||||||
name: name
|
name: name
|
||||||
is_pub: is_pub
|
is_pub: is_pub
|
||||||
|
@ -2083,7 +2083,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
|
||||||
}
|
}
|
||||||
is_public: is_pub
|
is_public: is_pub
|
||||||
})
|
})
|
||||||
comments = p.eat_lineend_comments()
|
comments = p.eat_line_end_comments()
|
||||||
return ast.AliasTypeDecl{
|
return ast.AliasTypeDecl{
|
||||||
name: name
|
name: name
|
||||||
is_pub: is_pub
|
is_pub: is_pub
|
||||||
|
|
|
@ -324,17 +324,18 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
|
||||||
mut expr := ast.Expr{}
|
mut expr := ast.Expr{}
|
||||||
mut field_pos := token.Position{}
|
mut field_pos := token.Position{}
|
||||||
mut comments := []ast.Comment{}
|
mut comments := []ast.Comment{}
|
||||||
|
mut nline_comments := []ast.Comment{}
|
||||||
if no_keys {
|
if no_keys {
|
||||||
// name will be set later in checker
|
// name will be set later in checker
|
||||||
expr = p.expr(0)
|
expr = p.expr(0)
|
||||||
field_pos = expr.position()
|
field_pos = expr.position()
|
||||||
comments = p.eat_comments()
|
comments = p.eat_line_end_comments()
|
||||||
} else {
|
} else {
|
||||||
first_field_pos := p.tok.position()
|
first_field_pos := p.tok.position()
|
||||||
field_name = p.check_name()
|
field_name = p.check_name()
|
||||||
p.check(.colon)
|
p.check(.colon)
|
||||||
expr = p.expr(0)
|
expr = p.expr(0)
|
||||||
comments = p.eat_comments()
|
comments = p.eat_line_end_comments()
|
||||||
last_field_pos := expr.position()
|
last_field_pos := expr.position()
|
||||||
field_pos = token.Position{
|
field_pos = token.Position{
|
||||||
line_nr: first_field_pos.line_nr
|
line_nr: first_field_pos.line_nr
|
||||||
|
@ -346,12 +347,14 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
|
||||||
if p.tok.kind == .comma {
|
if p.tok.kind == .comma {
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
comments << p.eat_comments()
|
comments << p.eat_line_end_comments()
|
||||||
|
nline_comments << p.eat_comments()
|
||||||
fields << ast.StructInitField{
|
fields << ast.StructInitField{
|
||||||
name: field_name
|
name: field_name
|
||||||
expr: expr
|
expr: expr
|
||||||
pos: field_pos
|
pos: field_pos
|
||||||
comments: comments
|
comments: comments
|
||||||
|
next_comments: nline_comments
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
last_pos := p.tok.position()
|
last_pos := p.tok.position()
|
||||||
|
|
Loading…
Reference in New Issue