1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fmt: proper single line check for ConcatExpr (#9121)

This commit is contained in:
Lukas Neubert 2021-03-05 13:36:49 +01:00 committed by GitHub
parent cbbfb460a7
commit ead2ba6004
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 7 deletions

View File

@ -1070,6 +1070,13 @@ fn expr_is_single_line(expr ast.Expr) bool {
return expr_is_single_line(expr.exprs[0])
}
}
ast.ConcatExpr {
for e in expr.vals {
if !expr_is_single_line(e) {
return false
}
}
}
else {}
}
return true
@ -1698,6 +1705,13 @@ pub fn (mut f Fmt) wrap_infix(start_pos int, start_len int, ignore_paren bool) {
}
}
fn branch_is_single_line(b ast.IfBranch) bool {
if b.stmts.len == 1 && b.comments.len == 0 && stmt_is_single_line(b.stmts[0]) {
return true
}
return false
}
pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
dollar := if node.is_comptime { '$' } else { '' }
mut is_ternary := node.branches.len == 2 && node.has_else
@ -1769,13 +1783,6 @@ pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
}
}
fn branch_is_single_line(b ast.IfBranch) bool {
if b.stmts.len == 1 && b.comments.len == 0 && stmt_is_single_line(b.stmts[0]) {
return true
}
return false
}
pub fn (mut f Fmt) at_expr(node ast.AtExpr) {
f.write(node.name)
}

View File

@ -80,3 +80,24 @@ fn (mut p Parser) name_expr() {
fn set_nr_muls(t table.Type, nr_muls int) table.Type {
return int(t) & 0xff00ffff | (nr_muls << 16)
}
// Test what exprs are treated as multiline. The ternary if only functions as a wrapper.
// When one expr in a branch doesn't fit a single line, the whole if will be unwrapped.
fn multiline_exprs() {
// StructInit with at least one field
_ := if true {
Foo{}
} else {
Foo{
val: 123
}
}
// ConcatExpr with a multiline child expr
_, _ := if true {
1, Foo{
val: 123
}
} else {
2, Foo{}
}
}

View File

@ -91,3 +91,12 @@ fn set_nr_muls(t table.Type, nr_muls int) table.Type {
return int(t) &
0xff00ffff | (nr_muls << 16)
}
// Test what exprs are treated as multiline. The ternary if only functions as a wrapper.
// When one expr in a branch doesn't fit a single line, the whole if will be unwrapped.
fn multiline_exprs() {
// StructInit with at least one field
_ := if true { Foo{} } else { Foo{ val: 123} }
// ConcatExpr with a multiline child expr
_, _ := if true { 1, Foo{val: 123} } else { 2, Foo{} }
}