fmt: array_init: use line breaks of first elements in every hierarchy (#5777)
							parent
							
								
									14d83c8457
								
							
						
					
					
						commit
						194ecda829
					
				| 
						 | 
					@ -37,6 +37,8 @@ pub mut:
 | 
				
			||||||
	penalties         []int // how hard should it be to break line after each expression
 | 
						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
 | 
						precedences       []int // operator/parenthese precedences for operator at end of each expression
 | 
				
			||||||
	par_level         int // how many parentheses are put around the current 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
 | 
						single_line_if    bool
 | 
				
			||||||
	cur_mod           string
 | 
						cur_mod           string
 | 
				
			||||||
	file              ast.File
 | 
						file              ast.File
 | 
				
			||||||
| 
						 | 
					@ -1479,13 +1481,15 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
 | 
				
			||||||
	f.write('[')
 | 
						f.write('[')
 | 
				
			||||||
	mut inc_indent := false
 | 
						mut inc_indent := false
 | 
				
			||||||
	mut last_line_nr := it.pos.line_nr // to have the same newlines between array elements
 | 
						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 {
 | 
						for i, expr in it.exprs {
 | 
				
			||||||
		line_nr := expr.position().line_nr
 | 
							line_nr := expr.position().line_nr
 | 
				
			||||||
		if i == 0 && last_line_nr < line_nr {
 | 
							if i == 0 {
 | 
				
			||||||
			 break_after_each_element = true
 | 
								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 penalty > 0 {
 | 
				
			||||||
			if i == 0 ||
 | 
								if i == 0 ||
 | 
				
			||||||
				it.exprs[i - 1] is ast.ArrayInit ||
 | 
									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
 | 
							last_line_nr = line_nr
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						f.array_init_depth--
 | 
				
			||||||
 | 
						if f.array_init_depth == 0 {
 | 
				
			||||||
 | 
							f.array_init_break = []
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	if inc_indent {
 | 
						if inc_indent {
 | 
				
			||||||
		f.indent--
 | 
							f.indent--
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -38,15 +38,16 @@ b := [[
 | 
				
			||||||
5,8],[   5,  1,
 | 
					5,8],[   5,  1,
 | 
				
			||||||
3],[ 2, 6, 0]],[
 | 
					3],[ 2, 6, 0]],[
 | 
				
			||||||
[9,
 | 
					[9,
 | 
				
			||||||
4,5],[7,2,3],
 | 
					4,5],[
 | 
				
			||||||
 | 
					7,2,3],
 | 
				
			||||||
[1,
 | 
					[1,
 | 
				
			||||||
2,3]]]
 | 
					2,3]]]
 | 
				
			||||||
c := [
 | 
					c := [
 | 
				
			||||||
[
 | 
					[
 | 
				
			||||||
[2,
 | 
					[2,
 | 
				
			||||||
5,8],[   5,  1,
 | 
					5,8],[   5,  1,
 | 
				
			||||||
3],[ 2, 6, 0]],[
 | 
					3],[ 2, 6, 0]],[[
 | 
				
			||||||
[9,
 | 
					9,
 | 
				
			||||||
4,5],[7,2,3],
 | 
					4,5],[7,2,3],
 | 
				
			||||||
[1,
 | 
					[1,
 | 
				
			||||||
2,3]]]
 | 
					2,3]]]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue