Skip to content

Commit

Permalink
vfmt: fix array_init line wrapping (#14154)
Browse files Browse the repository at this point in the history
  • Loading branch information
stackotter committed Apr 25, 2022
1 parent c819f0f commit 563469e
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 41 deletions.
3 changes: 1 addition & 2 deletions examples/clock/clock.v
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions vlib/crypto/hmac/hmac_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
)

Expand Down
39 changes: 13 additions & 26 deletions vlib/net/http/cookie_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ const (
cookie: [&http.Cookie{
name: 'cookie-1'
value: 'v1'
},
&http.Cookie{
}, &http.Cookie{
name: 'cookie-2'
value: 'v2'
},
Expand Down Expand Up @@ -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: '/'
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -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"'
Expand All @@ -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 "'
Expand All @@ -390,8 +381,7 @@ const (
header: {
'Set-Cookie': ['special-4=" "']
}
cookies: [
&http.Cookie{
cookies: [&http.Cookie{
name: 'special-4'
value: ' '
raw: 'special-4=" "'
Expand All @@ -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'
Expand All @@ -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"'
Expand All @@ -423,8 +411,7 @@ const (
header: {
'Set-Cookie': ['special-7=","']
}
cookies: [
&http.Cookie{
cookies: [&http.Cookie{
name: 'special-7'
value: ','
raw: 'special-8=","'
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 15 additions & 3 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(' ')
Expand Down
3 changes: 1 addition & 2 deletions vlib/v/fmt/tests/array_init_comment_ending_keep.vv
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ const (
header: {
'Set-Cookie': ['special-7=","']
}
cookies: [
&http.Cookie{
cookies: [&http.Cookie{
name: 'special-7'
value: ','
raw: 'special-8=","'
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/fmt/tests/array_init_expected.vv
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/fmt/tests/array_init_input.vv
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions vlib/v/gen/c/match.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/js/js.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 563469e

Please sign in to comment.