vfmt: fix single line if
parent
307daacf67
commit
852ec61b34
|
@ -28,7 +28,7 @@ mut:
|
||||||
fn_return_type table.Type // current function's return type
|
fn_return_type table.Type // current function's return type
|
||||||
const_decl string
|
const_decl string
|
||||||
const_deps []string
|
const_deps []string
|
||||||
assigned_var_name string
|
//assigned_var_name string
|
||||||
// fn_decl ast.FnDecl
|
// fn_decl ast.FnDecl
|
||||||
pref &pref.Preferences // Preferences shared from V struct
|
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 {
|
for i, _ in assign_stmt.left {
|
||||||
mut ident := assign_stmt.left[i]
|
mut ident := assign_stmt.left[i]
|
||||||
mut ident_var_info := ident.var_info()
|
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])
|
val_type := c.expr(assign_stmt.right[i])
|
||||||
if assign_stmt.op == .assign {
|
if assign_stmt.op == .assign {
|
||||||
var_type := c.expr(ident)
|
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.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 {
|
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 {
|
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)
|
//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)
|
//println('$c.file.path $node.pos.line_nr IF is expr: checker exp type = ' + sym.name)
|
||||||
node.is_expr = true
|
node.is_expr = true
|
||||||
|
|
|
@ -26,6 +26,7 @@ mut:
|
||||||
cur_mod string
|
cur_mod string
|
||||||
file ast.File
|
file ast.File
|
||||||
did_imports bool
|
did_imports bool
|
||||||
|
is_assign bool
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fmt(file ast.File, table &table.Table) string {
|
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.write(', ')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
f.is_assign = true
|
||||||
f.write(' $it.op.str() ')
|
f.write(' $it.op.str() ')
|
||||||
for i, val in it.right {
|
for i, val in it.right {
|
||||||
f.expr(val)
|
f.expr(val)
|
||||||
|
@ -138,10 +140,16 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
|
f.is_assign = false
|
||||||
}
|
}
|
||||||
ast.Attr {
|
ast.Attr {
|
||||||
f.writeln('[$it.name]')
|
f.writeln('[$it.name]')
|
||||||
}
|
}
|
||||||
|
ast.Block {
|
||||||
|
f.writeln('{')
|
||||||
|
f.stmts(it.stmts)
|
||||||
|
f.writeln('}')
|
||||||
|
}
|
||||||
ast.BranchStmt {
|
ast.BranchStmt {
|
||||||
match it.tok.kind {
|
match it.tok.kind {
|
||||||
.key_break {
|
.key_break {
|
||||||
|
@ -676,7 +684,7 @@ fn short_module(name string) string {
|
||||||
|
|
||||||
fn (f mut Fmt) if_expr(it ast.IfExpr) {
|
fn (f mut Fmt) if_expr(it ast.IfExpr) {
|
||||||
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len ==
|
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
|
f.single_line_if = single_line
|
||||||
for i, branch in it.branches {
|
for i, branch in it.branches {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
|
|
|
@ -8,9 +8,5 @@ fn fn_with_if_else() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fn_with_if_else_oneline() {
|
fn fn_with_if_else_oneline() {
|
||||||
x := if true {
|
x := if true { 1 } else { 2 }
|
||||||
1
|
|
||||||
} else {
|
|
||||||
2
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue