diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 47d7573272..6ff978bb06 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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) } diff --git a/vlib/v/fmt/tests/expressions_expected.vv b/vlib/v/fmt/tests/expressions_expected.vv index c5901bb8dc..1498e2e88f 100644 --- a/vlib/v/fmt/tests/expressions_expected.vv +++ b/vlib/v/fmt/tests/expressions_expected.vv @@ -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{} + } +} diff --git a/vlib/v/fmt/tests/expressions_input.vv b/vlib/v/fmt/tests/expressions_input.vv index 37b6b982c8..dec30ce43a 100644 --- a/vlib/v/fmt/tests/expressions_input.vv +++ b/vlib/v/fmt/tests/expressions_input.vv @@ -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{} } +}