diff --git a/cmd/tools/vfmt.v b/cmd/tools/vfmt.v index 41bb8f325f..4f7b0189a1 100644 --- a/cmd/tools/vfmt.v +++ b/cmd/tools/vfmt.v @@ -162,7 +162,9 @@ fn (foptions &FormatOptions) format_file(file string) { eprintln('vfmt2 running fmt.fmt over file: $file') } table := table.new_table() + //checker := checker.new_checker(table, prefs) file_ast := parser.parse_file(file, table, .parse_comments, prefs, &ast.Scope{parent: 0}) + //checker.check(file_ast) formatted_content := fmt.fmt(file_ast, table) file_name := os.file_name(file) vfmt_output_path := os.join_path(os.temp_dir(), 'vfmt_' + file_name) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 947f7f6dd8..8c9555508f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -28,6 +28,7 @@ mut: fn_return_type table.Type // current function's return type const_decl string const_deps []string + assigned_var_name string // fn_decl ast.FnDecl pref &pref.Preferences // Preferences shared from V struct } @@ -478,6 +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 val_type := c.expr(assign_stmt.right[i]) if assign_stmt.op == .assign { var_type := c.expr(ident) @@ -496,6 +498,7 @@ pub fn (c mut Checker) assign_stmt(assign_stmt mut ast.AssignStmt) { } } c.expected_type = table.void_type + c.assigned_var_name = '' } pub fn (c mut Checker) array_init(array_init mut ast.ArrayInit) table.Type { @@ -994,9 +997,9 @@ 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 { - // sym := c.table.get_type_symbol(c.expected_type) - // println('$c.file.path $node.pos.line_nr IF: checker exp type = ' + sym.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 } node.typ = table.void_type diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 80cea09892..a0e1c0463d 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -10,7 +10,7 @@ import ( ) const ( - tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', + tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t'] max_len = 80 ) @@ -25,6 +25,7 @@ mut: single_line_if bool cur_mod string file ast.File + did_imports bool } pub fn fmt(file ast.File, table &table.Table) string { @@ -34,6 +35,7 @@ pub fn fmt(file ast.File, table &table.Table) string { indent: 0 file: file } + f.cur_mod = 'main' for stmt in file.stmts { f.stmt(stmt) } @@ -77,10 +79,16 @@ fn (f mut Fmt) mod(mod ast.Module) { } fn (f mut Fmt) imports(imports []ast.Import) { + if f.did_imports { + return + } + f.did_imports = true if imports.len == 1 { imp_stmt_str := f.imp_stmt_str(imports[0]) f.writeln('import ${imp_stmt_str}\n') - } else if imports.len > 1 { + } + // + else if imports.len > 1 { f.writeln('import (') f.indent++ for imp in imports { @@ -667,7 +675,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 == + single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == 1 && it.branches[1].stmts.len == 1 && it.is_expr f.single_line_if = single_line for i, branch in it.branches { diff --git a/vlib/v/fmt/tests/conditions_expected.vv b/vlib/v/fmt/tests/conditions_expected.vv index d9c69302f3..6378ceeff3 100644 --- a/vlib/v/fmt/tests/conditions_expected.vv +++ b/vlib/v/fmt/tests/conditions_expected.vv @@ -8,5 +8,9 @@ fn fn_with_if_else() { } fn fn_with_if_else_oneline() { - x := if true { 1 } else { 2 } + x := if true { + 1 + } else { + 2 + } } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 7f6045ff47..85b73d0ccd 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -34,7 +34,6 @@ mut: ast_imports []ast.Import is_amp bool returns bool - inside_match_case bool // to separate `match_expr { }` from `Struct{}` //comments []ast.Comment }