From 997f56a3dcd4745b68b61176da4f6548bb76a158 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Sun, 24 Jan 2021 22:08:24 +0100 Subject: [PATCH] fmt: better newline handling in block comments (#8325) --- vlib/v/fmt/comments.v | 25 ++++++++++++++++++++----- vlib/v/fmt/tests/comments_expected.vv | 17 +++++++++++++++++ vlib/v/fmt/tests/comments_input.vv | 13 +++++++++++++ vlib/v/fmt/tests/comments_keep.vv | 18 ++++++++++++++---- 4 files changed, 64 insertions(+), 9 deletions(-) diff --git a/vlib/v/fmt/comments.v b/vlib/v/fmt/comments.v index 45a705ea1f..840b5746fe 100644 --- a/vlib/v/fmt/comments.v +++ b/vlib/v/fmt/comments.v @@ -44,9 +44,8 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { mut s := node.text.trim_left('\x01') mut out_s := '//' if s != '' { - match s[0] { - `a`...`z`, `A`...`Z`, `0`...`9` { out_s += ' ' } - else {} + if is_first_char_alphanumeric(s) { + out_s += ' ' } out_s += s } @@ -57,12 +56,21 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { f.write(out_s) } else { lines := node.text.trim_space().split_into_lines() - f.writeln('/*') + expected_line_count := node.pos.last_line - node.pos.line_nr + no_new_lines := lines.len > expected_line_count && !is_first_char_alphanumeric(lines[0]) + f.write('/*') + if !no_new_lines { + f.writeln('') + } for line in lines { f.writeln(line) f.empty_line = false } - f.empty_line = true + if no_new_lines { + f.remove_new_line() + } else { + f.empty_line = true + } f.write('*/') } if options.level == .indent { @@ -96,3 +104,10 @@ pub fn (mut f Fmt) comments_after_last_field(comments []ast.Comment) { f.indent-- } } + +fn is_first_char_alphanumeric(s string) bool { + return match s[0] { + `a`...`z`, `A`...`Z`, `0`...`9` { true } + else { false } + } +} diff --git a/vlib/v/fmt/tests/comments_expected.vv b/vlib/v/fmt/tests/comments_expected.vv index c02f724f7f..85a7e56eb2 100644 --- a/vlib/v/fmt/tests/comments_expected.vv +++ b/vlib/v/fmt/tests/comments_expected.vv @@ -64,3 +64,20 @@ fn main() { // empty return return } + +fn insert_space() { + // abc +} + +fn linebreaks_in_block_comments() { + /* + foo + comment goes here! + bar + */ + /* + spam + spaces make no difference there + eggs + */ +} diff --git a/vlib/v/fmt/tests/comments_input.vv b/vlib/v/fmt/tests/comments_input.vv index a6f4a3f509..3078f64587 100644 --- a/vlib/v/fmt/tests/comments_input.vv +++ b/vlib/v/fmt/tests/comments_input.vv @@ -52,3 +52,16 @@ fn main() { } return // empty return } + +fn insert_space() { + //abc +} + +fn linebreaks_in_block_comments() { + /*foo + comment goes here! + bar*/ + /* spam + spaces make no difference there + eggs */ +} diff --git a/vlib/v/fmt/tests/comments_keep.vv b/vlib/v/fmt/tests/comments_keep.vv index e44c28cc81..5e63a4edbc 100644 --- a/vlib/v/fmt/tests/comments_keep.vv +++ b/vlib/v/fmt/tests/comments_keep.vv @@ -35,16 +35,26 @@ fn main() { _ := User{ // Just a comment } + ////// + // / + // 123 +} + +fn assign_comments() { a := 123 // comment after assign b := 'foo' // also comment after assign c := true // Between two assigns d := false - ////// - // / - // 123 + // at the end +} + +fn linebreaks_in_block_comments() { /* block */ - println('hello world') + /***** + Want a long line of stars? + no problem + *****/ }