vfmt: fix indent level of commented match branches

pull/5643/head
Delyan Angelov 2020-07-03 17:16:20 +03:00
parent b7175b54eb
commit df2749dd50
2 changed files with 75 additions and 6 deletions

View File

@ -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('}')

View File

@ -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{} }
}
}