From 563469ed9f26276a6e6b575f0e1f71dfc6f30461 Mon Sep 17 00:00:00 2001 From: stackotter Date: Mon, 25 Apr 2022 15:11:44 +1000 Subject: [PATCH] vfmt: fix array_init line wrapping (#14154) --- examples/clock/clock.v | 3 +- vlib/crypto/hmac/hmac_test.v | 3 +- vlib/net/http/cookie_test.v | 39 +++++++------------ vlib/v/checker/checker.v | 4 +- vlib/v/fmt/fmt.v | 18 +++++++-- .../tests/array_init_comment_ending_keep.vv | 3 +- vlib/v/fmt/tests/array_init_expected.vv | 7 ++++ vlib/v/fmt/tests/array_init_input.vv | 4 ++ vlib/v/gen/c/match.v | 3 +- vlib/v/gen/js/js.v | 4 +- 10 files changed, 47 insertions(+), 41 deletions(-) diff --git a/examples/clock/clock.v b/examples/clock/clock.v index d80416e7ef..ded4350734 100644 --- a/examples/clock/clock.v +++ b/examples/clock/clock.v @@ -32,8 +32,7 @@ const ( struct App { minutes_tic []f32 = [f32(center - tw), tp, center + tw, tp, center + tw, tp, center + tw, - tp + - 1 * th, center - tw, tp + 1 * th] + tp + 1 * th, center - tw, tp + 1 * th] hours_tic []f32 = [f32(center - tw), tp, center + tw, tp, center + tw, tp, center + tw, tp + 2 * th, center - tw, tp + 2 * th] hours3_tic []f32 = [f32(center - tw), tp, center + tw, tp, center + tw, tp, center + tw, tp + 3 * th, diff --git a/vlib/crypto/hmac/hmac_test.v b/vlib/crypto/hmac/hmac_test.v index 31c31a6a8b..010757faa5 100644 --- a/vlib/crypto/hmac/hmac_test.v +++ b/vlib/crypto/hmac/hmac_test.v @@ -51,8 +51,7 @@ const ( 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd], - 'Test With Truncation'.bytes(), - 'Test Using Larger Than Block-Size Key - Hash Key First'.bytes(), + 'Test With Truncation'.bytes(), 'Test Using Larger Than Block-Size Key - Hash Key First'.bytes(), 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data'.bytes()] ) diff --git a/vlib/net/http/cookie_test.v b/vlib/net/http/cookie_test.v index 591017b7c1..4100f9d81c 100644 --- a/vlib/net/http/cookie_test.v +++ b/vlib/net/http/cookie_test.v @@ -242,8 +242,7 @@ const ( cookie: [&http.Cookie{ name: 'cookie-1' value: 'v1' - }, - &http.Cookie{ + }, &http.Cookie{ name: 'cookie-2' value: 'v2' }, @@ -294,8 +293,7 @@ const ( header: { 'Set-Cookie': ['ASP.NET_SessionId=foo; path=/; HttpOnly'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'ASP.NET_SessionId' value: 'foo' path: '/' @@ -307,8 +305,7 @@ const ( header: { 'Set-Cookie': ['samesitedefault=foo; SameSite'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'samesitedefault' value: 'foo' same_site: .same_site_default_mode @@ -319,8 +316,7 @@ const ( header: { 'Set-Cookie': ['samesitelax=foo; SameSite=Lax'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'samesitelax' value: 'foo' same_site: .same_site_lax_mode @@ -331,8 +327,7 @@ const ( header: { 'Set-Cookie': ['samesitestrict=foo; SameSite=Strict'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'samesitestrict' value: 'foo' same_site: .same_site_strict_mode @@ -343,8 +338,7 @@ const ( header: { 'Set-Cookie': ['samesitenone=foo; SameSite=None'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'samesitenone' value: 'foo' same_site: .same_site_none_mode @@ -357,8 +351,7 @@ const ( header: { 'Set-Cookie': ['special-1=a z'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'special-1' value: 'a z' raw: 'special-1=a z' @@ -368,8 +361,7 @@ const ( header: { 'Set-Cookie': ['special-2=" z"'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'special-2' value: ' z' raw: 'special-2=" z"' @@ -379,8 +371,7 @@ const ( header: { 'Set-Cookie': ['special-3="a "'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'special-3' value: 'a ' raw: 'special-3="a "' @@ -390,8 +381,7 @@ const ( header: { 'Set-Cookie': ['special-4=" "'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'special-4' value: ' ' raw: 'special-4=" "' @@ -401,8 +391,7 @@ const ( header: { 'Set-Cookie': ['special-5=a,z'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'special-5' value: 'a,z' raw: 'special-5=a,z' @@ -412,8 +401,7 @@ const ( header: { 'Set-Cookie': ['special-6=",z"'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'special-6' value: ',z' raw: 'special-6=",z"' @@ -423,8 +411,7 @@ const ( header: { 'Set-Cookie': ['special-7=","'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'special-7' value: ',' raw: 'special-8=","' diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 227d826147..8a572e375b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3683,8 +3683,8 @@ pub fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type { for mut expr is ast.ParExpr { expr = expr.expr } - if expr in [ast.BoolLiteral, ast.CallExpr, ast.CharLiteral, ast.FloatLiteral, - ast.IntegerLiteral, ast.InfixExpr, ast.StringLiteral, ast.StringInterLiteral] { + if expr in [ast.BoolLiteral, ast.CallExpr, ast.CharLiteral, ast.FloatLiteral, ast.IntegerLiteral, + ast.InfixExpr, ast.StringLiteral, ast.StringInterLiteral] { c.error('cannot take the address of $expr', node.pos) } if mut node.right is ast.IndexExpr { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index c19e4a3f55..fe35d39191 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1462,7 +1462,7 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) { || f.line_len + expr.pos().len > fmt.max_len[3] } } - line_break := f.array_init_break[f.array_init_depth - 1] + mut line_break := f.array_init_break[f.array_init_depth - 1] mut penalty := if line_break { 0 } else { 4 } if penalty > 0 { if i == 0 @@ -1480,14 +1480,26 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) { } single_line_expr := expr_is_single_line(expr) if single_line_expr { - estr := f.node_str(expr) - if !is_new_line && !f.buffering && f.line_len + estr.len > fmt.max_len.last() { + mut estr := '' + if !is_new_line && !f.buffering && f.line_len + expr.pos().len > fmt.max_len.last() { + if inc_indent { + estr = f.node_str(expr) + } f.writeln('') is_new_line = true if !inc_indent { f.indent++ inc_indent = true + f.write_indent() + f.empty_line = false + estr = f.node_str(expr) } + if i == 0 { + f.array_init_break[f.array_init_depth - 1] = true + line_break = true + } + } else { + estr = f.node_str(expr) } if !is_new_line && i > 0 { f.write(' ') diff --git a/vlib/v/fmt/tests/array_init_comment_ending_keep.vv b/vlib/v/fmt/tests/array_init_comment_ending_keep.vv index d4f4fd7a16..7758d48761 100644 --- a/vlib/v/fmt/tests/array_init_comment_ending_keep.vv +++ b/vlib/v/fmt/tests/array_init_comment_ending_keep.vv @@ -6,8 +6,7 @@ const ( header: { 'Set-Cookie': ['special-7=","'] } - cookies: [ - &http.Cookie{ + cookies: [&http.Cookie{ name: 'special-7' value: ',' raw: 'special-8=","' diff --git a/vlib/v/fmt/tests/array_init_expected.vv b/vlib/v/fmt/tests/array_init_expected.vv index 47c28b9eec..90979c7d45 100644 --- a/vlib/v/fmt/tests/array_init_expected.vv +++ b/vlib/v/fmt/tests/array_init_expected.vv @@ -12,6 +12,13 @@ fn wrapping_tests() { 'elit. Donec varius purus leo, vel maximus diam', 'finibus sed. Etiam eu urna ante. Nunc quis vehicula', 'velit. Sed at mauris et quam ornare tristique.'] + multi_level := [ + [1, 2, 3], + [ + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec varius purus leo, vel maximus diam', + 'finibus sed. Etiam eu urna ante. Nunc quis vehicula velit. Sed at mauris et quam ornare tristique.', + ], + ] } fn array_init_without_commas() { diff --git a/vlib/v/fmt/tests/array_init_input.vv b/vlib/v/fmt/tests/array_init_input.vv index 4b465d7cd8..7d33d48f9a 100644 --- a/vlib/v/fmt/tests/array_init_input.vv +++ b/vlib/v/fmt/tests/array_init_input.vv @@ -9,6 +9,10 @@ fn main() { fn wrapping_tests() { my_arr := ['Lorem ipsum dolor sit amet, consectetur adipiscing', 'elit. Donec varius purus leo, vel maximus diam', 'finibus sed. Etiam eu urna ante. Nunc quis vehicula', 'velit. Sed at mauris et quam ornare tristique.'] + multi_level := [ + [1, 2, 3], + ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec varius purus leo, vel maximus diam', 'finibus sed. Etiam eu urna ante. Nunc quis vehicula velit. Sed at mauris et quam ornare tristique.'], + ] } fn array_init_without_commas() { diff --git a/vlib/v/gen/c/match.v b/vlib/v/gen/c/match.v index 37e4613f21..9ff991062c 100644 --- a/vlib/v/gen/c/match.v +++ b/vlib/v/gen/c/match.v @@ -61,8 +61,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { } g.inside_match_optional = true } - if node.cond in [ast.Ident, ast.SelectorExpr, ast.IntegerLiteral, ast.StringLiteral, - ast.FloatLiteral] { + if node.cond in [ast.Ident, ast.SelectorExpr, ast.IntegerLiteral, ast.StringLiteral, ast.FloatLiteral] { cond_var = g.expr_string(node.cond) } else { line := if is_expr { diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 9ad09b351c..74baccffc6 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -2426,8 +2426,8 @@ fn (mut g JsGen) match_expr(node ast.MatchExpr) { g.inside_ternary = true } - if node.cond in [ast.Ident, ast.SelectorExpr, ast.IntegerLiteral, ast.StringLiteral, - ast.FloatLiteral, ast.CallExpr, ast.EnumVal] { + if node.cond in [ast.Ident, ast.SelectorExpr, ast.IntegerLiteral, ast.StringLiteral, ast.FloatLiteral, + ast.CallExpr, ast.EnumVal] { cond_var = CondExpr{node.cond} } else { s := g.new_tmp_var()