fmt: proper single line check for ConcatExpr (#9121)
parent
cbbfb460a7
commit
ead2ba6004
|
@ -1070,6 +1070,13 @@ fn expr_is_single_line(expr ast.Expr) bool {
|
||||||
return expr_is_single_line(expr.exprs[0])
|
return expr_is_single_line(expr.exprs[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ast.ConcatExpr {
|
||||||
|
for e in expr.vals {
|
||||||
|
if !expr_is_single_line(e) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
return true
|
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) {
|
pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
|
||||||
dollar := if node.is_comptime { '$' } else { '' }
|
dollar := if node.is_comptime { '$' } else { '' }
|
||||||
mut is_ternary := node.branches.len == 2 && node.has_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) {
|
pub fn (mut f Fmt) at_expr(node ast.AtExpr) {
|
||||||
f.write(node.name)
|
f.write(node.name)
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,3 +80,24 @@ fn (mut p Parser) name_expr() {
|
||||||
fn set_nr_muls(t table.Type, nr_muls int) table.Type {
|
fn set_nr_muls(t table.Type, nr_muls int) table.Type {
|
||||||
return int(t) & 0xff00ffff | (nr_muls << 16)
|
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{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -91,3 +91,12 @@ fn set_nr_muls(t table.Type, nr_muls int) table.Type {
|
||||||
return int(t) &
|
return int(t) &
|
||||||
0xff00ffff | (nr_muls << 16)
|
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{} }
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue