fmt: keep same line comment in array init (#7980)

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

View File

@ -1944,7 +1944,10 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
f.array_init_break << (last_line_nr < line_nr) f.array_init_break << (last_line_nr < line_nr)
} }
} }
mut penalty := if f.array_init_break[f.array_init_depth - 1] { 0 } else { 3 } is_same_line_comment := i > 0 &&
(expr is ast.Comment && line_nr == it.exprs[i - 1].position().line_nr)
line_break := f.array_init_break[f.array_init_depth - 1]
mut penalty := if line_break && !is_same_line_comment { 0 } else { 3 }
if penalty > 0 { if penalty > 0 {
if i == 0 || it.exprs[i - 1] is ast.ArrayInit || it.exprs[i - 1] is ast.StructInit || if i == 0 || it.exprs[i - 1] is ast.ArrayInit || it.exprs[i - 1] is ast.StructInit ||
it.exprs[i - 1] is ast.MapInit || it.exprs[i - 1] is ast.CallExpr { it.exprs[i - 1] is ast.MapInit || it.exprs[i - 1] is ast.CallExpr {
@ -1976,6 +1979,8 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
f.write(',') f.write(',')
} }
f.writeln('') f.writeln('')
} else if is_same_line_comment {
f.writeln('')
} }
} else if expr !is ast.Comment { } else if expr !is ast.Comment {
f.write(',') f.write(',')

View File

@ -5,6 +5,8 @@ fn main() {
/* test 1 */ /* test 1 */
2, 2,
/* test 2 */ /* test 2 */
3, /* 3 */
4, /* 4-1 */ /* 4-2 */
] ]
arr2 := [ arr2 := [
'foo', 'foo',