From 4af4a8f2ffd278905c555f13708cf4d6b18bd745 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 22 May 2020 22:18:56 +0300 Subject: [PATCH] fmt: fix 'import time as t', then using t.sleep_ms() . --- vlib/v/fmt/fmt.v | 19 +++++++++++++------ vlib/v/fmt/tests/array_newlines_keep.vv | 2 ++ vlib/v/fmt/tests/import_with_alias_keep.vv | 9 +++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 vlib/v/fmt/tests/import_with_alias_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index b22742eed8..8e8c5b9076 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -30,6 +30,7 @@ pub mut: import_pos int // position of the imports in the resulting string for later autoimports insertion used_imports []string // to remove unused imports is_debug bool + mod2alias map[string]string // for `import time as t`, will contain: 'time'=>'t' } pub fn fmt(file ast.File, table &table.Table, is_debug bool) string { @@ -41,6 +42,9 @@ pub fn fmt(file ast.File, table &table.Table, is_debug bool) string { file: file is_debug: is_debug } + for imp in file.imports { + f.mod2alias[imp.mod.all_after_last('.')] = imp.alias + } f.cur_mod = 'main' for stmt in file.stmts { if stmt is ast.Import { @@ -542,7 +546,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) { } } ast.EnumVal { - name := short_module(it.enum_name) + name := f.short_module(it.enum_name) f.write(name + '.' + it.val) } ast.FloatLiteral { @@ -555,7 +559,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) { if it.kind == .blank_ident { f.write('_') } else { - name := short_module(it.name) + name := f.short_module(it.name) // f.write('<$it.name => $name>') f.write(name) if name.contains('.') { @@ -777,7 +781,7 @@ pub fn (mut f Fmt) fn_decl(node ast.FnDecl) { } // foo.bar.fn() => bar.fn() -pub fn short_module(name string) string { +pub fn (mut f Fmt) short_module(name string) string { if !name.contains('.') { return name } @@ -785,7 +789,10 @@ pub fn short_module(name string) string { if vals.len < 2 { return name } - return vals[vals.len - 2] + '.' + vals[vals.len - 1] + mname := vals[vals.len - 2] + symname := vals[vals.len - 1] + aname := f.mod2alias[mname] + return '${aname}.${symname}' } pub fn (mut f Fmt) if_expr(it ast.IfExpr) { @@ -847,7 +854,7 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) { f.write(')') f.or_expr(node.or_block) } else { - name := short_module(node.name) + name := f.short_module(node.name) f.mark_module_as_used(name) f.write('${name}') if node.generic_type != 0 && node.generic_type != table.void_type { @@ -1065,7 +1072,7 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) { pub fn (mut f Fmt) struct_init(it ast.StructInit) { type_sym := f.table.get_type_symbol(it.typ) // f.write('') - mut name := short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str? + mut name := f.short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str? if name == 'void' { name = '' } diff --git a/vlib/v/fmt/tests/array_newlines_keep.vv b/vlib/v/fmt/tests/array_newlines_keep.vv index 822e0cbad1..b2d2538ffa 100644 --- a/vlib/v/fmt/tests/array_newlines_keep.vv +++ b/vlib/v/fmt/tests/array_newlines_keep.vv @@ -12,4 +12,6 @@ fn main() { x := []int{len: 10, cap: 100, init: 1} _ := expected_flags buf := [100]byte + println(x) + println(buf) } diff --git a/vlib/v/fmt/tests/import_with_alias_keep.vv b/vlib/v/fmt/tests/import_with_alias_keep.vv new file mode 100644 index 0000000000..db8f4d3d8d --- /dev/null +++ b/vlib/v/fmt/tests/import_with_alias_keep.vv @@ -0,0 +1,9 @@ +import os +import time as t + +fn main() { + println('start') + t.sleep_ms(500) + println('end') + os.system('date') +}