fmt: improve const comment placement

pull/13595/head
lmp 2022-02-24 10:34:59 +01:00
parent 9662b79662
commit c43b4f2784
5 changed files with 44 additions and 2 deletions

View File

@ -842,10 +842,20 @@ pub fn (mut f Fmt) const_decl(node ast.ConstDecl) {
f.write(strings.repeat(` `, align_infos[align_idx].max - field.name.len))
f.write('= ')
f.expr(field.expr)
f.writeln('')
if node.is_block {
f.writeln('')
} else {
if node.end_comments.len > 0 && node.end_comments[0].text.contains('\n') {
f.writeln('')
}
f.comments(node.end_comments, inline: true)
}
prev_field = field
}
f.comments_after_last_field(node.end_comments)
if node.is_block {
f.comments_after_last_field(node.end_comments)
}
if node.is_block {
f.indent--
f.writeln(')\n')

View File

@ -0,0 +1,14 @@
// leave
const one = 1 // leave
// move
const two = 2
/*
move
*/
const three = 3 // rewrite and leave
// leave
const four = 4 // leave

View File

@ -0,0 +1,11 @@
// leave
const one = 1 // leave
// move
const two = 2 /* move
*/
const three = 3 /* rewrite and leave */
// leave
const four = 4 // leave

View File

@ -2,3 +2,8 @@ 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
)
// Keep
const one = 1 // Keep
// Keep

View File

@ -3292,6 +3292,8 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
p.top_level_statement_end()
if is_block {
p.check(.rpar)
} else {
comments << p.eat_comments(same_line: true)
}
return ast.ConstDecl{
pos: start_pos.extend_with_last_line(const_pos, p.prev_tok.line_nr)