1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vfmt: handle comments after last const field

This commit is contained in:
Delyan Angelov 2020-08-27 15:07:49 +03:00
parent f5c245ceb8
commit 7476428def
4 changed files with 20 additions and 10 deletions

View File

@ -14,7 +14,7 @@ pub type Expr = AnonFn | ArrayInit | AsCast | Assoc | BoolLiteral | CallExpr | C
IfGuardExpr | IndexExpr | InfixExpr | IntegerLiteral | Likely | LockExpr | MapInit | MatchExpr |
None | OrExpr | ParExpr | PostfixExpr | PrefixExpr | RangeExpr | SelectorExpr | SizeOf |
SqlExpr | StringInterLiteral | StringLiteral | StructInit | Type | TypeOf | UnsafeExpr
pub type Stmt = AssertStmt | AssignStmt | Block | BranchStmt | CompFor | CompIf | ConstDecl |
DeferStmt | EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | ForStmt | GlobalDecl |
@ -162,6 +162,7 @@ pub:
pos token.Position
pub mut:
fields []ConstField
end_comments []Comment
}
pub struct StructDecl {

View File

@ -677,8 +677,13 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
}
f.write('\n')
}
f.comments_after_last_field(node.end_comments)
f.writeln('}\n')
}
pub fn (mut f Fmt) comments_after_last_field(comments []ast.Comment) {
// Handle comments after last field
for comment in node.end_comments {
for comment in comments {
f.indent++
f.empty_line = true
f.comment(comment, {
@ -687,7 +692,6 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
f.writeln('')
f.indent--
}
f.writeln('}\n')
}
pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
@ -1814,6 +1818,7 @@ pub fn (mut f Fmt) const_decl(it ast.ConstDecl) {
f.expr(field.expr)
f.writeln('')
}
f.comments_after_last_field(it.end_comments)
f.indent--
f.writeln(')\n')
}

View File

@ -0,0 +1,4 @@
const (
fsm_state_array = ['init', 'state_a', 'state_b', 'state_c', 'exit'] // use as a first half key for map see fsm_state_ev_fn, the same order as in enum FSM_state
fsm_event_array = ['ev1', 'ev2', 'ev3', 'ev4', 'ev5'] // use as a second half key for map see fsm_state_ev_fn, the same order as in enum FSM_event
)

View File

@ -1602,13 +1602,11 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
}
p.next() // (
mut fields := []ast.ConstField{}
for p.tok.kind != .rpar {
mut comments := []ast.Comment{}
for p.tok.kind == .comment {
comments << p.comment()
if p.tok.kind == .rpar {
break
}
mut comments := []ast.Comment{}
for {
comments = p.eat_comments()
if p.tok.kind == .rpar {
break
}
pos := p.tok.position()
name := p.check_name()
@ -1630,6 +1628,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
}
fields << field
p.global_scope.register(field.name, field)
comments = []
}
p.top_level_statement_end()
p.check(.rpar)
@ -1637,6 +1636,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
pos: start_pos.extend(end_pos)
fields: fields
is_pub: is_pub
end_comments: comments
}
}