Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: use fixed size array for max_len const #21140

Merged
merged 3 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import v.pref
const bs = '\\'

// when to break a line depending on the penalty
const max_len = [0, 35, 60, 85, 93, 100]
const max_len = [0, 35, 60, 85, 93, 100]!

@[minify]
pub struct Fmt {
Expand Down Expand Up @@ -1696,7 +1696,7 @@ pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) {
for variant in variants {
// 3 = length of ' = ' or ' | '
line_length += 3 + variant.name.len
if line_length > fmt.max_len.last() || (variant.id != node.variants.len - 1
if line_length > fmt.max_len[fmt.max_len.len - 1] || (variant.id != node.variants.len - 1
&& node.variants[variant.id].end_comments.len > 0) {
separator = '\n\t| '
is_multiline = true
Expand Down Expand Up @@ -1825,7 +1825,7 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
single_line_expr := expr_is_single_line(expr)
if single_line_expr {
mut estr := ''
if !is_new_line && !f.buffering && f.line_len + expr.pos().len > fmt.max_len.last() {
if !is_new_line && !f.buffering && f.line_len + expr.pos().len > fmt.max_len[fmt.max_len.len - 1] {
if inc_indent {
estr = f.node_str(expr)
}
Expand Down Expand Up @@ -2379,7 +2379,7 @@ pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
}
// When a single line if is really long, write it again as multiline,
// except it is part of an InfixExpr.
if is_ternary && f.line_len > fmt.max_len.last() && !f.buffering {
if is_ternary && f.line_len > fmt.max_len[fmt.max_len.len - 1] && !f.buffering {
is_ternary = false
f.single_line_if = false
f.out.go_back_to(start_pos)
Expand Down Expand Up @@ -2504,7 +2504,7 @@ pub fn (mut f Fmt) infix_expr(node ast.InfixExpr) {
}
if !buffering_save && f.buffering {
f.buffering = false
if !f.single_line_if && f.line_len > fmt.max_len.last() {
if !f.single_line_if && f.line_len > fmt.max_len[fmt.max_len.len - 1] {
is_cond := node.op in [.and, .logical_or]
f.wrap_infix(start_pos, start_len, is_cond)
}
Expand Down Expand Up @@ -2587,7 +2587,7 @@ fn (mut f Fmt) write_splitted_infix(conditions []string, penalties []int, ignore
f.write(c)
continue
}
if final_len > fmt.max_len.last() && is_paren_expr {
if final_len > fmt.max_len[fmt.max_len.len - 1] && is_paren_expr {
conds, pens := split_up_infix(c, true, is_cond)
f.write_splitted_infix(conds, pens, true, is_cond)
continue
Expand Down Expand Up @@ -2827,7 +2827,7 @@ pub fn (mut f Fmt) or_expr(node ast.OrExpr) {
// so, since this'll all be on one line, trim any possible whitespace
str := f.node_str(node.stmts[0]).trim_space()
single_line := ' or { ${str} }'
if single_line.len + f.line_len <= fmt.max_len.last() {
if single_line.len + f.line_len <= fmt.max_len[fmt.max_len.len - 1] {
f.write(single_line)
return
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/fmt/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
if single_line_fields && (init_field.comments.len > 0
|| init_field.next_comments.len > 0
|| !expr_is_single_line(init_field.expr)
|| f.line_len > max_len.last()) {
|| f.line_len > fmt.max_len[fmt.max_len.len - 1]) {
spytheman marked this conversation as resolved.
Show resolved Hide resolved
single_line_fields = false
f.out.go_back_to(fields_start)
f.line_len = fields_start
Expand Down
Loading