mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vfmt: fix comments before ...f in Abc{...f} (#7870)
This commit is contained in:
parent
bf904c2f82
commit
7c9fb73b3f
@ -254,6 +254,7 @@ pub mut:
|
||||
typ table.Type
|
||||
update_expr Expr
|
||||
update_expr_type table.Type
|
||||
update_expr_comments []Comment
|
||||
has_update_expr bool
|
||||
fields []StructInitField
|
||||
embeds []StructInitEmbed
|
||||
|
@ -2054,13 +2054,14 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
|
||||
}
|
||||
init_start := f.out.len
|
||||
f.indent++
|
||||
short_args_loop: for {
|
||||
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)
|
||||
}
|
||||
short_args_loop: for {
|
||||
f.comments(it.pre_comments, inline: true, has_nl: true, level: .keep)
|
||||
for i, field in it.fields {
|
||||
f.write('$field.name: ')
|
||||
f.prefix_expr_cast_expr(field.expr)
|
||||
|
18
vlib/v/fmt/tests/struct_update_comment_keep.vv
Normal file
18
vlib/v/fmt/tests/struct_update_comment_keep.vv
Normal file
@ -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
|
||||
p.is_amp = false
|
||||
mut update_expr := ast.Expr{}
|
||||
mut update_expr_comments := []ast.Comment{}
|
||||
mut has_update_expr := false
|
||||
for p.tok.kind !in [.rcbr, .rpar, .eof] {
|
||||
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' }
|
||||
p.check(.ellipsis)
|
||||
update_expr = p.expr(0)
|
||||
update_expr_comments << p.eat_line_end_comments()
|
||||
has_update_expr = true
|
||||
} else {
|
||||
first_field_pos := p.tok.position()
|
||||
@ -394,6 +396,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
|
||||
typ: typ
|
||||
fields: fields
|
||||
update_expr: update_expr
|
||||
update_expr_comments: update_expr_comments
|
||||
has_update_expr: has_update_expr
|
||||
pos: token.Position{
|
||||
line_nr: first_pos.line_nr
|
||||
|
Loading…
Reference in New Issue
Block a user