From c9225655258d66876234fd29776a302d50aae079 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Tue, 15 Dec 2020 04:26:28 +0100 Subject: [PATCH] fmt: only insert a space after // if the 3rd char is alphanumeric (#7330) --- vlib/v/fmt/fmt.v | 13 ++++++++----- vlib/v/fmt/tests/comments_keep.vv | 5 ++++- vlib/v/scanner/scanner.v | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 4fd17cd3bf..e76d90ebd7 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1204,16 +1204,19 @@ 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 } - if s == '' { - s = '//' - } else { - s = '// ' + s + mut out_s := '//' + if s != '' { + match s[0] { + `a`...`z`, `A`...`Z`, `0`...`9` { out_s += ' ' } + else {} + } + out_s += s } if !is_separate_line && f.indent > 0 { f.remove_new_line() // delete the generated \n f.write(' ') } - f.write(s) + f.write(out_s) return } lines := node.text.split_into_lines() diff --git a/vlib/v/fmt/tests/comments_keep.vv b/vlib/v/fmt/tests/comments_keep.vv index 6c7ccb4fd8..9a648ffa99 100644 --- a/vlib/v/fmt/tests/comments_keep.vv +++ b/vlib/v/fmt/tests/comments_keep.vv @@ -30,6 +30,9 @@ fn main() { a := 123 // comment after assign b := 'foo' // also comment after assign c := true - // between two assigns + // Between two assigns d := false + ////// + // / + // 123 } diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 19c27107e8..0fd6973309 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -926,7 +926,7 @@ fn (mut s Scanner) text_scan() token.Token { } if s.should_parse_comment() { s.line_comment = s.text[start + 1..comment_line_end] - mut comment := s.line_comment.trim_space() + mut comment := s.line_comment // Find out if this comment is on its own line (for vfmt) mut is_separate_line_comment := true for j := start - 2; j >= 0 && s.text[j] != `\n`; j-- {