From 73f931b52e6374c6ee0815c756a225819f9afcba Mon Sep 17 00:00:00 2001 From: Larpon Date: Fri, 25 Feb 2022 13:46:12 +0100 Subject: [PATCH] fmt: improve single line const comment placement (#13595) --- .../ed25519/internal/edwards25519/scalar.v | 2 +- vlib/v/fmt/fmt.v | 19 +++++++++++++++++-- .../tests/consts_with_comments_expected.vv | 15 +++++++++++++++ .../v/fmt/tests/consts_with_comments_input.vv | 11 +++++++++++ vlib/v/fmt/tests/consts_with_comments_keep.vv | 5 +++++ vlib/v/parser/parser.v | 2 ++ 6 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 vlib/v/fmt/tests/consts_with_comments_expected.vv create mode 100644 vlib/v/fmt/tests/consts_with_comments_input.vv diff --git a/vlib/crypto/ed25519/internal/edwards25519/scalar.v b/vlib/crypto/ed25519/internal/edwards25519/scalar.v index ee8fffffeb..aa1f3cc8e6 100644 --- a/vlib/crypto/ed25519/internal/edwards25519/scalar.v +++ b/vlib/crypto/ed25519/internal/edwards25519/scalar.v @@ -1099,7 +1099,7 @@ fn generate_scalar(size int) ?Scalar { return reflect.ValueOf(s) */ mut s := edwards25519.sc_zero - diceroll := rand.intn(100) or {0} + diceroll := rand.intn(100) or { 0 } match true { /* case diceroll == 0: diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index c9ccd98980..698c61335b 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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('= ') f.expr(field.expr) - f.writeln('') + if node.is_block { + f.writeln('') + } else { + // Write out single line comments after const expr if present + // E.g.: `const x = 1 // ` + 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 } - f.comments_after_last_field(node.end_comments) + + if node.is_block { + 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 { f.indent-- f.writeln(')\n') diff --git a/vlib/v/fmt/tests/consts_with_comments_expected.vv b/vlib/v/fmt/tests/consts_with_comments_expected.vv new file mode 100644 index 0000000000..3f428cfacf --- /dev/null +++ b/vlib/v/fmt/tests/consts_with_comments_expected.vv @@ -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 diff --git a/vlib/v/fmt/tests/consts_with_comments_input.vv b/vlib/v/fmt/tests/consts_with_comments_input.vv new file mode 100644 index 0000000000..4d6511500e --- /dev/null +++ b/vlib/v/fmt/tests/consts_with_comments_input.vv @@ -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 diff --git a/vlib/v/fmt/tests/consts_with_comments_keep.vv b/vlib/v/fmt/tests/consts_with_comments_keep.vv index 2f8a2218e8..aed4eec418 100644 --- a/vlib/v/fmt/tests/consts_with_comments_keep.vv +++ b/vlib/v/fmt/tests/consts_with_comments_keep.vv @@ -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 diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 434082de31..600bbbff7a 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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)