vfmt: fix comments before ...f in Abc{...f} (#7870)
parent
bf904c2f82
commit
7c9fb73b3f
|
@ -247,16 +247,17 @@ pub mut:
|
||||||
|
|
||||||
pub struct StructInit {
|
pub struct StructInit {
|
||||||
pub:
|
pub:
|
||||||
pos token.Position
|
pos token.Position
|
||||||
is_short bool
|
is_short bool
|
||||||
pub mut:
|
pub mut:
|
||||||
pre_comments []Comment
|
pre_comments []Comment
|
||||||
typ table.Type
|
typ table.Type
|
||||||
update_expr Expr
|
update_expr Expr
|
||||||
update_expr_type table.Type
|
update_expr_type table.Type
|
||||||
has_update_expr bool
|
update_expr_comments []Comment
|
||||||
fields []StructInitField
|
has_update_expr bool
|
||||||
embeds []StructInitEmbed
|
fields []StructInitField
|
||||||
|
embeds []StructInitEmbed
|
||||||
}
|
}
|
||||||
|
|
||||||
// import statement
|
// import statement
|
||||||
|
|
|
@ -2054,13 +2054,14 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
|
||||||
}
|
}
|
||||||
init_start := f.out.len
|
init_start := f.out.len
|
||||||
f.indent++
|
f.indent++
|
||||||
if it.has_update_expr {
|
|
||||||
f.write('...')
|
|
||||||
f.expr(it.update_expr)
|
|
||||||
f.writeln('')
|
|
||||||
}
|
|
||||||
short_args_loop: for {
|
short_args_loop: for {
|
||||||
f.comments(it.pre_comments, inline: true, has_nl: true, level: .keep)
|
f.comments(it.pre_comments, inline: true, has_nl: true, level: .keep)
|
||||||
|
if it.has_update_expr {
|
||||||
|
f.write('...')
|
||||||
|
f.expr(it.update_expr)
|
||||||
|
f.writeln('')
|
||||||
|
f.comments(it.update_expr_comments, inline: true, has_nl: true, level: .keep)
|
||||||
|
}
|
||||||
for i, field in it.fields {
|
for i, field in it.fields {
|
||||||
f.write('$field.name: ')
|
f.write('$field.name: ')
|
||||||
f.prefix_expr_cast_expr(field.expr)
|
f.prefix_expr_cast_expr(field.expr)
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
struct Foo {
|
||||||
|
name string
|
||||||
|
age int
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Foo2 {}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
f := Foo{
|
||||||
|
name: 'test'
|
||||||
|
age: 18
|
||||||
|
}
|
||||||
|
f2 := Foo{
|
||||||
|
// before
|
||||||
|
...f // after
|
||||||
|
name: 'f2'
|
||||||
|
}
|
||||||
|
}
|
|
@ -336,6 +336,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
|
||||||
saved_is_amp := p.is_amp
|
saved_is_amp := p.is_amp
|
||||||
p.is_amp = false
|
p.is_amp = false
|
||||||
mut update_expr := ast.Expr{}
|
mut update_expr := ast.Expr{}
|
||||||
|
mut update_expr_comments := []ast.Comment{}
|
||||||
mut has_update_expr := false
|
mut has_update_expr := false
|
||||||
for p.tok.kind !in [.rcbr, .rpar, .eof] {
|
for p.tok.kind !in [.rcbr, .rpar, .eof] {
|
||||||
mut field_name := ''
|
mut field_name := ''
|
||||||
|
@ -353,6 +354,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
|
||||||
// struct updating syntax; f2 := Foo{ ...f, name: 'f2' }
|
// struct updating syntax; f2 := Foo{ ...f, name: 'f2' }
|
||||||
p.check(.ellipsis)
|
p.check(.ellipsis)
|
||||||
update_expr = p.expr(0)
|
update_expr = p.expr(0)
|
||||||
|
update_expr_comments << p.eat_line_end_comments()
|
||||||
has_update_expr = true
|
has_update_expr = true
|
||||||
} else {
|
} else {
|
||||||
first_field_pos := p.tok.position()
|
first_field_pos := p.tok.position()
|
||||||
|
@ -394,6 +396,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
|
||||||
typ: typ
|
typ: typ
|
||||||
fields: fields
|
fields: fields
|
||||||
update_expr: update_expr
|
update_expr: update_expr
|
||||||
|
update_expr_comments: update_expr_comments
|
||||||
has_update_expr: has_update_expr
|
has_update_expr: has_update_expr
|
||||||
pos: token.Position{
|
pos: token.Position{
|
||||||
line_nr: first_pos.line_nr
|
line_nr: first_pos.line_nr
|
||||||
|
|
Loading…
Reference in New Issue