1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fmt: refactor, fix typos (#18392)

This commit is contained in:
Turiiya 2023-06-10 10:32:41 +02:00 committed by GitHub
parent 3e5f2541f2
commit c0843af4f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 132 additions and 122 deletions

View File

@ -31,7 +31,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) {
} }
defer { defer {
// ensure that the `vfmt off` comment itself was sent to the output, // ensure that the `vfmt off` comment itself was sent to the output,
// by defering the check for that state transition: // by deferring the check for that state transition:
if node.text.starts_with('\x01 vfmt off') { if node.text.starts_with('\x01 vfmt off') {
f.vfmt_off(node.pos.line_nr) f.vfmt_off(node.pos.line_nr)
} }

View File

@ -391,46 +391,47 @@ fn (f Fmt) should_insert_newline_before_node(node ast.Node, prev_node ast.Node)
prev_line_nr := prev_node.pos().last_line prev_line_nr := prev_node.pos().last_line
// The nodes are Stmts // The nodes are Stmts
if node is ast.Stmt && prev_node is ast.Stmt { if node is ast.Stmt && prev_node is ast.Stmt {
stmt := node match prev_node {
prev_stmt := prev_node // Force a newline after a block of HashStmts
// Force a newline after a block of HashStmts ast.HashStmt {
if prev_stmt is ast.HashStmt && stmt !in [ast.HashStmt, ast.ExprStmt] { if node !in [ast.HashStmt, ast.ExprStmt] {
return true return true
} }
// Force a newline after function declarations }
// The only exception is inside an block of no_body functions // Force a newline after function declarations
if prev_stmt is ast.FnDecl { // The only exception is inside a block of no_body functions
if stmt !is ast.FnDecl || !prev_stmt.no_body { ast.FnDecl {
if node !is ast.FnDecl || !prev_node.no_body {
return true
}
}
// Force a newline after struct declarations
ast.StructDecl {
return true return true
} }
} // Empty line after a block of type declarations
// Force a newline after struct declarations ast.TypeDecl {
if prev_stmt is ast.StructDecl { if node !is ast.TypeDecl {
return true return true
} }
// Empty line after an block of type declarations }
if prev_stmt is ast.TypeDecl && stmt !is ast.TypeDecl { // Imports are handled special hence they are ignored here
return true ast.Import {
}
// Imports are handled special hence they are ignored here
if stmt is ast.Import || prev_stmt is ast.Import {
return false
}
// Attributes are not respected in the stmts position, so this requires manual checking
if stmt is ast.StructDecl {
if stmt.attrs.len > 0 && stmt.attrs[0].pos.line_nr - prev_line_nr <= 1 {
return false return false
} }
else {}
} }
if stmt is ast.EnumDecl { match node {
if stmt.attrs.len > 0 && stmt.attrs[0].pos.line_nr - prev_line_nr <= 1 { // Attributes are not respected in the stmts position, so this requires manual checking
return false ast.StructDecl, ast.EnumDecl, ast.FnDecl {
} if node.attrs.len > 0 && node.attrs[0].pos.line_nr - prev_line_nr <= 1 {
} return false
if stmt is ast.FnDecl { }
if stmt.attrs.len > 0 && stmt.attrs[0].pos.line_nr - prev_line_nr <= 1 { }
ast.Import {
return false return false
} }
else {}
} }
} }
// The node shouldn't have a newline before // The node shouldn't have a newline before
@ -800,8 +801,8 @@ fn expr_is_single_line(expr ast.Expr) bool {
pub fn (mut f Fmt) assert_stmt(node ast.AssertStmt) { pub fn (mut f Fmt) assert_stmt(node ast.AssertStmt) {
f.write('assert ') f.write('assert ')
mut expr := node.expr mut expr := node.expr
for expr is ast.ParExpr { for mut expr is ast.ParExpr {
expr = (expr as ast.ParExpr).expr expr = expr.expr
} }
f.expr(expr) f.expr(expr)
if node.extra !is ast.EmptyExpr { if node.extra !is ast.EmptyExpr {
@ -1288,7 +1289,7 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
} }
pub fn (mut f Fmt) interface_field(field ast.StructField) { pub fn (mut f Fmt) interface_field(field ast.StructField) {
mut ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias)) ft := f.no_cur_mod(f.table.type_to_str_using_aliases(field.typ, f.mod2alias))
end_pos := field.pos.pos + field.pos.len end_pos := field.pos.pos + field.pos.len
before_comments := field.comments.filter(it.pos.pos < field.pos.pos) before_comments := field.comments.filter(it.pos.pos < field.pos.pos)
between_comments := field.comments[before_comments.len..].filter(it.pos.pos < end_pos) 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) {
} else if node.language != .v { } else if node.language != .v {
f.write('${node.name.after_char(`.`)}') f.write('${node.name.after_char(`.`)}')
} else { } else {
mut name := f.short_module(node.name) name := f.short_module(node.name)
f.mark_import_as_used(name) f.mark_import_as_used(name)
f.write('${name}') f.write('${name}')
} }
@ -1951,36 +1952,42 @@ pub fn (mut f Fmt) comptime_call(node ast.ComptimeCall) {
f.write('\$tmpl(${node.args[0].expr})') f.write('\$tmpl(${node.args[0].expr})')
} }
} else { } else {
if node.is_embed { match true {
if node.embed_file.compression_type == 'none' { node.is_embed {
f.write('\$embed_file(${node.args[0].expr})') if node.embed_file.compression_type == 'none' {
} else { f.write('\$embed_file(${node.args[0].expr})')
f.write('\$embed_file(${node.args[0].expr}, .${node.embed_file.compression_type})')
}
} else if node.is_env {
f.write("\$env('${node.args_var}')")
} else if node.is_pkgconfig {
f.write("\$pkgconfig('${node.args_var}')")
} else if node.method_name in ['compile_error', 'compile_warn'] {
f.write("\$${node.method_name}('${node.args_var}')")
} else {
inner_args := if node.args_var != '' {
node.args_var
} else {
node.args.map(if it.expr is ast.ArrayDecompose {
'...${it.expr.expr.str()}'
} else { } else {
it.str() f.write('\$embed_file(${node.args[0].expr}, .${node.embed_file.compression_type})')
}).join(', ') }
} }
method_expr := if node.has_parens { node.is_env {
'(${node.method_name}(${inner_args}))' f.write("\$env('${node.args_var}')")
} else { }
'${node.method_name}(${inner_args})' node.is_pkgconfig {
f.write("\$pkgconfig('${node.args_var}')")
}
node.method_name in ['compile_error', 'compile_warn'] {
f.write("\$${node.method_name}('${node.args_var}')")
}
else {
inner_args := if node.args_var != '' {
node.args_var
} else {
node.args.map(if it.expr is ast.ArrayDecompose {
'...${it.expr.expr.str()}'
} else {
it.str()
}).join(', ')
}
method_expr := if node.has_parens {
'(${node.method_name}(${inner_args}))'
} else {
'${node.method_name}(${inner_args})'
}
f.expr(node.left)
f.write('.$${method_expr}')
f.or_expr(node.or_block)
} }
f.expr(node.left)
f.write('.$${method_expr}')
f.or_expr(node.or_block)
} }
} }
} }
@ -2628,14 +2635,12 @@ pub fn (mut f Fmt) prefix_expr(node ast.PrefixExpr) {
if node.right.expr.op in [.key_in, .not_in, .key_is, .not_is] if node.right.expr.op in [.key_in, .not_in, .key_is, .not_is]
&& node.right.expr.right !is ast.InfixExpr { && node.right.expr.right !is ast.InfixExpr {
f.expr(node.right.expr.left) f.expr(node.right.expr.left)
if node.right.expr.op == .key_in { match node.right.expr.op {
f.write(' !in ') .key_in { f.write(' !in ') }
} else if node.right.expr.op == .not_in { .not_in { f.write(' in ') }
f.write(' in ') .key_is { f.write(' !is ') }
} else if node.right.expr.op == .key_is { .not_is { f.write(' is ') }
f.write(' !is ') else {}
} else if node.right.expr.op == .not_is {
f.write(' is ')
} }
f.expr(node.right.expr.right) f.expr(node.right.expr.right)
return return

View File

@ -79,39 +79,46 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
mut default_expr_align_i := 0 mut default_expr_align_i := 0
mut inc_indent := false // for correct indents with multi line default exprs mut inc_indent := false // for correct indents with multi line default exprs
for i, field in node.fields { for i, field in node.fields {
if i == node.mut_pos { match true {
f.writeln('mut:') i == node.mut_pos {
} else if i == node.pub_pos { f.writeln('mut:')
f.writeln('pub:')
} else if i == node.pub_mut_pos {
f.writeln('pub mut:')
} else if i == node.global_pos {
f.writeln('__global:')
} else if i == node.module_pos {
f.writeln('module:')
} else if i > 0 {
// keep one empty line between fields (exclude one after mut:, pub:, ...)
mut before_last_line := node.fields[i - 1].pos.line_nr
if node.fields[i - 1].comments.len > 0 {
if before_last_line < node.fields[i - 1].comments.last().pos.last_line {
before_last_line = node.fields[i - 1].comments.last().pos.last_line
}
} }
if node.fields[i - 1].has_default_expr { i == node.pub_pos {
if before_last_line < node.fields[i - 1].default_expr.pos().last_line { f.writeln('pub:')
before_last_line = node.fields[i - 1].default_expr.pos().last_line
}
} }
i == node.pub_mut_pos {
f.writeln('pub mut:')
}
i == node.global_pos {
f.writeln('__global:')
}
i == node.module_pos {
f.writeln('module:')
}
i > 0 {
// keep one empty line between fields (exclude one after mut:, pub:, ...)
last_field := node.fields[i - 1]
before_last_line := if last_field.comments.len > 0
&& last_field.pos.line_nr < last_field.comments.last().pos.last_line {
last_field.comments.last().pos.last_line
} else if last_field.has_default_expr {
last_field.default_expr.pos().last_line
} else {
last_field.pos.line_nr
}
mut next_first_line := field.pos.line_nr next_first_line := if field.comments.len > 0
if field.comments.len > 0 { && field.pos.line_nr > field.comments[0].pos.line_nr {
if next_first_line > field.comments[0].pos.line_nr { field.comments[0].pos.line_nr
next_first_line = field.comments[0].pos.line_nr } else {
field.pos.line_nr
}
if next_first_line - before_last_line > 1 {
f.writeln('')
} }
} }
if next_first_line - before_last_line > 1 { else {}
f.writeln('')
}
} }
end_pos := field.pos.pos + field.pos.len end_pos := field.pos.pos + field.pos.len
before_comments := field.comments.filter(it.pos.pos < field.pos.pos) 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) {
before_len := f.line_len before_len := f.line_len
f.comments(between_comments, iembed: true, has_nl: false) f.comments(between_comments, iembed: true, has_nl: false)
comments_len := f.line_len - before_len comments_len := f.line_len - before_len
mut field_align := field_aligns[field_align_i] if field_aligns[field_align_i].line_nr < field.pos.line_nr {
if field_align.line_nr < field.pos.line_nr {
field_align_i++ field_align_i++
field_align = field_aligns[field_align_i]
} }
field_align := field_aligns[field_align_i]
f.write(strings.repeat(` `, field_align.max_len - field.name.len - comments_len)) f.write(strings.repeat(` `, field_align.max_len - field.name.len - comments_len))
// Handle anon structs recursively // Handle anon structs recursively
if !f.write_anon_struct_field_decl(field.typ, field.anon_struct_decl) { 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) {
f.single_line_attrs(field.attrs, inline: true) f.single_line_attrs(field.attrs, inline: true)
} }
if field.has_default_expr { if field.has_default_expr {
mut align := default_expr_aligns[default_expr_align_i] if default_expr_aligns[default_expr_align_i].line_nr < field.pos.line_nr {
if align.line_nr < field.pos.line_nr {
default_expr_align_i++ default_expr_align_i++
align = default_expr_aligns[default_expr_align_i]
} }
align := default_expr_aligns[default_expr_align_i]
pad_len := align.max_len - attrs_len + align.max_type_len - field_types[i].len pad_len := align.max_len - attrs_len + align.max_type_len - field_types[i].len
f.write(strings.repeat(` `, pad_len)) f.write(strings.repeat(` `, pad_len))
f.write(' = ') f.write(' = ')
@ -167,11 +172,10 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
f.writeln('') f.writeln('')
} else { } else {
if !field.has_default_expr { if !field.has_default_expr {
mut align := comment_aligns[comment_align_i] if comment_aligns[comment_align_i].line_nr < field.pos.line_nr {
if align.line_nr < field.pos.line_nr {
comment_align_i++ comment_align_i++
align = comment_aligns[comment_align_i]
} }
align := comment_aligns[comment_align_i]
pad_len := align.max_len - attrs_len + align.max_type_len - field_types[i].len pad_len := align.max_len - attrs_len + align.max_type_len - field_types[i].len
f.write(strings.repeat(` `, pad_len)) f.write(strings.repeat(` `, pad_len))
} }
@ -238,11 +242,12 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
f.is_struct_init = struct_init_save f.is_struct_init = struct_init_save
} }
f.mark_types_import_as_used(node.typ) f.mark_types_import_as_used(node.typ)
type_sym := f.table.sym(node.typ) sym_name := f.table.sym(node.typ).name
// f.write('<old name: $type_sym.name>') // f.write('<old name: $type_sym.name>')
mut name := type_sym.name mut name := if !sym_name.starts_with('C.') && !sym_name.starts_with('JS.') {
if !name.starts_with('C.') && !name.starts_with('JS.') { f.no_cur_mod(f.short_module(sym_name)) // TODO f.type_to_str?
name = f.no_cur_mod(f.short_module(type_sym.name)) // TODO f.type_to_str? } else {
sym_name
} }
if name == 'void' { if name == 'void' {
name = '' name = ''

View File

@ -14,7 +14,7 @@ pub fn test_anon_fn_void(func fn ()) int {
fn C.HasAnonFnWithNamedParams(cb fn (c C.bar, d voidptr)) fn C.HasAnonFnWithNamedParams(cb fn (c C.bar, d voidptr))
// NB: the signature of both anonymus functions should only differs in the param name // NB: the signature of both anonymous functions should only differs in the param name
fn anon_fn_param_has_no_name(f fn (int) string) {} fn anon_fn_param_has_no_name(f fn (int) string) {}
fn anon_fn_with_named_param(func fn (a int) string) {} fn anon_fn_with_named_param(func fn (a int) string) {}

View File

@ -1,4 +1,4 @@
struct AttrsWithEscpaedStringArgs { struct AttrsWithEscapedStringArgs {
dollar string [foo: '\$var'] dollar string [foo: '\$var']
double_bs string [bar: '\\baz'] double_bs string [bar: '\\baz']
} }

View File

@ -18,7 +18,7 @@ fn string_inter_lit(mut c checker.Checker, mut node ast.StringInterLiteral) tabl
c.error('precision specification only valid for float types', node.fmt_poss[i]) c.error('precision specification only valid for float types', node.fmt_poss[i])
} }
if node.pluss[i] && !typ.is_number() { if node.pluss[i] && !typ.is_number() {
c.error('plus prefix only allowd for numbers', node.fmt_poss[i]) c.error('plus prefix only allowed for numbers', node.fmt_poss[i])
} }
if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`])
|| (typ.is_signed() && fmt !in [`d`, `x`, `X`, `o`, `c`]) || (typ.is_signed() && fmt !in [`d`, `x`, `X`, `o`, `c`])
@ -51,7 +51,7 @@ fn main() {
s << ' `${v_str}`' s << ' `${v_str}`'
println(s) println(s)
println('this is quite a long string' + println('this is quite a long string' +
' that is followd by an even longer part that should go to another line') ' that is followed by an even longer part that should go to another line')
if (a == b && b > r) || d > r || a < b || (b < d && a + b > r) 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) { || (a + b + d >= 0 && r < 0) || (a > b && d - r < b) {
println('ok') println('ok')

View File

@ -22,7 +22,7 @@ fn string_inter_lit(mut c checker.Checker, mut node ast.StringInterLiteral) tabl
node.fmt_poss[i]) node.fmt_poss[i])
} }
if node.pluss[i] && !typ.is_number() { if node.pluss[i] && !typ.is_number() {
c.error('plus prefix only allowd for numbers', node.fmt_poss[i]) c.error('plus prefix only allowed for numbers', node.fmt_poss[i])
} }
if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) || (typ.is_signed() && if (typ.is_unsigned() && fmt !in [`u`, `x`, `X`, `o`, `c`]) || (typ.is_signed() &&
fmt !in [`d`, `x`, `X`, `o`, `c`]) || (typ.is_int_literal() fmt !in [`d`, `x`, `X`, `o`, `c`]) || (typ.is_int_literal()
@ -60,7 +60,7 @@ fn main() {
s := []string{} s := []string{}
s << ' `$v_str`' s << ' `$v_str`'
println(s) println(s)
println('this is quite a long string' + ' that is followd by an even longer part that should go to another line') println('this is quite a long string' + ' that is followed by an even longer part that should go to another line')
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) { 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) {
println('ok') println('ok')
} }

View File

@ -14,7 +14,7 @@ fn main() {
} }
// NB: secret_key_size is missing here on purpose // NB: secret_key_size is missing here on purpose
// vfmt should leave it as is, assuming it is comming // vfmt should leave it as is, assuming it is coming
// from another .v file // from another .v file
struct VerifyKey { struct VerifyKey {

View File

@ -9,7 +9,7 @@ fn main() {
println('hello') println('hello')
/* /*
this comment also this comment also
has mutliple lines has multiple lines
but it's difference but it's difference
is that it is indented ! is that it is indented !
*/ */