fmt: update expr_is_single_line for MatchExpr, StructInit and CallExpr with or block (#7564)

pull/7567/head
Lukas Neubert 2020-12-25 20:43:50 +01:00 committed by GitHub
parent 2c0fba5480
commit e7ca5dd17a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 104 additions and 15 deletions

View File

@ -1192,11 +1192,17 @@ pub:
pub fn (stmt Stmt) position() token.Position { pub fn (stmt Stmt) position() token.Position {
match stmt { 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 { return stmt.pos } AssertStmt, AssignStmt, Block, BranchStmt, CompFor, ConstDecl, DeferStmt, EnumDecl, ExprStmt, FnDecl, ForCStmt, ForInStmt, ForStmt, GotoLabel, GotoStmt, Import, Return, StructDecl, GlobalDecl, HashStmt, InterfaceDecl, Module, SqlStmt {
GoStmt { return stmt.call_expr.position() } return stmt.pos
TypeDecl { match stmt { }
GoStmt {
return stmt.call_expr.position()
}
TypeDecl {
match stmt {
AliasTypeDecl, FnTypeDecl, SumTypeDecl { return stmt.pos } AliasTypeDecl, FnTypeDecl, SumTypeDecl { return stmt.pos }
} } }
}
// Please, do NOT use else{} here. // Please, do NOT use else{} here.
// This match is exhaustive *on purpose*, to help force // This match is exhaustive *on purpose*, to help force
// maintaining/implementing proper .pos fields. // maintaining/implementing proper .pos fields.

View File

@ -98,12 +98,20 @@ pub fn (mut d Doc) stmt_signature(stmt ast.Stmt) string {
// stmt_name returns the name of a given `ast.Stmt` node. // stmt_name returns the name of a given `ast.Stmt` node.
pub fn (d Doc) stmt_name(stmt ast.Stmt) string { pub fn (d Doc) stmt_name(stmt ast.Stmt) string {
match stmt { match stmt {
ast.FnDecl, ast.StructDecl, ast.EnumDecl, ast.InterfaceDecl { return stmt.name } ast.FnDecl, ast.StructDecl, ast.EnumDecl, ast.InterfaceDecl {
ast.TypeDecl { match stmt { return stmt.name
}
ast.TypeDecl {
match stmt {
ast.FnTypeDecl, ast.AliasTypeDecl, ast.SumTypeDecl { return stmt.name } ast.FnTypeDecl, ast.AliasTypeDecl, ast.SumTypeDecl { return stmt.name }
} } }
ast.ConstDecl { return '' } // leave it blank }
else { return '' } ast.ConstDecl {
return ''
} // leave it blank
else {
return ''
}
} }
} }
@ -111,11 +119,17 @@ pub fn (d Doc) stmt_name(stmt ast.Stmt) string {
// is exposed to the public. // is exposed to the public.
pub fn (d Doc) stmt_pub(stmt ast.Stmt) bool { pub fn (d Doc) stmt_pub(stmt ast.Stmt) bool {
match stmt { match stmt {
ast.FnDecl, ast.StructDecl, ast.EnumDecl, ast.InterfaceDecl, ast.ConstDecl { return stmt.is_pub } ast.FnDecl, ast.StructDecl, ast.EnumDecl, ast.InterfaceDecl, ast.ConstDecl {
ast.TypeDecl { match stmt { return stmt.is_pub
}
ast.TypeDecl {
match stmt {
ast.FnTypeDecl, ast.AliasTypeDecl, ast.SumTypeDecl { return stmt.is_pub } ast.FnTypeDecl, ast.AliasTypeDecl, ast.SumTypeDecl { return stmt.is_pub }
} } }
else { return false } }
else {
return false
}
} }
} }

View File

@ -1696,8 +1696,25 @@ fn stmt_is_single_line(stmt ast.Stmt) bool {
fn expr_is_single_line(expr ast.Expr) bool { fn expr_is_single_line(expr ast.Expr) bool {
match expr { match expr {
ast.IfExpr { return false } ast.IfExpr {
ast.Comment { return false } return false
}
ast.Comment {
return false
}
ast.MatchExpr {
return false
}
ast.StructInit {
if expr.fields.len > 0 || expr.pre_comments.len > 0 {
return false
}
}
ast.CallExpr {
if expr.or_block.stmts.len > 1 {
return false
}
}
else {} else {}
} }
return true return true

View File

@ -0,0 +1,52 @@
fn main() {
// Nested match
match 100 {
0...1 {
println('0 to 1')
}
else {
match 200 {
0...1 { println('0 to 1') }
else { println('unknown value') }
}
}
}
// StructInit
match 'a' {
'b' { SpamStruct{} }
}
match 'a' {
'b' {
SpamStruct{
x: 42
}
}
}
match 'a' {
'b' {
SpamStruct{
// only a comment
}
}
}
// CallExpr with or block
match 'a' {
'b' { foo() or { panic(err) } }
}
match 'a' {
'b' {
foo() or {
// do stuff
panic(err)
}
}
}
match 'a' {
'b' {
foo() or {
another_stmt()
panic(err)
}
}
}
}