fmt: update expr_is_single_line for MatchExpr, StructInit and CallExpr with or block (#7564)
parent
2c0fba5480
commit
e7ca5dd17a
|
@ -1192,11 +1192,17 @@ 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 { return stmt.pos }
|
||||
GoStmt { return stmt.call_expr.position() }
|
||||
TypeDecl { 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
|
||||
}
|
||||
GoStmt {
|
||||
return stmt.call_expr.position()
|
||||
}
|
||||
TypeDecl {
|
||||
match stmt {
|
||||
AliasTypeDecl, FnTypeDecl, SumTypeDecl { return stmt.pos }
|
||||
} }
|
||||
}
|
||||
}
|
||||
// Please, do NOT use else{} here.
|
||||
// This match is exhaustive *on purpose*, to help force
|
||||
// maintaining/implementing proper .pos fields.
|
||||
|
|
|
@ -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.
|
||||
pub fn (d Doc) stmt_name(stmt ast.Stmt) string {
|
||||
match stmt {
|
||||
ast.FnDecl, ast.StructDecl, ast.EnumDecl, ast.InterfaceDecl { return stmt.name }
|
||||
ast.TypeDecl { match stmt {
|
||||
ast.FnDecl, ast.StructDecl, ast.EnumDecl, ast.InterfaceDecl {
|
||||
return stmt.name
|
||||
}
|
||||
ast.TypeDecl {
|
||||
match stmt {
|
||||
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.
|
||||
pub fn (d Doc) stmt_pub(stmt ast.Stmt) bool {
|
||||
match stmt {
|
||||
ast.FnDecl, ast.StructDecl, ast.EnumDecl, ast.InterfaceDecl, ast.ConstDecl { return stmt.is_pub }
|
||||
ast.TypeDecl { match stmt {
|
||||
ast.FnDecl, ast.StructDecl, ast.EnumDecl, ast.InterfaceDecl, ast.ConstDecl {
|
||||
return stmt.is_pub
|
||||
}
|
||||
ast.TypeDecl {
|
||||
match stmt {
|
||||
ast.FnTypeDecl, ast.AliasTypeDecl, ast.SumTypeDecl { return stmt.is_pub }
|
||||
} }
|
||||
else { return false }
|
||||
}
|
||||
}
|
||||
else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1696,8 +1696,25 @@ fn stmt_is_single_line(stmt ast.Stmt) bool {
|
|||
|
||||
fn expr_is_single_line(expr ast.Expr) bool {
|
||||
match expr {
|
||||
ast.IfExpr { return false }
|
||||
ast.Comment { return false }
|
||||
ast.IfExpr {
|
||||
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 {}
|
||||
}
|
||||
return true
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue