fmt: improve match branch wrapping (#9619)

pull/9628/head
Lukas Neubert 2021-04-07 15:26:10 +02:00 committed by GitHub
parent 1d5ed89138
commit 3a07fbc653
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 18 deletions

View File

@ -1475,12 +1475,12 @@ pub fn (expr Expr) position() token.Position {
// println('compiler bug, unhandled EmptyExpr position()') // println('compiler bug, unhandled EmptyExpr position()')
return token.Position{} return token.Position{}
} }
NodeError, ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, CastExpr, NodeError, ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr,
ChanInit, CharLiteral, ConcatExpr, Comment, ComptimeCall, ComptimeSelector, EnumVal, DumpExpr, CastExpr, ChanInit, CharLiteral, ConcatExpr, Comment, ComptimeCall, ComptimeSelector,
FloatLiteral, GoExpr, Ident, IfExpr, IntegerLiteral, Likely, LockExpr, MapInit, MatchExpr, EnumVal, DumpExpr, FloatLiteral, GoExpr, Ident, IfExpr, IntegerLiteral, Likely, LockExpr,
None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr, SelectExpr, SelectorExpr, MapInit, MatchExpr, None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr,
SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit, TypeNode, TypeOf, UnsafeExpr SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit,
{ TypeNode, TypeOf, UnsafeExpr {
return expr.pos return expr.pos
} }
IndexExpr { IndexExpr {
@ -1617,8 +1617,7 @@ pub fn (node Node) position() token.Position {
StructField { StructField {
return node.pos.extend(node.type_pos) return node.pos.extend(node.type_pos)
} }
MatchBranch, SelectBranch, EnumField, ConstField, StructInitField, GlobalField, CallArg MatchBranch, SelectBranch, EnumField, ConstField, StructInitField, GlobalField, CallArg {
{
return node.pos return node.pos
} }
IfBranch { IfBranch {

View File

@ -3084,8 +3084,8 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
right.position()) 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() if !left_sym.is_int()
&& !c.table.get_final_type_symbol(left_type_unwrapped).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`', c.error('operator $assign_stmt.op.str() not defined on left operand type `$left_sym.name`',

View File

@ -2068,7 +2068,12 @@ pub fn (mut f Fmt) match_expr(node ast.MatchExpr) {
// normal branch // normal branch
f.is_mbranch_expr = true f.is_mbranch_expr = true
for j, expr in branch.exprs { 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 { if j < branch.ecmnts.len && branch.ecmnts[j].len > 0 {
f.write(' ') f.write(' ')
f.comments(branch.ecmnts[j], iembed: true) 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 { if j < branch.exprs.len - 1 {
f.write(', ') f.write(', ')
} }
f.wrap_long_line(4, false)
} }
f.is_mbranch_expr = false f.is_mbranch_expr = false
} else { } else {

View File

@ -24,11 +24,9 @@ fn match_branch_comment() {
fn really_long_branch_exprs() { fn really_long_branch_exprs() {
match x { match x {
ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr, CastExpr, ChanInit, CharLiteral, NodeError, ArrayDecompose, ArrayInit, AsCast, Assoc, AtExpr, BoolLiteral, CallExpr,
ConcatExpr, Comment, EnumVal, FloatLiteral, GoExpr, Ident, IfExpr, IndexExpr, IntegerLiteral, MapInit, MatchExpr, None, OffsetOf, OrExpr, ParExpr, PostfixExpr, PrefixExpr, RangeExpr,
Likely, LockExpr, MapInit, MatchExpr, None, OrExpr, ParExpr, PostfixExpr, PrefixExpr, SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral, StructInit {
RangeExpr, SelectExpr, SelectorExpr, SizeOf, SqlExpr, StringInterLiteral, StringLiteral,
StructInit, Type, TypeOf, UnsafeExpr {
return expr.pos return expr.pos
} }
InfixExpr { InfixExpr {

View File

@ -24,7 +24,7 @@ fn match_branch_comment() {
fn really_long_branch_exprs() { fn really_long_branch_exprs() {
match x { 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 return expr.pos
} }
InfixExpr { InfixExpr {