From 4bf1c2fdcc935dea53b9a363bb27ff6902a3ed36 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 5 Nov 2020 18:35:14 +0200 Subject: [PATCH] vfmt: support `match a { x...y {} }` --- vlib/v/fmt/fmt.v | 9 ++++++++- .../tests/match_range_expression_branches_keep.vv | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 vlib/v/fmt/tests/match_range_expression_branches_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index a9a45de479..e56f8675f9 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -46,6 +46,7 @@ pub mut: use_short_fn_args bool it_name string // the name to replace `it` with inside_lambda bool + is_mbranch_expr bool // math a { x...y { } } } pub fn fmt(file ast.File, table &table.Table, is_debug bool) string { @@ -896,7 +897,11 @@ pub fn (mut f Fmt) expr(node ast.Expr) { } ast.RangeExpr { f.expr(node.low) - f.write('..') + if f.is_mbranch_expr { + f.write('...') + } else { + f.write('..') + } f.expr(node.high) } ast.SelectExpr { @@ -1530,12 +1535,14 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) { } if !branch.is_else { // normal branch + f.is_mbranch_expr = true for j, expr in branch.exprs { f.expr(expr) if j < branch.exprs.len - 1 { f.write(', ') } } + f.is_mbranch_expr = false } else { // else branch f.write('else') diff --git a/vlib/v/fmt/tests/match_range_expression_branches_keep.vv b/vlib/v/fmt/tests/match_range_expression_branches_keep.vv new file mode 100644 index 0000000000..d557ddb0c8 --- /dev/null +++ b/vlib/v/fmt/tests/match_range_expression_branches_keep.vv @@ -0,0 +1,15 @@ +pub fn (b byte) str_escaped() string { + str := match b { + 0 { '`\\' + '0`' } // Bug is preventing \\0 in a literal + 7 { '`\\a`' } + 8 { '`\\b`' } + 9 { '`\\t`' } + 10 { '`\\n`' } + 11 { '`\\v`' } + 12 { '`\\f`' } + 13 { '`\\r`' } + 32...126 { b.str() } + else { '0x' + b.hex() } + } + return str +}