1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vfmt: allow multiple comments after if branches

This commit is contained in:
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 struct IfBranch {
pub: pub:
cond Expr cond Expr
stmts []Stmt stmts []Stmt
pos token.Position pos token.Position
comment Comment comments []Comment
} }
pub struct LockExpr { pub struct LockExpr {

View File

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

View File

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