diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 2e64009048..b10b532937 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -697,6 +697,7 @@ pub: op token.Kind pos token.Position comments []Comment + end_comments []Comment pub mut: left []Expr left_types []table.Type diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 955b0effa4..def3780c2e 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -282,6 +282,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) { f.write(', ') } } + f.comments(node.end_comments, has_nl: false, inline: true, level: .keep) if !f.single_line_if { f.writeln('') } diff --git a/vlib/v/fmt/tests/comments_expected.vv b/vlib/v/fmt/tests/comments_expected.vv index b3df92b480..081ad60164 100644 --- a/vlib/v/fmt/tests/comments_expected.vv +++ b/vlib/v/fmt/tests/comments_expected.vv @@ -16,6 +16,8 @@ fn main() { // just to make it worse b, c := a, 2 d := c // and an extra one + e := c + // more comments = more good // before arg comment // after arg comment println('this is a test') diff --git a/vlib/v/fmt/tests/comments_input.vv b/vlib/v/fmt/tests/comments_input.vv index e45ea17b6a..b09b4a4cc4 100644 --- a/vlib/v/fmt/tests/comments_input.vv +++ b/vlib/v/fmt/tests/comments_input.vv @@ -10,6 +10,8 @@ fn main() { a := /* this is a comment */ 1 b, c := /* and another comment */ a, /* just to make it worse */ 2 d := c // and an extra one + e := c + // more comments = more good println(/* before arg comment */ 'this is a test' /* after arg comment */) if /* before if expr */ true /* after if expr */ { println('if') diff --git a/vlib/v/fmt/tests/comments_keep.vv b/vlib/v/fmt/tests/comments_keep.vv index 57b111bde0..6c7ccb4fd8 100644 --- a/vlib/v/fmt/tests/comments_keep.vv +++ b/vlib/v/fmt/tests/comments_keep.vv @@ -27,4 +27,9 @@ fn main() { _ := User{ // Just a comment } + a := 123 // comment after assign + b := 'foo' // also comment after assign + c := true + // between two assigns + d := false } diff --git a/vlib/v/parser/assign.v b/vlib/v/parser/assign.v index 05e3b7ba4c..52b0c1ffa9 100644 --- a/vlib/v/parser/assign.v +++ b/vlib/v/parser/assign.v @@ -94,6 +94,7 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme mut comments := []ast.Comment{cap: left_comments.len + right_comments.len} comments << left_comments comments << right_comments + end_comments := p.eat_line_end_comments() mut has_cross_var := false if op == .decl_assign { // a, b := a + 1, b @@ -187,6 +188,7 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme left: left right: right comments: comments + end_comments: end_comments pos: pos has_cross_var: has_cross_var is_simple: p.inside_for && p.tok.kind == .lcbr