Skip to content
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
2 changes: 1 addition & 1 deletion vlib/v/fmt/comments.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
147 changes: 76 additions & 71 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
return false
// Empty line after a block of type declarations
ast.TypeDecl {
if node !is ast.TypeDecl {
return true
}
}
}
if stmt is ast.EnumDecl {
if stmt.attrs.len > 0 && stmt.attrs[0].pos.line_nr - prev_line_nr <= 1 {
// Imports are handled special hence they are ignored here
ast.Import {
return false
}
else {}
}
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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}')
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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
Expand Down
87 changes: 46 additions & 41 deletions vlib/v/fmt/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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(' = ')
Expand All @@ -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))
}
Expand Down Expand Up @@ -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('<old name: $type_sym.name>')
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 = ''
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/fmt/tests/anon_fn_as_param_keep.vv
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
2 changes: 1 addition & 1 deletion vlib/v/fmt/tests/attrs_keep.vv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct AttrsWithEscpaedStringArgs {
struct AttrsWithEscapedStringArgs {
dollar string [foo: '\$var']
double_bs string [bar: '\\baz']
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/fmt/tests/expressions_expected.vv
Original file line number Diff line number Diff line change
Expand Up @@ -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`])
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/fmt/tests/expressions_input.vv
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')
}
Expand Down
Loading