fmt: keep new line at both ends of block comment (#8003)

pull/8016/head
zakuro 2021-01-11 05:06:29 +09:00 committed by GitHub
parent 8f4238e76a
commit 2103d9a6c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 3 deletions

View File

@ -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)

View File

@ -10,6 +10,15 @@ fn mr_fun() (int, int) {
}
fn main() {
/*
block1
*/
/*
block2
*/
/*
block3
*/
// this is a comment
a := 1
// and another comment

View File

@ -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

View File

@ -43,5 +43,8 @@ fn main() {
//////
// /
// 123
/*
block
*/
println('hello world')
}

View File

@ -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