From e7ca5dd17a22add70d83646eeaaa5f4c2419de21 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Fri, 25 Dec 2020 20:43:50 +0100 Subject: [PATCH] fmt: update expr_is_single_line for MatchExpr, StructInit and CallExpr with or block (#7564) --- vlib/v/ast/ast.v | 14 ++++++--- vlib/v/doc/utils.v | 32 +++++++++++++++------ vlib/v/fmt/fmt.v | 21 ++++++++++++-- vlib/v/fmt/tests/match_keep.vv | 52 ++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 vlib/v/fmt/tests/match_keep.vv diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 22ecbf2eaf..08da857ccd 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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. diff --git a/vlib/v/doc/utils.v b/vlib/v/doc/utils.v index 4c4acc5b97..b209668f1b 100644 --- a/vlib/v/doc/utils.v +++ b/vlib/v/doc/utils.v @@ -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 + } } } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 7e43e289f5..a28d270f50 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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 diff --git a/vlib/v/fmt/tests/match_keep.vv b/vlib/v/fmt/tests/match_keep.vv new file mode 100644 index 0000000000..3271e9cf5a --- /dev/null +++ b/vlib/v/fmt/tests/match_keep.vv @@ -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) + } + } + } +}