From 990c4ab17ad17563156ecca0acbe5d53ac0c0db3 Mon Sep 17 00:00:00 2001 From: zakuro Date: Sat, 17 Apr 2021 14:28:33 +0900 Subject: [PATCH] fmt: move else branch of match expr to the end (#9766) --- vlib/v/fmt/fmt.v | 92 +++++++++++++++++++-------------- vlib/v/fmt/tests/match_input.vv | 2 +- 2 files changed, 53 insertions(+), 41 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index ea464605e4..12f94cd582 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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('}') diff --git a/vlib/v/fmt/tests/match_input.vv b/vlib/v/fmt/tests/match_input.vv index 5b58d7cf59..3f7cbae675 100644 --- a/vlib/v/fmt/tests/match_input.vv +++ b/vlib/v/fmt/tests/match_input.vv @@ -2,8 +2,8 @@ fn match_expr_assignment() { a := 20 _ := match a { 10 { 10 } - 5 { 5 } else { 2 } + 5 { 5 } } }