fmt: format array elements line-by-line when nl after `[` (#5776)

pull/5777/head
Uwe Krüger 2020-07-09 19:47:12 +02:00 committed by GitHub
parent 31ac20876c
commit e47ad33af6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 16 deletions

View File

@ -1479,12 +1479,14 @@ 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
for i, expr in it.exprs {
mut penalty := 3
line_nr := expr.position().line_nr
if last_line_nr < line_nr {
penalty--
if i == 0 && last_line_nr < line_nr {
break_after_each_element = true
}
mut penalty := if break_after_each_element { 0 } else { 3 }
if penalty > 0 {
if i == 0 ||
it.exprs[i - 1] is ast.ArrayInit ||
it.exprs[i - 1] is ast.StructInit ||
@ -1496,6 +1498,7 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) {
expr is ast.CallExpr {
penalty--
}
}
is_new_line := f.wrap_long_line(penalty, !inc_indent)
if is_new_line && !inc_indent {
f.indent++

View File

@ -13,6 +13,12 @@ const (
one_line_supported = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd',
'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos']
another_const = ['a', 'b', 'c', 'd', 'e', 'f']
multiline_const = [
'first line',
'second line',
'third line',
'fourth line'
]
)
const (
@ -24,3 +30,31 @@ const (
pub const (
i_am_pub_const = true
)
fn main() {
a := [
[3, 5, 6],
[7, 9, 2]
]
b := [[
[2, 5, 8],
[5, 1, 3],
[2, 6, 0]
], [
[9, 4, 5],
[7, 2, 3],
[1, 2, 3]
]]
c := [
[
[2, 5, 8],
[5, 1, 3],
[2, 6, 0]
],
[
[9, 4, 5],
[7, 2, 3],
[1, 2, 3]
]
]
}

View File

@ -11,11 +11,13 @@ eulers=2.7182
supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd',
'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos']
one_line_supported = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd', 'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos']
another_const = [
'a', 'b'
another_const = ['a', 'b'
'c', 'd', 'e'
'f'
]
multiline_const = [
'first line', 'second line','third line',
'fourth line']
)
const (
@ -26,4 +28,26 @@ pub const (
i_am_pub_const=true
)
fn main() {
a := [
[3,
5,
6],[7, 9, 2]]
b := [[
[2,
5,8],[ 5, 1,
3],[ 2, 6, 0]],[
[9,
4,5],[7,2,3],
[1,
2,3]]]
c := [
[
[2,
5,8],[ 5, 1,
3],[ 2, 6, 0]],[
[9,
4,5],[7,2,3],
[1,
2,3]]]
}