From 852ec61b34fbaa55509730b56b1b13af04d26364 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 5 Apr 2020 23:00:17 +0200 Subject: [PATCH] vfmt: fix single line if --- vlib/v/checker/checker.v | 8 ++++---- vlib/v/fmt/fmt.v | 10 +++++++++- vlib/v/fmt/tests/conditions_expected.vv | 6 +----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8c9555508f..af83ea368d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -28,7 +28,7 @@ mut: fn_return_type table.Type // current function's return type const_decl string const_deps []string - assigned_var_name string + //assigned_var_name string // fn_decl ast.FnDecl pref &pref.Preferences // Preferences shared from V struct } @@ -479,7 +479,7 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) { for i, _ in assign_stmt.left { mut ident := assign_stmt.left[i] mut ident_var_info := ident.var_info() - c.assigned_var_name = ident.name + //c.assigned_var_name = ident.name val_type := c.expr(assign_stmt.right[i]) if assign_stmt.op == .assign { var_type := c.expr(ident) @@ -498,7 +498,7 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) { } } c.expected_type = table.void_type - c.assigned_var_name = '' + //c.assigned_var_name = '' } pub fn (c mut Checker) array_init(array_init mut ast.ArrayInit) table.Type { @@ -997,7 +997,7 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type { } pub fn (c mut Checker) if_expr(node mut ast.IfExpr) table.Type { - if c.expected_type != table.void_type || c.assigned_var_name != '' { + if c.expected_type != table.void_type { // || c.assigned_var_name != '' { //sym := c.table.get_type_symbol(c.expected_type) //println('$c.file.path $node.pos.line_nr IF is expr: checker exp type = ' + sym.name) node.is_expr = true diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index a0e1c0463d..e11b5902f8 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -26,6 +26,7 @@ mut: cur_mod string file ast.File did_imports bool + is_assign bool } pub fn fmt(file ast.File, table &table.Table) string { @@ -130,6 +131,7 @@ fn (f mut Fmt) stmt(node ast.Stmt) { f.write(', ') } } + f.is_assign = true f.write(' $it.op.str() ') for i, val in it.right { f.expr(val) @@ -138,10 +140,16 @@ fn (f mut Fmt) stmt(node ast.Stmt) { } } f.writeln('') + f.is_assign = false } ast.Attr { f.writeln('[$it.name]') } + ast.Block { + f.writeln('{') + f.stmts(it.stmts) + f.writeln('}') + } ast.BranchStmt { match it.tok.kind { .key_break { @@ -676,7 +684,7 @@ fn short_module(name string) string { fn (f mut Fmt) if_expr(it ast.IfExpr) { single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == - 1 && it.branches[1].stmts.len == 1 && it.is_expr + 1 && it.branches[1].stmts.len == 1 && (it.is_expr || f.is_assign) f.single_line_if = single_line for i, branch in it.branches { if i == 0 { diff --git a/vlib/v/fmt/tests/conditions_expected.vv b/vlib/v/fmt/tests/conditions_expected.vv index 6378ceeff3..d9c69302f3 100644 --- a/vlib/v/fmt/tests/conditions_expected.vv +++ b/vlib/v/fmt/tests/conditions_expected.vv @@ -8,9 +8,5 @@ fn fn_with_if_else() { } fn fn_with_if_else_oneline() { - x := if true { - 1 - } else { - 2 - } + x := if true { 1 } else { 2 } }