From c72058fd9a0334348c5d597a6029bc5d3fde5581 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 4 Dec 2023 17:17:39 +0800 Subject: [PATCH] fmt: fix formatting multiline comments --- vlib/v/fmt/comments.v | 30 ++++----- vlib/v/fmt/tests/comments_expected.vv | 20 +++--- .../tests/consts_with_comments_expected.vv | 3 +- .../fn_headers_with_comments_expected.vv | 3 +- ...nt_keep.vv => multiline_comment_1_keep.vv} | 0 vlib/v/fmt/tests/multiline_comment_2_keep.vv | 7 ++ ...s_expected.vv => structs_comments_keep.vv} | 0 vlib/v/fmt/tests/structs_input.vv | 66 ------------------- 8 files changed, 31 insertions(+), 98 deletions(-) rename vlib/v/fmt/tests/{multiline_comment_keep.vv => multiline_comment_1_keep.vv} (100%) create mode 100644 vlib/v/fmt/tests/multiline_comment_2_keep.vv rename vlib/v/fmt/tests/{structs_expected.vv => structs_comments_keep.vv} (100%) delete mode 100644 vlib/v/fmt/tests/structs_input.vv diff --git a/vlib/v/fmt/comments.v b/vlib/v/fmt/comments.v index effbeca50e42c8..5bf02a52a049e0 100644 --- a/vlib/v/fmt/comments.v +++ b/vlib/v/fmt/comments.v @@ -58,24 +58,22 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { } f.write(out_s) } else { - lines := node.text.trim_space().split_into_lines() - start_break := is_char_alphanumeric(node.text[0]) || node.text[0].is_space() - end_break := is_char_alphanumeric(node.text.trim('\t').bytes().last()) - || node.text.bytes().last().is_space() + lines := node.text.split_into_lines() f.write('/*') - if start_break { - f.writeln('') - } - for line in lines { - f.writeln(line.trim_right(' ')) - f.empty_line = false - } - if end_break { - f.empty_line = true - } else { - f.remove_new_line() + for i, line in lines { + if i == lines.len - 1 { + f.empty_line = false + if node.text[node.text.len - 1] == `\n` { + f.writeln(line) + } else { + f.write(line) + } + f.write('*/') + } else { + f.empty_line = false + f.writeln(line.trim_right(' ')) + } } - f.write('*/') } if options.level == .indent { f.indent-- diff --git a/vlib/v/fmt/tests/comments_expected.vv b/vlib/v/fmt/tests/comments_expected.vv index abff1c18af6642..ff61a115fe054a 100644 --- a/vlib/v/fmt/tests/comments_expected.vv +++ b/vlib/v/fmt/tests/comments_expected.vv @@ -26,14 +26,14 @@ fn single_line_blocks() { } fn main() { - /* - block1 + /* block1 */ /* - block2 - */ + block2 */ /* + block3 + */ a := 1 // this is a comment d := c // and an extra one @@ -52,16 +52,12 @@ fn insert_space() { } fn linebreaks_in_block_comments() { - /* - foo + /*foo comment goes here! - bar - */ - /* - spam + bar*/ + /* spam spaces make no difference there - eggs - */ + eggs */ } fn between_if_branches() { diff --git a/vlib/v/fmt/tests/consts_with_comments_expected.vv b/vlib/v/fmt/tests/consts_with_comments_expected.vv index 3f428cfacf3660..cb1dcee2af5e12 100644 --- a/vlib/v/fmt/tests/consts_with_comments_expected.vv +++ b/vlib/v/fmt/tests/consts_with_comments_expected.vv @@ -5,8 +5,7 @@ const one = 1 // leave const two = 2 -/* -move +/* move */ const three = 3 // rewrite and leave diff --git a/vlib/v/fmt/tests/fn_headers_with_comments_expected.vv b/vlib/v/fmt/tests/fn_headers_with_comments_expected.vv index 8c2caad85a5052..16f34cd4493a00 100644 --- a/vlib/v/fmt/tests/fn_headers_with_comments_expected.vv +++ b/vlib/v/fmt/tests/fn_headers_with_comments_expected.vv @@ -7,6 +7,5 @@ fn C.Mix_LoadMUS3(file byteptr) voidptr // 1 2 3 // Loads music fn C.Mix_LoadMUS4(file byteptr) voidptr -/* -Test +/* Test */ diff --git a/vlib/v/fmt/tests/multiline_comment_keep.vv b/vlib/v/fmt/tests/multiline_comment_1_keep.vv similarity index 100% rename from vlib/v/fmt/tests/multiline_comment_keep.vv rename to vlib/v/fmt/tests/multiline_comment_1_keep.vv diff --git a/vlib/v/fmt/tests/multiline_comment_2_keep.vv b/vlib/v/fmt/tests/multiline_comment_2_keep.vv new file mode 100644 index 00000000000000..cd390da17013b6 --- /dev/null +++ b/vlib/v/fmt/tests/multiline_comment_2_keep.vv @@ -0,0 +1,7 @@ +/* + * + * BRUH + * + */ +fn main() { +} diff --git a/vlib/v/fmt/tests/structs_expected.vv b/vlib/v/fmt/tests/structs_comments_keep.vv similarity index 100% rename from vlib/v/fmt/tests/structs_expected.vv rename to vlib/v/fmt/tests/structs_comments_keep.vv diff --git a/vlib/v/fmt/tests/structs_input.vv b/vlib/v/fmt/tests/structs_input.vv deleted file mode 100644 index 30c5b50c5d850e..00000000000000 --- a/vlib/v/fmt/tests/structs_input.vv +++ /dev/null @@ -1,66 +0,0 @@ - - struct User { - name string // name -name2 []rune // name2 - very_long_field bool - age int // age - very_long_type_field1 very_looooog_type // long - very_long_type_field2 very_loooooooong_type // long - } - -struct FamousUser { - User -pub: - aka string -} - -struct Foo { - field1 int // f1 - field2 string // f2 - pub: - public_field1 int // f1 - public_field2 f64 // f2 - mut: - mut_field string - pub mut: - pub_mut_field string -} - -struct Bar { Foo } - -fn new_user() -User -{ - return User{ - name: 'Serious Sam' - age: 19 - } -} - -struct SomeStruct { -// 1 -mut: -// 2 -// 3 -somefield int /* -9 -10 -*/ -somefield2 int // 12 -pub: - - - - -somefield3 int - - - -somefield4 int -/* -13 -14 -*/ -} - -pub struct C.Foo