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 all commits
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
24 changes: 12 additions & 12 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import v.pref

const bs = '\\'

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

@[minify]
pub struct Fmt {
Expand Down Expand Up @@ -153,7 +153,7 @@ pub fn (mut f Fmt) wrap_long_line(penalty_idx int, add_indent bool) bool {
if f.buffering {
return false
}
if penalty_idx > 0 && f.line_len <= fmt.max_len[penalty_idx] {
if penalty_idx > 0 && f.line_len <= fmt.break_points[penalty_idx] {
return false
}
if f.out.last() == ` ` {
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 || (variant.id != node.variants.len - 1
&& node.variants[variant.id].end_comments.len > 0) {
separator = '\n\t| '
is_multiline = true
Expand Down Expand Up @@ -1803,7 +1803,7 @@ pub fn (mut f Fmt) array_init(node ast.ArrayInit) {
if i == 0 {
if f.array_init_depth > f.array_init_break.len {
f.array_init_break << pos.line_nr > last_line_nr
|| f.line_len + expr.pos().len > fmt.max_len[3]
|| f.line_len + expr.pos().len > fmt.break_points[3]
}
}
mut line_break := f.array_init_break[f.array_init_depth - 1]
Expand All @@ -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 {
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 && !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 {
is_cond := node.op in [.and, .logical_or]
f.wrap_infix(start_pos, start_len, is_cond)
}
Expand Down Expand Up @@ -2574,7 +2574,7 @@ fn (mut f Fmt) write_splitted_infix(conditions []string, penalties []int, ignore
}
for i, cnd in conditions {
c := cnd.trim_space()
if f.line_len + c.len < fmt.max_len[penalties[i]] {
if f.line_len + c.len < fmt.break_points[penalties[i]] {
if (i > 0 && i < conditions.len) || (ignore_paren && i == 0 && c.len > 5 && c[3] == `(`) {
f.write(' ')
}
Expand All @@ -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 && 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 @@ -2728,7 +2728,7 @@ fn (mut f Fmt) match_branch(branch ast.MatchBranch, single_line bool) {
f.is_mbranch_expr = true
for j, expr in branch.exprs {
estr := f.node_str(expr).trim_space()
if f.line_len + estr.len + 2 > fmt.max_len[5] {
if f.line_len + estr.len + 2 > fmt.max_len {
f.remove_new_line()
f.writeln('')
}
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 {
f.write(single_line)
return
}
Expand Down
3 changes: 1 addition & 2 deletions vlib/v/fmt/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,7 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
f.comments(init_field.next_comments, has_nl: true, level: .keep)
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()) {
|| !expr_is_single_line(init_field.expr) || f.line_len > max_len) {
single_line_fields = false
f.out.go_back_to(fields_start)
f.line_len = fields_start
Expand Down