fmt: minor cleanup of or_expr (#8789)

pull/8799/head
Lukas Neubert 2021-02-17 05:43:29 +01:00 committed by GitHub
parent 0f8edd918a
commit 55b69a76a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 14 deletions

View File

@ -1028,30 +1028,28 @@ fn expr_is_single_line(expr ast.Expr) bool {
return true
}
pub fn (mut f Fmt) or_expr(or_block ast.OrExpr) {
match or_block.kind {
pub fn (mut f Fmt) or_expr(node ast.OrExpr) {
match node.kind {
.absent {}
.block {
if or_block.stmts.len == 0 {
if node.stmts.len == 0 {
f.write(' or { }')
} else if or_block.stmts.len == 1 {
return
} else if node.stmts.len == 1 {
// the control stmts (return/break/continue...) print a newline inside them,
// so, since this'll all be on one line, trim any possible whitespace
str := f.stmt_str(or_block.stmts[0]).trim_space()
str := f.stmt_str(node.stmts[0]).trim_space()
single_line := ' or { $str }'
if single_line.len + f.line_len <= fmt.max_len.last() {
f.write(single_line)
} else {
// if the line would be too long, make it multiline
f.writeln(' or {')
f.stmts(or_block.stmts)
f.write('}')
return
}
} else {
f.writeln(' or {')
f.stmts(or_block.stmts)
f.write('}')
}
// Make it multiline if the blocks has at least two stmts
// or a single line would be too long
f.writeln(' or {')
f.stmts(node.stmts)
f.write('}')
}
.propagate {
f.write(' ?')