Skip to content

Commit

Permalink
fmt: fix formatting multiline comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Dec 4, 2023
1 parent c047a49 commit c72058f
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 98 deletions.
30 changes: 14 additions & 16 deletions vlib/v/fmt/comments.v
Original file line number Diff line number Diff line change
Expand Up @@ -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--
Expand Down
20 changes: 8 additions & 12 deletions vlib/v/fmt/tests/comments_expected.vv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions vlib/v/fmt/tests/consts_with_comments_expected.vv
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const one = 1 // leave

const two = 2

/*
move
/* move
*/

const three = 3 // rewrite and leave
Expand Down
3 changes: 1 addition & 2 deletions vlib/v/fmt/tests/fn_headers_with_comments_expected.vv
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
File renamed without changes.
7 changes: 7 additions & 0 deletions vlib/v/fmt/tests/multiline_comment_2_keep.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
*
* BRUH
*
*/
fn main() {
}
File renamed without changes.
66 changes: 0 additions & 66 deletions vlib/v/fmt/tests/structs_input.vv

This file was deleted.

0 comments on commit c72058f

Please sign in to comment.