fmt: improve single line const comment placement (#13595)

pull/13606/head
Larpon 2022-02-25 13:46:12 +01:00 committed by GitHub
parent 9535d38645
commit 73f931b52e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 3 deletions

View File

@ -842,10 +842,25 @@ pub fn (mut f Fmt) const_decl(node ast.ConstDecl) {
f.write(strings.repeat(` `, align_infos[align_idx].max - field.name.len)) f.write(strings.repeat(` `, align_infos[align_idx].max - field.name.len))
f.write('= ') f.write('= ')
f.expr(field.expr) f.expr(field.expr)
if node.is_block {
f.writeln('') f.writeln('')
} else {
// Write out single line comments after const expr if present
// E.g.: `const x = 1 // <comment>`
if node.end_comments.len > 0 && node.end_comments[0].text.contains('\n') {
f.writeln('\n')
}
f.comments(node.end_comments, inline: true)
}
prev_field = field prev_field = field
} }
if node.is_block {
f.comments_after_last_field(node.end_comments) f.comments_after_last_field(node.end_comments)
} else if node.end_comments.len == 0 {
// If no single line comments after the const expr is present
f.writeln('')
}
if node.is_block { if node.is_block {
f.indent-- f.indent--
f.writeln(')\n') f.writeln(')\n')

View File

@ -0,0 +1,15 @@
// 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_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 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() p.top_level_statement_end()
if is_block { if is_block {
p.check(.rpar) p.check(.rpar)
} else {
comments << p.eat_comments(same_line: true)
} }
return ast.ConstDecl{ return ast.ConstDecl{
pos: start_pos.extend_with_last_line(const_pos, p.prev_tok.line_nr) pos: start_pos.extend_with_last_line(const_pos, p.prev_tok.line_nr)