From 2103d9a6c43a3bc957c0988316c51bd28f0fd828 Mon Sep 17 00:00:00 2001 From: zakuro Date: Mon, 11 Jan 2021 05:06:29 +0900 Subject: [PATCH] fmt: keep new line at both ends of block comment (#8003) --- vlib/v/fmt/fmt.v | 4 ++-- vlib/v/fmt/tests/comments_expected.vv | 9 +++++++++ vlib/v/fmt/tests/comments_input.vv | 9 +++++++++ vlib/v/fmt/tests/comments_keep.vv | 3 +++ vlib/v/scanner/scanner.v | 2 +- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index f6acb25bed..afe5944f03 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1372,7 +1372,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { x := node.text.trim_left('\x01') if x.contains('\n') { f.writeln('/*') - f.writeln(x) + f.writeln(x.trim_space()) f.write('*/') } else { f.write('/* ${x.trim(' ')} */') @@ -1397,7 +1397,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { f.write(out_s) return } - lines := node.text.split_into_lines() + lines := node.text.trim_space().split_into_lines() f.writeln('/*') for line in lines { f.writeln(line) diff --git a/vlib/v/fmt/tests/comments_expected.vv b/vlib/v/fmt/tests/comments_expected.vv index 603e593128..a2b29961bc 100644 --- a/vlib/v/fmt/tests/comments_expected.vv +++ b/vlib/v/fmt/tests/comments_expected.vv @@ -10,6 +10,15 @@ fn mr_fun() (int, int) { } fn main() { + /* + block1 + */ + /* + block2 + */ + /* + block3 + */ // this is a comment a := 1 // and another comment diff --git a/vlib/v/fmt/tests/comments_input.vv b/vlib/v/fmt/tests/comments_input.vv index c53484437c..87b87daabf 100644 --- a/vlib/v/fmt/tests/comments_input.vv +++ b/vlib/v/fmt/tests/comments_input.vv @@ -7,6 +7,15 @@ fn mr_fun() (int, int) { } fn main() { + /* block1 + */ + /* + block2 */ + /* + + block3 + + */ a := /* this is a comment */ 1 b, c := /* and another comment */ a, /* just to make it worse */ 2 d := c // and an extra one diff --git a/vlib/v/fmt/tests/comments_keep.vv b/vlib/v/fmt/tests/comments_keep.vv index 9f1d287f02..ce5fdcbc82 100644 --- a/vlib/v/fmt/tests/comments_keep.vv +++ b/vlib/v/fmt/tests/comments_keep.vv @@ -43,5 +43,8 @@ fn main() { ////// // / // 123 + /* + block + */ println('hello world') } diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 6c145e0a01..58305176b1 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -959,7 +959,7 @@ fn (mut s Scanner) text_scan() token.Token { } s.pos++ if s.should_parse_comment() { - comment := s.text[start..(s.pos - 1)].trim_space() + comment := s.text[start..(s.pos - 1)].trim(' ') return s.new_token(.comment, comment, comment.len + 4) } // Skip if not in fmt mode