diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index e3dbdb9670..b5e75ffa7c 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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-- } diff --git a/vlib/v/fmt/tests/consts_input.vv b/vlib/v/fmt/tests/consts_input.vv index 481bd49fe4..26f30b8393 100644 --- a/vlib/v/fmt/tests/consts_input.vv +++ b/vlib/v/fmt/tests/consts_input.vv @@ -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]]]