type_to_str: fix imported types

pull/3807/head
Alexander Medvednikov 2020-02-21 19:56:37 +01:00
parent e0c6766a79
commit 9be87d03f5
2 changed files with 21 additions and 1 deletions

View File

@ -23,6 +23,7 @@ mut:
empty_line bool empty_line bool
line_len int line_len int
single_line_if bool single_line_if bool
cur_mod string
} }
pub fn fmt(file ast.File, table &table.Table) string { pub fn fmt(file ast.File, table &table.Table) string {
@ -63,6 +64,7 @@ fn (f mut Fmt) mod(mod ast.Module) {
if mod.name != 'main' { if mod.name != 'main' {
f.writeln('module ${mod.name}\n') f.writeln('module ${mod.name}\n')
} }
f.cur_mod = mod.name
} }
fn (f mut Fmt) imports(imports []ast.Import) { fn (f mut Fmt) imports(imports []ast.Import) {
@ -197,11 +199,16 @@ fn (f mut Fmt) struct_decl(node ast.StructDecl) {
for field in node.fields { for field in node.fields {
f.write('\t$field.name ') f.write('\t$field.name ')
f.write(strings.repeat(` `, max - field.name.len)) f.write(strings.repeat(` `, max - field.name.len))
f.writeln(f.table.type_to_str(field.typ)) f.writeln(f.type_to_str(field.typ))
} }
f.writeln('}\n') f.writeln('}\n')
} }
fn (f &Fmt) type_to_str(t table.Type) string {
res := f.table.type_to_str(t)
return res.replace(f.cur_mod + '.', '')
}
fn (f mut Fmt) expr(node ast.Expr) { fn (f mut Fmt) expr(node ast.Expr) {
match node { match node {
ast.ArrayInit { ast.ArrayInit {

View File

@ -400,6 +400,13 @@ pub mut:
pub fn (table &Table) type_to_str(t Type) string { pub fn (table &Table) type_to_str(t Type) string {
sym := table.get_type_symbol(t) sym := table.get_type_symbol(t)
mut res := sym.name.replace('array_', '[]') mut res := sym.name.replace('array_', '[]')
// mod.submod.submod2.Type => submod2.Type
if res.contains('.') {
vals := res.split('.')
if vals.len > 2 {
res = vals[vals.len - 2] + '.' + vals[vals.len - 1]
}
}
if type_is_optional(t) { if type_is_optional(t) {
res = '?' + res res = '?' + res
} }
@ -407,5 +414,11 @@ pub fn (table &Table) type_to_str(t Type) string {
if nr_muls > 0 { if nr_muls > 0 {
res = strings.repeat(`&`, nr_muls) + res res = strings.repeat(`&`, nr_muls) + res
} }
/*
if res.starts_with(cur_mod +'.') {
res = res[cur_mod.len+1.. ]
}
*/
return res return res
} }