fmt: wrap match branch expressions (#8190)

pull/8197/head
Lukas Neubert 2021-01-19 06:16:42 +01:00 committed by GitHub
parent 4779a273bf
commit b44c08aff0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 6 deletions

View File

@ -1187,7 +1187,11 @@ pub fn (expr Expr) position() token.Position {
AnonFn {
return expr.decl.pos
}
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 {
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 {
return expr.pos
}
ArrayDecompose {
@ -1272,7 +1276,9 @@ pub:
pub fn (stmt Stmt) position() token.Position {
match stmt {
AssertStmt, AssignStmt, Block, BranchStmt, CompFor, ConstDecl, DeferStmt, EnumDecl, ExprStmt, FnDecl, ForCStmt, ForInStmt, ForStmt, GotoLabel, GotoStmt, Import, Return, StructDecl, GlobalDecl, HashStmt, InterfaceDecl, Module, SqlStmt {
AssertStmt, AssignStmt, Block, BranchStmt, CompFor, ConstDecl, DeferStmt, EnumDecl, ExprStmt,
FnDecl, ForCStmt, ForInStmt, ForStmt, GotoLabel, GotoStmt, Import, Return, StructDecl,
GlobalDecl, HashStmt, InterfaceDecl, Module, SqlStmt {
return stmt.pos
}
GoStmt {
@ -1306,7 +1312,8 @@ pub fn (node Node) position() token.Position {
StructField {
return node.pos.extend(node.type_pos)
}
MatchBranch, SelectBranch, Field, EnumField, ConstField, StructInitField, GlobalField, table.Param {
MatchBranch, SelectBranch, Field, EnumField, ConstField, StructInitField, GlobalField,
table.Param {
return node.pos
}
IfBranch {
@ -1336,7 +1343,8 @@ pub fn (node Node) children() []Node {
StringInterLiteral, Assoc, ArrayInit {
return node.exprs.map(Node(it))
}
SelectorExpr, PostfixExpr, UnsafeExpr, AsCast, ParExpr, IfGuardExpr, SizeOf, Likely, TypeOf, ArrayDecompose {
SelectorExpr, PostfixExpr, UnsafeExpr, AsCast, ParExpr, IfGuardExpr, SizeOf, Likely,
TypeOf, ArrayDecompose {
children << node.expr
}
LockExpr, OrExpr {

View File

@ -2580,7 +2580,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()
{

View File

@ -1773,6 +1773,7 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) {
if j < branch.exprs.len - 1 {
f.write(', ')
}
f.wrap_long_line(3, false)
}
f.is_mbranch_expr = false
} else {

View File

@ -30,3 +30,20 @@ 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 {
return expr.pos
}
InfixExpr {
Foo{
x: 3
}
}
}
}

View File

@ -34,3 +34,16 @@ 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 {
return expr.pos
}
InfixExpr {
Foo{
x: 3
}
}
}
}

View File

@ -759,7 +759,8 @@ pub fn (table &Table) type_to_str_using_aliases(t Type, import_aliases map[strin
.int_literal, .float_literal {
res = sym.name
}
.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .f32, .f64, .char, .rune, .string, .bool, .none_, .byteptr, .voidptr, .charptr {
.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .f32, .f64, .char, .rune, .string, .bool,
.none_, .byteptr, .voidptr, .charptr {
// primitive types
res = sym.kind.str()
}