vfmt: fix bug where duplicated comma is placed before comment in array init (#12281)

pull/12288/head
zakuro 2021-10-24 18:05:34 +09:00 committed by GitHub
parent 47a2301139
commit 242b99340d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 3 deletions

View File

@ -1460,7 +1460,8 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
if cmt.is_inline {
f.write(' ')
f.comment(cmt, iembed: true)
if cmt.pos.line_nr == expr_pos.last_line && cmt.pos.pos < expr_pos.pos {
if !set_comma && cmt.pos.line_nr == expr_pos.last_line
&& cmt.pos.pos < expr_pos.pos {
f.write(',')
set_comma = true
} else {
@ -1471,9 +1472,12 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
}
}
} else {
f.write(', ')
if !set_comma {
f.write(',')
set_comma = true
}
f.write(' ')
f.comment(cmt, iembed: false)
set_comma = true
}
}
last_comment_was_inline = cmt.is_inline

View File

@ -0,0 +1,9 @@
v := [
0, // 0
1, // 1
2, // 2
// 2-3
3, // 3
// 4
// 5
]

View File

@ -0,0 +1,9 @@
v := [
0// 0
1 // 1
2,// 2
// 2-3
3, // 3
// 4
/* 5 */
]