vfmt: support `match a { x...y {} }`

pull/6757/head
Delyan Angelov 2020-11-05 18:35:14 +02:00
parent 35096cda3f
commit 4bf1c2fdcc
2 changed files with 23 additions and 1 deletions

View File

@ -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')

View File

@ -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
}