mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vfmt: fix imports
This commit is contained in:
parent
08bbc251ff
commit
ffe8452f02
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user