vfmt: allow multiple comments after if branches

pull/5656/head
Delyan Angelov 2020-07-04 16:13:58 +03:00
parent 68af46402e
commit 5b93b4f37d
3 changed files with 13 additions and 14 deletions

View File

@ -436,10 +436,10 @@ pub mut:
pub struct IfBranch {
pub:
cond Expr
stmts []Stmt
pos token.Position
comment Comment
cond Expr
stmts []Stmt
pos token.Position
comments []Comment
}
pub struct LockExpr {

View File

@ -836,7 +836,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
f.write(node.val)
}
ast.LockExpr {
f.lock_expr(node)
f.lock_expr(node)
}
ast.MapInit {
if node.keys.len == 0 {
@ -1191,8 +1191,8 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
(it.is_expr || f.is_assign)
f.single_line_if = single_line
for i, branch in it.branches {
if branch.comment.text != '' {
f.comment(branch.comment)
if branch.comments.len > 0 {
f.comments(branch.comments, true, .keep)
}
if i == 0 {
f.write('if ')

View File

@ -16,17 +16,14 @@ fn (mut p Parser) if_expr() ast.IfExpr {
pos := p.tok.position()
mut branches := []ast.IfBranch{}
mut has_else := false
mut comments := []ast.Comment{}
for p.tok.kind in [.key_if, .key_else] {
p.inside_if = true
start_pos := p.tok.position()
mut comment := ast.Comment{}
if p.tok.kind == .key_if {
p.next()
} else {
// if p.tok.kind == .comment {
// p.error('place comments inside {}')
// }
comment = p.check_comment()
comments = p.eat_comments()
p.check(.key_else)
if p.tok.kind == .key_if {
p.next()
@ -37,8 +34,9 @@ fn (mut p Parser) if_expr() ast.IfExpr {
branches << ast.IfBranch{
stmts: p.parse_block()
pos: start_pos.extend(end_pos)
comment: comment
comments: comments
}
comments = []
break
}
}
@ -74,8 +72,9 @@ fn (mut p Parser) if_expr() ast.IfExpr {
cond: cond
stmts: stmts
pos: start_pos.extend(end_pos)
comment: comment
comments: comments
}
comments = []
if p.tok.kind != .key_else {
break
}