From 0e490766dfda9407f10a16b109506615fdc7afed Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Wed, 13 Jan 2021 03:30:24 +0100 Subject: [PATCH] fmt: do not move single line block comments to previous line (#8046) --- vlib/v/fmt/fmt.v | 6 +++--- vlib/v/fmt/tests/comments_expected.vv | 12 ++++++++++++ vlib/v/fmt/tests/comments_input.vv | 12 ++++++++++++ vlib/v/scanner/scanner.v | 5 ++++- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index ca6dc217a1..cbebeba7bf 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -725,7 +725,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { continue } if comment.pos.pos > field.pos.pos { - comments_len += '/* $comment.text */ '.len + comments_len += '/* ${comment.text.trim_left('\x01')} */ '.len } } field_aligns.add_info(comments_len + field.name.len, ft.len, field.pos.line_nr) @@ -764,7 +764,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { // Handle comments between field name and type mut comments_len := 0 for comm_idx < comments.len && comments[comm_idx].pos.pos < end_pos { - comment_text := '/* ${comments[comm_idx].text} */ ' // TODO handle in a function + comment_text := '/* ${comments[comm_idx].text.trim_left('\x01')} */ ' // TODO handle in a function comments_len += comment_text.len f.write(comment_text) comm_idx++ @@ -1419,7 +1419,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { } if !node.text.contains('\n') { is_separate_line := !options.inline || node.text.starts_with('\x01') - mut s := if node.text.starts_with('\x01') { node.text[1..] } else { node.text } + mut s := node.text.trim_left('\x01') mut out_s := '//' if s != '' { match s[0] { diff --git a/vlib/v/fmt/tests/comments_expected.vv b/vlib/v/fmt/tests/comments_expected.vv index a2b29961bc..c02f724f7f 100644 --- a/vlib/v/fmt/tests/comments_expected.vv +++ b/vlib/v/fmt/tests/comments_expected.vv @@ -9,6 +9,18 @@ fn mr_fun() (int, int) { return 1, 2 } +fn single_line_blocks() { + // 1 + println('') + // 2 + println('') + // 3 + // 4 + println('') + // 5 + // 6 +} + fn main() { /* block1 diff --git a/vlib/v/fmt/tests/comments_input.vv b/vlib/v/fmt/tests/comments_input.vv index 87b87daabf..a6f4a3f509 100644 --- a/vlib/v/fmt/tests/comments_input.vv +++ b/vlib/v/fmt/tests/comments_input.vv @@ -6,6 +6,18 @@ fn mr_fun() (int, int) { return /* one comment */ 1, /* another comment */ 2 } +fn single_line_blocks() { + /* 1 */ + println('') + /* 2 */ + println('') + /* 3 */ + /* 4 */ + println('') + // 5 + /* 6 */ +} + fn main() { /* block1 */ diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index a7d111522b..ca5eb0a77f 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -959,7 +959,10 @@ fn (mut s Scanner) text_scan() token.Token { } s.pos++ if s.should_parse_comment() { - comment := s.text[start..(s.pos - 1)].trim(' ') + mut comment := s.text[start..(s.pos - 1)].trim(' ') + if !comment.contains('\n') { + comment = '\x01' + comment + } return s.new_token(.comment, comment, comment.len + 4) } // Skip if not in fmt mode