From 7c9fb73b3f7880dbfc117a25e1b72ba6b663d82b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Mon, 4 Jan 2021 20:01:35 +0100 Subject: [PATCH] vfmt: fix comments before ...f in Abc{...f} (#7870) --- vlib/v/ast/ast.v | 19 ++++++++++--------- vlib/v/fmt/fmt.v | 11 ++++++----- .../v/fmt/tests/struct_update_comment_keep.vv | 18 ++++++++++++++++++ vlib/v/parser/struct.v | 3 +++ 4 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 vlib/v/fmt/tests/struct_update_comment_keep.vv diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 47f23209d1..52717d3ffb 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -247,16 +247,17 @@ pub mut: pub struct StructInit { pub: - pos token.Position - is_short bool + pos token.Position + is_short bool pub mut: - pre_comments []Comment - typ table.Type - update_expr Expr - update_expr_type table.Type - has_update_expr bool - fields []StructInitField - embeds []StructInitEmbed + pre_comments []Comment + typ table.Type + update_expr Expr + update_expr_type table.Type + update_expr_comments []Comment + has_update_expr bool + fields []StructInitField + embeds []StructInitEmbed } // import statement diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 3015af32c8..d1acc0fb8a 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2054,13 +2054,14 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) { } init_start := f.out.len f.indent++ - if it.has_update_expr { - f.write('...') - f.expr(it.update_expr) - f.writeln('') - } 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) + } for i, field in it.fields { f.write('$field.name: ') f.prefix_expr_cast_expr(field.expr) diff --git a/vlib/v/fmt/tests/struct_update_comment_keep.vv b/vlib/v/fmt/tests/struct_update_comment_keep.vv new file mode 100644 index 0000000000..1a257d8350 --- /dev/null +++ b/vlib/v/fmt/tests/struct_update_comment_keep.vv @@ -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' + } +} diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 0dff6345cb..26279c5e25 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -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