fmt: move else branch of match expr to the end (#9766)
parent
0cc04850d7
commit
990c4ab17a
|
@ -2054,30 +2054,7 @@ pub fn (mut f Fmt) map_init(node ast.MapInit) {
|
|||
f.write('}')
|
||||
}
|
||||
|
||||
pub fn (mut f Fmt) match_expr(node ast.MatchExpr) {
|
||||
f.write('match ')
|
||||
f.expr(node.cond)
|
||||
if node.cond is ast.Ident {
|
||||
f.it_name = node.cond.name
|
||||
}
|
||||
f.writeln(' {')
|
||||
f.indent++
|
||||
f.comments(node.comments, {})
|
||||
mut single_line := true
|
||||
for branch in node.branches {
|
||||
if branch.stmts.len > 1 || branch.pos.line_nr < branch.pos.last_line {
|
||||
single_line = false
|
||||
break
|
||||
}
|
||||
if branch.stmts.len == 0 {
|
||||
continue
|
||||
}
|
||||
if !stmt_is_single_line(branch.stmts[0]) {
|
||||
single_line = false
|
||||
break
|
||||
}
|
||||
}
|
||||
for branch in node.branches {
|
||||
fn (mut f Fmt) match_branch(branch ast.MatchBranch, single_line bool) {
|
||||
if !branch.is_else {
|
||||
// normal branch
|
||||
f.is_mbranch_expr = true
|
||||
|
@ -2118,6 +2095,41 @@ pub fn (mut f Fmt) match_expr(node ast.MatchExpr) {
|
|||
}
|
||||
}
|
||||
f.comments(branch.post_comments, inline: true)
|
||||
}
|
||||
|
||||
pub fn (mut f Fmt) match_expr(node ast.MatchExpr) {
|
||||
f.write('match ')
|
||||
f.expr(node.cond)
|
||||
if node.cond is ast.Ident {
|
||||
f.it_name = node.cond.name
|
||||
}
|
||||
f.writeln(' {')
|
||||
f.indent++
|
||||
f.comments(node.comments, {})
|
||||
mut single_line := true
|
||||
for branch in node.branches {
|
||||
if branch.stmts.len > 1 || branch.pos.line_nr < branch.pos.last_line {
|
||||
single_line = false
|
||||
break
|
||||
}
|
||||
if branch.stmts.len == 0 {
|
||||
continue
|
||||
}
|
||||
if !stmt_is_single_line(branch.stmts[0]) {
|
||||
single_line = false
|
||||
break
|
||||
}
|
||||
}
|
||||
mut else_idx := -1
|
||||
for i, branch in node.branches {
|
||||
if branch.is_else {
|
||||
else_idx = i
|
||||
continue
|
||||
}
|
||||
f.match_branch(branch, single_line)
|
||||
}
|
||||
if else_idx >= 0 {
|
||||
f.match_branch(node.branches[else_idx], single_line)
|
||||
}
|
||||
f.indent--
|
||||
f.write('}')
|
||||
|
|
|
@ -2,8 +2,8 @@ fn match_expr_assignment() {
|
|||
a := 20
|
||||
_ := match a {
|
||||
10 { 10 }
|
||||
5 { 5 }
|
||||
else { 2 }
|
||||
5 { 5 }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue