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

View File

@ -677,8 +677,13 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
} }
f.write('\n') 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 // Handle comments after last field
for comment in node.end_comments { for comment in comments {
f.indent++ f.indent++
f.empty_line = true f.empty_line = true
f.comment(comment, { f.comment(comment, {
@ -687,7 +692,6 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
f.writeln('') f.writeln('')
f.indent-- f.indent--
} }
f.writeln('}\n')
} }
pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) { 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.expr(field.expr)
f.writeln('') f.writeln('')
} }
f.comments_after_last_field(it.end_comments)
f.indent-- f.indent--
f.writeln(')\n') 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() // ( p.next() // (
mut fields := []ast.ConstField{} mut fields := []ast.ConstField{}
for p.tok.kind != .rpar { mut comments := []ast.Comment{}
mut comments := []ast.Comment{} for {
for p.tok.kind == .comment { comments = p.eat_comments()
comments << p.comment() if p.tok.kind == .rpar {
if p.tok.kind == .rpar { break
break
}
} }
pos := p.tok.position() pos := p.tok.position()
name := p.check_name() name := p.check_name()
@ -1630,6 +1628,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
} }
fields << field fields << field
p.global_scope.register(field.name, field) p.global_scope.register(field.name, field)
comments = []
} }
p.top_level_statement_end() p.top_level_statement_end()
p.check(.rpar) p.check(.rpar)
@ -1637,6 +1636,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
pos: start_pos.extend(end_pos) pos: start_pos.extend(end_pos)
fields: fields fields: fields
is_pub: is_pub is_pub: is_pub
end_comments: comments
} }
} }