From 3a07fbc65349ce7575c259016d4b7f1038d97fad Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Wed, 7 Apr 2021 15:26:10 +0200 Subject: [PATCH] fmt: improve match branch wrapping (#9619) --- vlib/v/ast/ast.v | 15 +++++++-------- vlib/v/checker/checker.v | 4 ++-- vlib/v/fmt/fmt.v | 8 ++++++-- vlib/v/fmt/tests/match_expected.vv | 8 +++----- vlib/v/fmt/tests/match_input.vv | 2 +- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 95623fa8be..aa02f3e897 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1475,12 +1475,12 @@ pub fn (expr Expr) position() token.Position { // println('compiler bug, unhandled EmptyExpr position()') return token.Position{} } - NodeError, ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, CastExpr, - ChanInit, CharLiteral, ConcatExpr, Comment, ComptimeCall, ComptimeSelector, EnumVal, DumpExpr, - FloatLiteral, GoExpr, Ident, IfExpr, IntegerLiteral, Likely, LockExpr, MapInit, MatchExpr, - None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, SelectExpr, SelectorExpr, - SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit, TypeNode, TypeOf, UnsafeExpr - { + NodeError, ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, + CastExpr, ChanInit, CharLiteral, ConcatExpr, Comment, ComptimeCall, ComptimeSelector, + EnumVal, DumpExpr, FloatLiteral, GoExpr, Ident, IfExpr, IntegerLiteral, Likely, LockExpr, + MapInit, MatchExpr, None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, + SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit, + TypeNode, TypeOf, UnsafeExpr { return expr.pos } IndexExpr { @@ -1617,8 +1617,7 @@ pub fn (node Node) position() token.Position { StructField { return node.pos.extend(node.type_pos) } - MatchBranch, SelectBranch, EnumField, ConstField, StructInitField, GlobalField, CallArg - { + MatchBranch, SelectBranch, EnumField, ConstField, StructInitField, GlobalField, CallArg { return node.pos } IfBranch { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index acc514a08b..10b52cda05 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3084,8 +3084,8 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { right.position()) } } - .and_assign, .or_assign, .xor_assign, .mod_assign, .left_shift_assign, .right_shift_assign - { + .and_assign, .or_assign, .xor_assign, .mod_assign, .left_shift_assign, + .right_shift_assign { if !left_sym.is_int() && !c.table.get_final_type_symbol(left_type_unwrapped).is_int() { c.error('operator $assign_stmt.op.str() not defined on left operand type `$left_sym.name`', diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 9fb10be620..6a2309af32 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2068,7 +2068,12 @@ pub fn (mut f Fmt) match_expr(node ast.MatchExpr) { // normal branch f.is_mbranch_expr = true for j, expr in branch.exprs { - f.expr(expr) + estr := f.node_str(expr) + if f.line_len + estr.len + 2 > fmt.max_len[5] { + f.remove_new_line({}) + f.writeln('') + } + f.write(estr) if j < branch.ecmnts.len && branch.ecmnts[j].len > 0 { f.write(' ') f.comments(branch.ecmnts[j], iembed: true) @@ -2076,7 +2081,6 @@ pub fn (mut f Fmt) match_expr(node ast.MatchExpr) { if j < branch.exprs.len - 1 { f.write(', ') } - f.wrap_long_line(4, false) } f.is_mbranch_expr = false } else { diff --git a/vlib/v/fmt/tests/match_expected.vv b/vlib/v/fmt/tests/match_expected.vv index 3a9de2ba19..556768f95c 100644 --- a/vlib/v/fmt/tests/match_expected.vv +++ b/vlib/v/fmt/tests/match_expected.vv @@ -24,11 +24,9 @@ fn match_branch_comment() { fn really_long_branch_exprs() { match x { - ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, CastExpr, ChanInit, CharLiteral, - ConcatExpr, Comment, EnumVal, FloatLiteral, GoExpr, Ident, IfExpr, IndexExpr, IntegerLiteral, - Likely, LockExpr, MapInit, MatchExpr, None, OrExpr, ParExpr, PostfixExpr, PrefixExpr, - RangeExpr, SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, - StructInit, Type, TypeOf, UnsafeExpr { + NodeError, ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, + MapInit, MatchExpr, None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, + SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit { return expr.pos } InfixExpr { diff --git a/vlib/v/fmt/tests/match_input.vv b/vlib/v/fmt/tests/match_input.vv index 5593ab34d6..5b58d7cf59 100644 --- a/vlib/v/fmt/tests/match_input.vv +++ b/vlib/v/fmt/tests/match_input.vv @@ -24,7 +24,7 @@ fn match_branch_comment() { fn really_long_branch_exprs() { match x { - ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, CastExpr, ChanInit, CharLiteral, ConcatExpr, Comment, EnumVal, FloatLiteral, GoExpr, Ident, IfExpr, IndexExpr, IntegerLiteral, Likely, LockExpr, MapInit, MatchExpr, None, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit, Type, TypeOf, UnsafeExpr { + NodeError, ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, MapInit, MatchExpr, None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit { return expr.pos } InfixExpr {