From e47ad33af664e940a14f5e3e634a1af9c84a9f85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Thu, 9 Jul 2020 19:47:12 +0200 Subject: [PATCH] fmt: format array elements line-by-line when nl after `[` (#5776) --- vlib/v/fmt/fmt.v | 29 +++++++++++++----------- vlib/v/fmt/tests/consts_expected.vv | 34 +++++++++++++++++++++++++++++ vlib/v/fmt/tests/consts_input.vv | 30 ++++++++++++++++++++++--- 3 files changed, 77 insertions(+), 16 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 36edb8ca63..e3dbdb9670 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1479,22 +1479,25 @@ 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 } - 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 { - penalty-- - } - if expr is ast.ArrayInit || - expr is ast.StructInit || expr is ast.MapInit || - expr is ast.CallExpr { - penalty-- + 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 || + it.exprs[i - 1] is ast.MapInit || it.exprs[i - 1] is ast.CallExpr { + penalty-- + } + if expr is ast.ArrayInit || + expr is ast.StructInit || expr is ast.MapInit || + expr is ast.CallExpr { + penalty-- + } } is_new_line := f.wrap_long_line(penalty, !inc_indent) if is_new_line && !inc_indent { diff --git a/vlib/v/fmt/tests/consts_expected.vv b/vlib/v/fmt/tests/consts_expected.vv index d31ee98b5a..c4dedffa51 100644 --- a/vlib/v/fmt/tests/consts_expected.vv +++ b/vlib/v/fmt/tests/consts_expected.vv @@ -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] + ] + ] +} diff --git a/vlib/v/fmt/tests/consts_input.vv b/vlib/v/fmt/tests/consts_input.vv index e54bf59c4a..481bd49fe4 100644 --- a/vlib/v/fmt/tests/consts_input.vv +++ b/vlib/v/fmt/tests/consts_input.vv @@ -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]]] +}