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') x := node.text.trim_left('\x01')
if x.contains('\n') { if x.contains('\n') {
f.writeln('/*') f.writeln('/*')
f.writeln(x) f.writeln(x.trim_space())
f.write('*/') f.write('*/')
} else { } else {
f.write('/* ${x.trim(' ')} */') f.write('/* ${x.trim(' ')} */')
@ -1397,7 +1397,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
f.write(out_s) f.write(out_s)
return return
} }
lines := node.text.split_into_lines() lines := node.text.trim_space().split_into_lines()
f.writeln('/*') f.writeln('/*')
for line in lines { for line in lines {
f.writeln(line) f.writeln(line)

View File

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

View File

@ -7,6 +7,15 @@ fn mr_fun() (int, int) {
} }
fn main() { fn main() {
/* block1
*/
/*
block2 */
/*
block3
*/
a := /* this is a comment */ 1 a := /* this is a comment */ 1
b, c := /* and another comment */ a, /* just to make it worse */ 2 b, c := /* and another comment */ a, /* just to make it worse */ 2
d := c // and an extra one d := c // and an extra one

View File

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

View File

@ -959,7 +959,7 @@ fn (mut s Scanner) text_scan() token.Token {
} }
s.pos++ s.pos++
if s.should_parse_comment() { 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) return s.new_token(.comment, comment, comment.len + 4)
} }
// Skip if not in fmt mode // Skip if not in fmt mode