fmt: keep comments after assign on same line (#7269)

pull/7274/head
Lukas Neubert 2020-12-11 18:20:24 +01:00 committed by GitHub
parent 2a731e1c8e
commit 25153490e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 0 deletions

View File

@ -697,6 +697,7 @@ pub:
op token.Kind
pos token.Position
comments []Comment
end_comments []Comment
pub mut:
left []Expr
left_types []table.Type

View File

@ -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('')
}

View File

@ -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')

View File

@ -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')

View File

@ -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
}

View File

@ -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