From df2749dd50a4a815d90035da3530003cf12a6c70 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 3 Jul 2020 17:16:20 +0300 Subject: [PATCH] vfmt: fix indent level of commented match branches --- vlib/v/fmt/fmt.v | 23 ++++++-- .../match_with_commented_branches_keep.vv | 58 +++++++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 vlib/v/fmt/tests/match_with_commented_branches_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 55af316439..b64d2560ff 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -17,6 +17,11 @@ const ( max_len = [0, 35, 85, 93, 100] ) +enum CommentsLevel { + keep + indent +} + pub struct Fmt { pub: table &table.Table @@ -316,14 +321,14 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) { } name := it.name.after('.') f.writeln('enum $name {') - f.comments(it.comments, false) + f.comments(it.comments, false, .indent) for field in it.fields { f.write('\t$field.name') if field.has_expr { f.write(' = ') f.expr(field.expr) } - f.comments(field.comments, true) + f.comments(field.comments, true, .indent) f.writeln('') } f.writeln('}\n') @@ -1101,14 +1106,18 @@ pub fn (mut f Fmt) comment(node ast.Comment) { f.writeln('*/') } -pub fn (mut f Fmt) comments(some_comments []ast.Comment, remove_last_new_line bool) { +pub fn (mut f Fmt) comments(some_comments []ast.Comment, remove_last_new_line bool, level CommentsLevel) { for c in some_comments { if !f.out.last_n(1)[0].is_space() { f.write('\t') } - f.indent++ + if level == .indent { + f.indent++ + } f.comment(c) - f.indent-- + if level == .indent { + f.indent-- + } } if remove_last_new_line { f.remove_new_line() @@ -1334,7 +1343,9 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) { f.writeln('}') } } - f.comments(branch.post_comments, false) + if branch.post_comments.len > 0 { + f.comments(branch.post_comments, false, .keep) + } } f.indent-- f.write('}') diff --git a/vlib/v/fmt/tests/match_with_commented_branches_keep.vv b/vlib/v/fmt/tests/match_with_commented_branches_keep.vv new file mode 100644 index 0000000000..4396b2fdbb --- /dev/null +++ b/vlib/v/fmt/tests/match_with_commented_branches_keep.vv @@ -0,0 +1,58 @@ +module ast + +pub fn (stmt Stmt) position() Position { + match stmt { + AssertStmt { return stmt.pos } + AssignStmt { return stmt.pos } + /* + // Attr { + // } + // Block { + // } + // BranchStmt { + // } + */ + Comment { return stmt.pos } + CompIf { return stmt.pos } + ConstDecl { return stmt.pos } + /* + // DeferStmt { + // } + */ + EnumDecl { return stmt.pos } + ExprStmt { return stmt.pos } + FnDecl { return stmt.pos } + ForCStmt { return stmt.pos } + ForInStmt { return stmt.pos } + ForStmt { return stmt.pos } + /* + // GlobalDecl { + // } + // GoStmt { + // } + // GotoLabel { + // } + // GotoStmt { + // } + // HashStmt { + // } + */ + Import { return stmt.pos } + /* + // InterfaceDecl { + // } + // Module { + // } + */ + Return { return stmt.pos } + StructDecl { return stmt.pos } + /* + // TypeDecl { + // } + // UnsafeStmt { + // } + */ + // + else { return Position{} } + } +}