diff --git a/vlib/v/fmt/comments.v b/vlib/v/fmt/comments.v index 1a5c5d3fd1..fcd592fdee 100644 --- a/vlib/v/fmt/comments.v +++ b/vlib/v/fmt/comments.v @@ -31,7 +31,7 @@ pub fn (mut f Fmt) comment(node ast.Comment, options CommentsOptions) { } defer { // 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') { f.vfmt_off(node.pos.line_nr) } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index bd869e163a..0e38b9c5c1 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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 // The nodes are Stmts if node is ast.Stmt && prev_node is ast.Stmt { - stmt := node - prev_stmt := prev_node - // Force a newline after a block of HashStmts - if prev_stmt is ast.HashStmt && stmt !in [ast.HashStmt, ast.ExprStmt] { - return true - } - // Force a newline after function declarations - // The only exception is inside an block of no_body functions - if prev_stmt is ast.FnDecl { - if stmt !is ast.FnDecl || !prev_stmt.no_body { + match prev_node { + // Force a newline after a block of HashStmts + ast.HashStmt { + if node !in [ast.HashStmt, ast.ExprStmt] { + return true + } + } + // Force a newline after function declarations + // The only exception is inside a block of no_body functions + ast.FnDecl { + if node !is ast.FnDecl || !prev_node.no_body { + return true + } + } + // Force a newline after struct declarations + ast.StructDecl { return true } - } - // Force a newline after struct declarations - if prev_stmt is ast.StructDecl { - return true - } - // Empty line after an block of type declarations - if prev_stmt is ast.TypeDecl && stmt !is ast.TypeDecl { - return true - } - // 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 { + // Empty line after a block of type declarations + ast.TypeDecl { + if node !is ast.TypeDecl { + return true + } + } + // Imports are handled special hence they are ignored here + ast.Import { return false } + else {} } - if stmt is ast.EnumDecl { - if stmt.attrs.len > 0 && stmt.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 { + match node { + // Attributes are not respected in the stmts position, so this requires manual checking + ast.StructDecl, ast.EnumDecl, ast.FnDecl { + if node.attrs.len > 0 && node.attrs[0].pos.line_nr - prev_line_nr <= 1 { + return false + } + } + ast.Import { return false } + else {} } } // 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) { f.write('assert ') mut expr := node.expr - for expr is ast.ParExpr { - expr = (expr as ast.ParExpr).expr + for mut expr is ast.ParExpr { + expr = expr.expr } f.expr(expr) 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) { - 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 before_comments := field.comments.filter(it.pos.pos < field.pos.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 { f.write('${node.name.after_char(`.`)}') } else { - mut name := f.short_module(node.name) + name := f.short_module(node.name) f.mark_import_as_used(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})') } } else { - if node.is_embed { - if node.embed_file.compression_type == 'none' { - f.write('\$embed_file(${node.args[0].expr})') - } else { - 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()}' + match true { + node.is_embed { + if node.embed_file.compression_type == 'none' { + f.write('\$embed_file(${node.args[0].expr})') } else { - it.str() - }).join(', ') + f.write('\$embed_file(${node.args[0].expr}, .${node.embed_file.compression_type})') + } } - method_expr := if node.has_parens { - '(${node.method_name}(${inner_args}))' - } else { - '${node.method_name}(${inner_args})' + node.is_env { + f.write("\$env('${node.args_var}')") + } + 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] && node.right.expr.right !is ast.InfixExpr { f.expr(node.right.expr.left) - if node.right.expr.op == .key_in { - f.write(' !in ') - } else if node.right.expr.op == .not_in { - f.write(' in ') - } else if node.right.expr.op == .key_is { - f.write(' !is ') - } else if node.right.expr.op == .not_is { - f.write(' is ') + match node.right.expr.op { + .key_in { f.write(' !in ') } + .not_in { f.write(' in ') } + .key_is { f.write(' !is ') } + .not_is { f.write(' is ') } + else {} } f.expr(node.right.expr.right) return diff --git a/vlib/v/fmt/struct.v b/vlib/v/fmt/struct.v index 452e0a2150..d0fb47933f 100644 --- a/vlib/v/fmt/struct.v +++ b/vlib/v/fmt/struct.v @@ -79,39 +79,46 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) { mut default_expr_align_i := 0 mut inc_indent := false // for correct indents with multi line default exprs for i, field in node.fields { - if i == node.mut_pos { - f.writeln('mut:') - } else if i == node.pub_pos { - 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 - } + match true { + i == node.mut_pos { + f.writeln('mut:') } - if node.fields[i - 1].has_default_expr { - if before_last_line < node.fields[i - 1].default_expr.pos().last_line { - before_last_line = node.fields[i - 1].default_expr.pos().last_line - } + i == node.pub_pos { + f.writeln('pub:') } + 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 - if field.comments.len > 0 { - if next_first_line > field.comments[0].pos.line_nr { - next_first_line = field.comments[0].pos.line_nr + next_first_line := if field.comments.len > 0 + && field.pos.line_nr > field.comments[0].pos.line_nr { + 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 { - f.writeln('') - } + else {} } end_pos := field.pos.pos + field.pos.len 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 f.comments(between_comments, iembed: true, has_nl: false) comments_len := f.line_len - before_len - mut field_align := field_aligns[field_align_i] - if field_align.line_nr < field.pos.line_nr { + if field_aligns[field_align_i].line_nr < field.pos.line_nr { 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)) // Handle anon structs recursively 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) } if field.has_default_expr { - mut align := default_expr_aligns[default_expr_align_i] - if align.line_nr < field.pos.line_nr { + if default_expr_aligns[default_expr_align_i].line_nr < field.pos.line_nr { 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 f.write(strings.repeat(` `, pad_len)) f.write(' = ') @@ -167,11 +172,10 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) { f.writeln('') } else { if !field.has_default_expr { - mut align := comment_aligns[comment_align_i] - if align.line_nr < field.pos.line_nr { + if comment_aligns[comment_align_i].line_nr < field.pos.line_nr { 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 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.mark_types_import_as_used(node.typ) - type_sym := f.table.sym(node.typ) + sym_name := f.table.sym(node.typ).name // f.write('') - mut name := type_sym.name - if !name.starts_with('C.') && !name.starts_with('JS.') { - name = f.no_cur_mod(f.short_module(type_sym.name)) // TODO f.type_to_str? + mut name := if !sym_name.starts_with('C.') && !sym_name.starts_with('JS.') { + f.no_cur_mod(f.short_module(sym_name)) // TODO f.type_to_str? + } else { + sym_name } if name == 'void' { name = '' diff --git a/vlib/v/fmt/tests/anon_fn_as_param_keep.vv b/vlib/v/fmt/tests/anon_fn_as_param_keep.vv index 4ed7d1a26e..d0e50bb12c 100644 --- a/vlib/v/fmt/tests/anon_fn_as_param_keep.vv +++ b/vlib/v/fmt/tests/anon_fn_as_param_keep.vv @@ -14,7 +14,7 @@ pub fn test_anon_fn_void(func fn ()) int { 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_with_named_param(func fn (a int) string) {} diff --git a/vlib/v/fmt/tests/attrs_keep.vv b/vlib/v/fmt/tests/attrs_keep.vv index cad488cec8..50929a103c 100644 --- a/vlib/v/fmt/tests/attrs_keep.vv +++ b/vlib/v/fmt/tests/attrs_keep.vv @@ -1,4 +1,4 @@ -struct AttrsWithEscpaedStringArgs { +struct AttrsWithEscapedStringArgs { dollar string [foo: '\$var'] double_bs string [bar: '\\baz'] } diff --git a/vlib/v/fmt/tests/consts_with_embeded_comments_expected.vv b/vlib/v/fmt/tests/consts_with_embedded_comments_expected.vv similarity index 100% rename from vlib/v/fmt/tests/consts_with_embeded_comments_expected.vv rename to vlib/v/fmt/tests/consts_with_embedded_comments_expected.vv diff --git a/vlib/v/fmt/tests/consts_with_embeded_comments_input.vv b/vlib/v/fmt/tests/consts_with_embedded_comments_input.vv similarity index 100% rename from vlib/v/fmt/tests/consts_with_embeded_comments_input.vv rename to vlib/v/fmt/tests/consts_with_embedded_comments_input.vv diff --git a/vlib/v/fmt/tests/expressions_expected.vv b/vlib/v/fmt/tests/expressions_expected.vv index dacfbbcdab..d91616fc9c 100644 --- a/vlib/v/fmt/tests/expressions_expected.vv +++ b/vlib/v/fmt/tests/expressions_expected.vv @@ -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]) } 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() && fmt !in [`d`, `x`, `X`, `o`, `c`]) @@ -51,7 +51,7 @@ fn main() { s << ' `${v_str}`' println(s) 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) || (a + b + d >= 0 && r < 0) || (a > b && d - r < b) { println('ok') diff --git a/vlib/v/fmt/tests/expressions_input.vv b/vlib/v/fmt/tests/expressions_input.vv index dec30ce43a..6f8ea6d9ac 100644 --- a/vlib/v/fmt/tests/expressions_input.vv +++ b/vlib/v/fmt/tests/expressions_input.vv @@ -22,7 +22,7 @@ fn string_inter_lit(mut c checker.Checker, mut node ast.StringInterLiteral) tabl node.fmt_poss[i]) } 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() && fmt !in [`d`, `x`, `X`, `o`, `c`]) || (typ.is_int_literal() @@ -60,7 +60,7 @@ fn main() { s := []string{} s << ' `$v_str`' 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) { println('ok') } diff --git a/vlib/v/fmt/tests/fixed_size_array_type_keep.vv b/vlib/v/fmt/tests/fixed_size_array_type_keep.vv index 3c91216679..9eb0361f6f 100644 --- a/vlib/v/fmt/tests/fixed_size_array_type_keep.vv +++ b/vlib/v/fmt/tests/fixed_size_array_type_keep.vv @@ -14,7 +14,7 @@ fn main() { } // 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 struct VerifyKey { diff --git a/vlib/v/fmt/tests/multiline_comment_keep.vv b/vlib/v/fmt/tests/multiline_comment_keep.vv index d3d1536d3c..4b3dcedfb6 100644 --- a/vlib/v/fmt/tests/multiline_comment_keep.vv +++ b/vlib/v/fmt/tests/multiline_comment_keep.vv @@ -9,7 +9,7 @@ fn main() { println('hello') /* this comment also - has mutliple lines + has multiple lines but it's difference is that it is indented ! */