fmt: move else branch of match expr to the end (#9766)

pull/9777/head
zakuro 2021-04-17 14:28:33 +09:00 committed by GitHub
parent 0cc04850d7
commit 990c4ab17a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 41 deletions

View File

@ -2054,6 +2054,49 @@ pub fn (mut f Fmt) map_init(node ast.MapInit) {
f.write('}')
}
fn (mut f Fmt) match_branch(branch ast.MatchBranch, single_line bool) {
if !branch.is_else {
// normal branch
f.is_mbranch_expr = true
for j, expr in branch.exprs {
estr := f.node_str(expr)
if f.line_len + estr.len + 2 > fmt.max_len[5] {
f.remove_new_line({})
f.writeln('')
}
f.write(estr)
if j < branch.ecmnts.len && branch.ecmnts[j].len > 0 {
f.write(' ')
f.comments(branch.ecmnts[j], iembed: true)
}
if j < branch.exprs.len - 1 {
f.write(', ')
}
}
f.is_mbranch_expr = false
} else {
// else branch
f.write('else')
}
if branch.stmts.len == 0 {
f.writeln(' {}')
} else {
if single_line {
f.write(' { ')
} else {
f.writeln(' {')
}
f.stmts(branch.stmts)
if single_line {
f.remove_new_line({})
f.writeln(' }')
} else {
f.writeln('}')
}
}
f.comments(branch.post_comments, inline: true)
}
pub fn (mut f Fmt) match_expr(node ast.MatchExpr) {
f.write('match ')
f.expr(node.cond)
@ -2077,47 +2120,16 @@ pub fn (mut f Fmt) match_expr(node ast.MatchExpr) {
break
}
}
for branch in node.branches {
if !branch.is_else {
// normal branch
f.is_mbranch_expr = true
for j, expr in branch.exprs {
estr := f.node_str(expr)
if f.line_len + estr.len + 2 > fmt.max_len[5] {
f.remove_new_line({})
f.writeln('')
}
f.write(estr)
if j < branch.ecmnts.len && branch.ecmnts[j].len > 0 {
f.write(' ')
f.comments(branch.ecmnts[j], iembed: true)
}
if j < branch.exprs.len - 1 {
f.write(', ')
}
}
f.is_mbranch_expr = false
} else {
// else branch
f.write('else')
mut else_idx := -1
for i, branch in node.branches {
if branch.is_else {
else_idx = i
continue
}
if branch.stmts.len == 0 {
f.writeln(' {}')
} else {
if single_line {
f.write(' { ')
} else {
f.writeln(' {')
}
f.stmts(branch.stmts)
if single_line {
f.remove_new_line({})
f.writeln(' }')
} else {
f.writeln('}')
}
}
f.comments(branch.post_comments, inline: true)
f.match_branch(branch, single_line)
}
if else_idx >= 0 {
f.match_branch(node.branches[else_idx], single_line)
}
f.indent--
f.write('}')

View File

@ -2,8 +2,8 @@ fn match_expr_assignment() {
a := 20
_ := match a {
10 { 10 }
5 { 5 }
else { 2 }
5 { 5 }
}
}