Skip to content

Commit

Permalink
vfmt: allow single line defer {}, just like or {}
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed May 22, 2024
1 parent ba5b274 commit 1b5af1f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
43 changes: 26 additions & 17 deletions vlib/v/fmt/fmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -924,9 +924,7 @@ pub fn (mut f Fmt) const_decl(node ast.ConstDecl) {
}
}
f.inside_const = true
defer {
f.inside_const = false
}
defer { f.inside_const = false }
if !node.is_block {
f.write('const ')
}
Expand Down Expand Up @@ -975,13 +973,30 @@ pub fn (mut f Fmt) const_decl(node ast.ConstDecl) {
f.writeln('')
}

pub fn (mut f Fmt) defer_stmt(node ast.DeferStmt) {
f.write('defer {')
if node.stmts.len > 0 || node.pos.line_nr < node.pos.last_line {
f.writeln('')
fn (mut f Fmt) defer_stmt(node ast.DeferStmt) {
f.write('defer ')
if node.stmts.len == 0 {
f.writeln('{}')
} else if node.stmts.len == 1 && node.pos.line_nr == node.pos.last_line
&& stmt_is_single_line(node.stmts[0]) {
f.write('{ ')
// the control stmts (return/break/continue...) print a newline inside them,
// so, since this'll all be on one line, trim any possible whitespace
str := f.node_str(node.stmts[0]).trim_space()
// single_line := ' defer { ${str} }'
// if single_line.len + f.line_len <= fmt.max_len {
// f.write(single_line)
// return
//}
f.write(str)

// f.stmt(node.stmts[0])
f.writeln(' }')
} else {
f.writeln('{')
f.stmts(node.stmts)
f.writeln('}')
}
f.writeln('}')
}

pub fn (mut f Fmt) expr_stmt(node ast.ExprStmt) {
Expand Down Expand Up @@ -1079,9 +1094,7 @@ pub fn (mut f Fmt) anon_fn(node ast.AnonFn) {
fn (mut f Fmt) fn_body(node ast.FnDecl) {
prev_fn_scope := f.fn_scope
f.fn_scope = node.scope
defer {
f.fn_scope = prev_fn_scope
}
defer { f.fn_scope = prev_fn_scope }
if node.language == .v || (node.is_method && node.language == .js) {
if !node.no_body {
f.write(' {')
Expand Down Expand Up @@ -1949,9 +1962,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
if node.is_method {
if node.name in ['map', 'filter', 'all', 'any'] {
f.in_lambda_depth++
defer {
f.in_lambda_depth--
}
defer { f.in_lambda_depth-- }
}
if node.left is ast.Ident {
// `time.now()` without `time imported` is processed as a method call with `time` being
Expand Down Expand Up @@ -2544,9 +2555,7 @@ const wsinfix_depth_max = 10

fn (mut f Fmt) write_splitted_infix(conditions []string, penalties []int, ignore_paren bool, is_cond bool) {
f.wsinfix_depth++
defer {
f.wsinfix_depth--
}
defer { f.wsinfix_depth-- }
for i, cnd in conditions {
c := cnd.trim_space()
if f.line_len + c.len < fmt.break_points[penalties[i]] {
Expand Down
4 changes: 1 addition & 3 deletions vlib/v/fmt/tests/blocks_expected.vv
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ fn unsafe_fn() {
}

fn fn_with_defer() {
defer {
voidfn()
}
defer { voidfn() }
}
12 changes: 12 additions & 0 deletions vlib/v/fmt/tests/defer_keep.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn foo() {}

fn nested_match() {
defer { foo() }
defer {
foo()
bar()
}
defer {
foo()
}
}
2 changes: 0 additions & 2 deletions vlib/v/fmt/tests/empty_curlies_and_parens_keep.vv
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ fn main() {
for false {
}
defer {}
defer {
}
unsafe {}
unsafe {
}
Expand Down
6 changes: 2 additions & 4 deletions vlib/v/tests/defer/defer_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,10 @@ fn test_defer_with_reserved_words() {
fn test_defer_inside_comptime_if_else() {
$if false {
} $else {
defer {
}
defer {}
}
$if true {
defer {
}
defer {}
} $else {
}
assert true
Expand Down

0 comments on commit 1b5af1f

Please sign in to comment.