fmt: array_init: use line breaks of first elements in every hierarchy (#5777)

pull/5780/head
Uwe Krüger 2020-07-09 22:36:49 +02:00 committed by GitHub
parent 14d83c8457
commit 194ecda829
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 7 deletions

View File

@ -37,6 +37,8 @@ pub mut:
penalties []int // how hard should it be to break line after each expression
precedences []int // operator/parenthese precedences for operator at end of each expression
par_level int // how many parentheses are put around the current expression
array_init_break []bool // line breaks after elements in hierarchy level of multi dimensional array
array_init_depth int // current level of hierarchie in array init
single_line_if bool
cur_mod string
file ast.File
@ -1479,13 +1481,15 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
f.write('[')
mut inc_indent := false
mut last_line_nr := it.pos.line_nr // to have the same newlines between array elements
mut break_after_each_element := false
f.array_init_depth++
for i, expr in it.exprs {
line_nr := expr.position().line_nr
if i == 0 && last_line_nr < line_nr {
break_after_each_element = true
if i == 0 {
if f.array_init_depth > f.array_init_break.len {
f.array_init_break << (last_line_nr < line_nr)
}
mut penalty := if break_after_each_element { 0 } else { 3 }
}
mut penalty := if f.array_init_break[f.array_init_depth - 1] { 0 } else { 3 }
if penalty > 0 {
if i == 0 ||
it.exprs[i - 1] is ast.ArrayInit ||
@ -1517,6 +1521,10 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
}
last_line_nr = line_nr
}
f.array_init_depth--
if f.array_init_depth == 0 {
f.array_init_break = []
}
if inc_indent {
f.indent--
}

View File

@ -38,15 +38,16 @@ b := [[
5,8],[ 5, 1,
3],[ 2, 6, 0]],[
[9,
4,5],[7,2,3],
4,5],[
7,2,3],
[1,
2,3]]]
c := [
[
[2,
5,8],[ 5, 1,
3],[ 2, 6, 0]],[
[9,
3],[ 2, 6, 0]],[[
9,
4,5],[7,2,3],
[1,
2,3]]]