Skip to content

Commit c0843af

Browse files
authored
fmt: refactor, fix typos (#18392)
1 parent 3e5f254 commit c0843af

11 files changed

Lines changed: 131 additions & 121 deletions

vlib/v/fmt/comments.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
3131
}
3232
defer {
3333
// ensure that the `vfmt off` comment itself was sent to the output,
34-
// by defering the check for that state transition:
34+
// by deferring the check for that state transition:
3535
if node.text.starts_with('\x01 vfmt off') {
3636
f.vfmt_off(node.pos.line_nr)
3737
}

vlib/v/fmt/fmt.v

Lines changed: 76 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -391,46 +391,47 @@ fn (f Fmt) should_insert_newline_before_node(node ast.Node, prev_node ast.Node)
391391
prev_line_nr := prev_node.pos().last_line
392392
// The nodes are Stmts
393393
if node is ast.Stmt && prev_node is ast.Stmt {
394-
stmt := node
395-
prev_stmt := prev_node
396-
// Force a newline after a block of HashStmts
397-
if prev_stmt is ast.HashStmt && stmt !in [ast.HashStmt, ast.ExprStmt] {
398-
return true
399-
}
400-
// Force a newline after function declarations
401-
// The only exception is inside an block of no_body functions
402-
if prev_stmt is ast.FnDecl {
403-
if stmt !is ast.FnDecl || !prev_stmt.no_body {
394+
match prev_node {
395+
// Force a newline after a block of HashStmts
396+
ast.HashStmt {
397+
if node !in [ast.HashStmt, ast.ExprStmt] {
398+
return true
399+
}
400+
}
401+
// Force a newline after function declarations
402+
// The only exception is inside a block of no_body functions
403+
ast.FnDecl {
404+
if node !is ast.FnDecl || !prev_node.no_body {
405+
return true
406+
}
407+
}
408+
// Force a newline after struct declarations
409+
ast.StructDecl {
404410
return true
405411
}
406-
}
407-
// Force a newline after struct declarations
408-
if prev_stmt is ast.StructDecl {
409-
return true
410-
}
411-
// Empty line after an block of type declarations
412-
if prev_stmt is ast.TypeDecl && stmt !is ast.TypeDecl {
413-
return true
414-
}
415-
// Imports are handled special hence they are ignored here
416-
if stmt is ast.Import || prev_stmt is ast.Import {
417-
return false
418-
}
419-
// Attributes are not respected in the stmts position, so this requires manual checking
420-
if stmt is ast.StructDecl {
421-
if stmt.attrs.len > 0 && stmt.attrs[0].pos.line_nr - prev_line_nr <= 1 {
422-
return false
412+
// Empty line after a block of type declarations
413+
ast.TypeDecl {
414+
if node !is ast.TypeDecl {
415+
return true
416+
}
423417
}
424-
}
425-
if stmt is ast.EnumDecl {
426-
if stmt.attrs.len > 0 && stmt.attrs[0].pos.line_nr - prev_line_nr <= 1 {
418+
// Imports are handled special hence they are ignored here
419+
ast.Import {
427420
return false
428421
}
422+
else {}
429423
}
430-
if stmt is ast.FnDecl {
431-
if stmt.attrs.len > 0 && stmt.attrs[0].pos.line_nr - prev_line_nr <= 1 {
424+
match node {
425+
// Attributes are not respected in the stmts position, so this requires manual checking
426+
ast.StructDecl, ast.EnumDecl, ast.FnDecl {
427+
if node.attrs.len > 0 && node.attrs[0].pos.line_nr - prev_line_nr <= 1 {
428+
return false
429+
}
430+
}
431+
ast.Import {
432432
return false
433433
}
434+
else {}
434435
}
435436
}
436437
// The node shouldn't have a newline before
@@ -800,8 +801,8 @@ fn expr_is_single_line(expr ast.Expr) bool {
800801
pub fn (mut f Fmt) assert_stmt(node ast.AssertStmt) {
801802
f.write('assert ')
802803
mut expr := node.expr
803-
for expr is ast.ParExpr {
804-
expr = (expr as ast.ParExpr).expr
804+
for mut expr is ast.ParExpr {
805+
expr = expr.expr
805806
}
806807
f.expr(expr)
807808
if node.extra !is ast.EmptyExpr {
@@ -1288,7 +1289,7 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
12881289
}
12891290

12901291
pub fn (mut f Fmt) interface_field(field ast.StructField) {
1291-
mut ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias))
1292+
ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias))
12921293
end_pos := field.pos.pos + field.pos.len
12931294
before_comments := field.comments.filter(it.pos.pos < field.pos.pos)
12941295
between_comments := field.comments[before_comments.len..].filter(it.pos.pos < end_pos)
@@ -1823,7 +1824,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
18231824
} else if node.language != .v {
18241825
f.write('${node.name.after_char(`.`)}')
18251826
} else {
1826-
mut name := f.short_module(node.name)
1827+
name := f.short_module(node.name)
18271828
f.mark_import_as_used(name)
18281829
f.write('${name}')
18291830
}
@@ -1951,36 +1952,42 @@ pub fn (mut f Fmt) comptime_call(node ast.ComptimeCall) {
19511952
f.write('\$tmpl(${node.args[0].expr})')
19521953
}
19531954
} else {
1954-
if node.is_embed {
1955-
if node.embed_file.compression_type == 'none' {
1956-
f.write('\$embed_file(${node.args[0].expr})')
1957-
} else {
1958-
f.write('\$embed_file(${node.args[0].expr}, .${node.embed_file.compression_type})')
1959-
}
1960-
} else if node.is_env {
1961-
f.write("\$env('${node.args_var}')")
1962-
} else if node.is_pkgconfig {
1963-
f.write("\$pkgconfig('${node.args_var}')")
1964-
} else if node.method_name in ['compile_error', 'compile_warn'] {
1965-
f.write("\$${node.method_name}('${node.args_var}')")
1966-
} else {
1967-
inner_args := if node.args_var != '' {
1968-
node.args_var
1969-
} else {
1970-
node.args.map(if it.expr is ast.ArrayDecompose {
1971-
'...${it.expr.expr.str()}'
1955+
match true {
1956+
node.is_embed {
1957+
if node.embed_file.compression_type == 'none' {
1958+
f.write('\$embed_file(${node.args[0].expr})')
19721959
} else {
1973-
it.str()
1974-
}).join(', ')
1960+
f.write('\$embed_file(${node.args[0].expr}, .${node.embed_file.compression_type})')
1961+
}
19751962
}
1976-
method_expr := if node.has_parens {
1977-
'(${node.method_name}(${inner_args}))'
1978-
} else {
1979-
'${node.method_name}(${inner_args})'
1963+
node.is_env {
1964+
f.write("\$env('${node.args_var}')")
1965+
}
1966+
node.is_pkgconfig {
1967+
f.write("\$pkgconfig('${node.args_var}')")
1968+
}
1969+
node.method_name in ['compile_error', 'compile_warn'] {
1970+
f.write("\$${node.method_name}('${node.args_var}')")
1971+
}
1972+
else {
1973+
inner_args := if node.args_var != '' {
1974+
node.args_var
1975+
} else {
1976+
node.args.map(if it.expr is ast.ArrayDecompose {
1977+
'...${it.expr.expr.str()}'
1978+
} else {
1979+
it.str()
1980+
}).join(', ')
1981+
}
1982+
method_expr := if node.has_parens {
1983+
'(${node.method_name}(${inner_args}))'
1984+
} else {
1985+
'${node.method_name}(${inner_args})'
1986+
}
1987+
f.expr(node.left)
1988+
f.write('.$${method_expr}')
1989+
f.or_expr(node.or_block)
19801990
}
1981-
f.expr(node.left)
1982-
f.write('.$${method_expr}')
1983-
f.or_expr(node.or_block)
19841991
}
19851992
}
19861993
}
@@ -2628,14 +2635,12 @@ pub fn (mut f Fmt) prefix_expr(node ast.PrefixExpr) {
26282635
if node.right.expr.op in [.key_in, .not_in, .key_is, .not_is]
26292636
&& node.right.expr.right !is ast.InfixExpr {
26302637
f.expr(node.right.expr.left)
2631-
if node.right.expr.op == .key_in {
2632-
f.write(' !in ')
2633-
} else if node.right.expr.op == .not_in {
2634-
f.write(' in ')
2635-
} else if node.right.expr.op == .key_is {
2636-
f.write(' !is ')
2637-
} else if node.right.expr.op == .not_is {
2638-
f.write(' is ')
2638+
match node.right.expr.op {
2639+
.key_in { f.write(' !in ') }
2640+
.not_in { f.write(' in ') }
2641+
.key_is { f.write(' !is ') }
2642+
.not_is { f.write(' is ') }
2643+
else {}
26392644
}
26402645
f.expr(node.right.expr.right)
26412646
return

vlib/v/fmt/struct.v

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -79,39 +79,46 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
7979
mut default_expr_align_i := 0
8080
mut inc_indent := false // for correct indents with multi line default exprs
8181
for i, field in node.fields {
82-
if i == node.mut_pos {
83-
f.writeln('mut:')
84-
} else if i == node.pub_pos {
85-
f.writeln('pub:')
86-
} else if i == node.pub_mut_pos {
87-
f.writeln('pub mut:')
88-
} else if i == node.global_pos {
89-
f.writeln('__global:')
90-
} else if i == node.module_pos {
91-
f.writeln('module:')
92-
} else if i > 0 {
93-
// keep one empty line between fields (exclude one after mut:, pub:, ...)
94-
mut before_last_line := node.fields[i - 1].pos.line_nr
95-
if node.fields[i - 1].comments.len > 0 {
96-
if before_last_line < node.fields[i - 1].comments.last().pos.last_line {
97-
before_last_line = node.fields[i - 1].comments.last().pos.last_line
98-
}
82+
match true {
83+
i == node.mut_pos {
84+
f.writeln('mut:')
9985
}
100-
if node.fields[i - 1].has_default_expr {
101-
if before_last_line < node.fields[i - 1].default_expr.pos().last_line {
102-
before_last_line = node.fields[i - 1].default_expr.pos().last_line
103-
}
86+
i == node.pub_pos {
87+
f.writeln('pub:')
88+
}
89+
i == node.pub_mut_pos {
90+
f.writeln('pub mut:')
91+
}
92+
i == node.global_pos {
93+
f.writeln('__global:')
10494
}
95+
i == node.module_pos {
96+
f.writeln('module:')
97+
}
98+
i > 0 {
99+
// keep one empty line between fields (exclude one after mut:, pub:, ...)
100+
last_field := node.fields[i - 1]
101+
before_last_line := if last_field.comments.len > 0
102+
&& last_field.pos.line_nr < last_field.comments.last().pos.last_line {
103+
last_field.comments.last().pos.last_line
104+
} else if last_field.has_default_expr {
105+
last_field.default_expr.pos().last_line
106+
} else {
107+
last_field.pos.line_nr
108+
}
105109

106-
mut next_first_line := field.pos.line_nr
107-
if field.comments.len > 0 {
108-
if next_first_line > field.comments[0].pos.line_nr {
109-
next_first_line = field.comments[0].pos.line_nr
110+
next_first_line := if field.comments.len > 0
111+
&& field.pos.line_nr > field.comments[0].pos.line_nr {
112+
field.comments[0].pos.line_nr
113+
} else {
114+
field.pos.line_nr
115+
}
116+
117+
if next_first_line - before_last_line > 1 {
118+
f.writeln('')
110119
}
111120
}
112-
if next_first_line - before_last_line > 1 {
113-
f.writeln('')
114-
}
121+
else {}
115122
}
116123
end_pos := field.pos.pos + field.pos.len
117124
before_comments := field.comments.filter(it.pos.pos < field.pos.pos)
@@ -125,11 +132,10 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
125132
before_len := f.line_len
126133
f.comments(between_comments, iembed: true, has_nl: false)
127134
comments_len := f.line_len - before_len
128-
mut field_align := field_aligns[field_align_i]
129-
if field_align.line_nr < field.pos.line_nr {
135+
if field_aligns[field_align_i].line_nr < field.pos.line_nr {
130136
field_align_i++
131-
field_align = field_aligns[field_align_i]
132137
}
138+
field_align := field_aligns[field_align_i]
133139
f.write(strings.repeat(` `, field_align.max_len - field.name.len - comments_len))
134140
// Handle anon structs recursively
135141
if !f.write_anon_struct_field_decl(field.typ, field.anon_struct_decl) {
@@ -143,11 +149,10 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
143149
f.single_line_attrs(field.attrs, inline: true)
144150
}
145151
if field.has_default_expr {
146-
mut align := default_expr_aligns[default_expr_align_i]
147-
if align.line_nr < field.pos.line_nr {
152+
if default_expr_aligns[default_expr_align_i].line_nr < field.pos.line_nr {
148153
default_expr_align_i++
149-
align = default_expr_aligns[default_expr_align_i]
150154
}
155+
align := default_expr_aligns[default_expr_align_i]
151156
pad_len := align.max_len - attrs_len + align.max_type_len - field_types[i].len
152157
f.write(strings.repeat(` `, pad_len))
153158
f.write(' = ')
@@ -167,11 +172,10 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
167172
f.writeln('')
168173
} else {
169174
if !field.has_default_expr {
170-
mut align := comment_aligns[comment_align_i]
171-
if align.line_nr < field.pos.line_nr {
175+
if comment_aligns[comment_align_i].line_nr < field.pos.line_nr {
172176
comment_align_i++
173-
align = comment_aligns[comment_align_i]
174177
}
178+
align := comment_aligns[comment_align_i]
175179
pad_len := align.max_len - attrs_len + align.max_type_len - field_types[i].len
176180
f.write(strings.repeat(` `, pad_len))
177181
}
@@ -238,11 +242,12 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
238242
f.is_struct_init = struct_init_save
239243
}
240244
f.mark_types_import_as_used(node.typ)
241-
type_sym := f.table.sym(node.typ)
245+
sym_name := f.table.sym(node.typ).name
242246
// f.write('<old name: $type_sym.name>')
243-
mut name := type_sym.name
244-
if !name.starts_with('C.') && !name.starts_with('JS.') {
245-
name = f.no_cur_mod(f.short_module(type_sym.name)) // TODO f.type_to_str?
247+
mut name := if !sym_name.starts_with('C.') && !sym_name.starts_with('JS.') {
248+
f.no_cur_mod(f.short_module(sym_name)) // TODO f.type_to_str?
249+
} else {
250+
sym_name
246251
}
247252
if name == 'void' {
248253
name = ''

vlib/v/fmt/tests/anon_fn_as_param_keep.vv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn test_anon_fn_void(func fn ()) int {
1414

1515
fn C.HasAnonFnWithNamedParams(cb fn (c C.bar, d voidptr))
1616

17-
// NB: the signature of both anonymus functions should only differs in the param name
17+
// NB: the signature of both anonymous functions should only differs in the param name
1818
fn anon_fn_param_has_no_name(f fn (int) string) {}
1919

2020
fn anon_fn_with_named_param(func fn (a int) string) {}

vlib/v/fmt/tests/attrs_keep.vv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
struct AttrsWithEscpaedStringArgs {
1+
struct AttrsWithEscapedStringArgs {
22
dollar string [foo: '\$var']
33
double_bs string [bar: '\\baz']
44
}

vlib/v/fmt/tests/consts_with_embeded_comments_expected.vv renamed to vlib/v/fmt/tests/consts_with_embedded_comments_expected.vv

File renamed without changes.

vlib/v/fmt/tests/consts_with_embeded_comments_input.vv renamed to vlib/v/fmt/tests/consts_with_embedded_comments_input.vv

File renamed without changes.

vlib/v/fmt/tests/expressions_expected.vv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn string_inter_lit(mut c checker.Checker, mut node ast.StringInterLiteral) tabl
1818
c.error('precision specification only valid for float types', node.fmt_poss[i])
1919
}
2020
if node.pluss[i] && !typ.is_number() {
21-
c.error('plus prefix only allowd for numbers', node.fmt_poss[i])
21+
c.error('plus prefix only allowed for numbers', node.fmt_poss[i])
2222
}
2323
if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`])
2424
|| (typ.is_signed() && fmt !in [`d`, `x`, `X`, `o`, `c`])
@@ -51,7 +51,7 @@ fn main() {
5151
s << ' `${v_str}`'
5252
println(s)
5353
println('this is quite a long string' +
54-
' that is followd by an even longer part that should go to another line')
54+
' that is followed by an even longer part that should go to another line')
5555
if (a == b && b > r) || d > r || a < b || (b < d && a + b > r)
5656
|| (a + b + d >= 0 && r < 0) || (a > b && d - r < b) {
5757
println('ok')

vlib/v/fmt/tests/expressions_input.vv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn string_inter_lit(mut c checker.Checker, mut node ast.StringInterLiteral) tabl
2222
node.fmt_poss[i])
2323
}
2424
if node.pluss[i] && !typ.is_number() {
25-
c.error('plus prefix only allowd for numbers', node.fmt_poss[i])
25+
c.error('plus prefix only allowed for numbers', node.fmt_poss[i])
2626
}
2727
if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) || (typ.is_signed() &&
2828
fmt !in [`d`, `x`, `X`, `o`, `c`]) || (typ.is_int_literal()
@@ -60,7 +60,7 @@ fn main() {
6060
s := []string{}
6161
s << ' `$v_str`'
6262
println(s)
63-
println('this is quite a long string' + ' that is followd by an even longer part that should go to another line')
63+
println('this is quite a long string' + ' that is followed by an even longer part that should go to another line')
6464
if (a == b && b > r) || (d > r) || (a < b) || (b< d && a+b > r) || (a+b+d >= 0 && r < 0) || (a > b && d-r < b) {
6565
println('ok')
6666
}

0 commit comments

Comments
 (0)