vfmt: fix single line if

pull/4259/head
Alexander Medvednikov 2020-04-05 23:00:17 +02:00
parent 307daacf67
commit 852ec61b34
3 changed files with 14 additions and 10 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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 }
}