vfmt: keep `vproto.Name`, when current module is `proto`

pull/5797/head
Delyan Angelov 2020-07-11 00:59:19 +03:00
parent 3c3a91697e
commit 7248d8422e
3 changed files with 42 additions and 4 deletions

View File

@ -46,7 +46,7 @@ pub struct string {
pub:
str byteptr // points to a C style 0 terminated string of bytes.
len int // the length of the .str field, excluding the ending 0 byte. It is always equal to strlen(.str).
mut:
mut:
is_lit int
}
// mut:
@ -157,7 +157,7 @@ pub fn cstring_to_vstring(cstr byteptr) string {
pub fn (s string) replace_once(rep, with string) string {
index := s.index(rep) or {
return s
return s.clone()
}
return s.substr(0, index) + with + s.substr(index + rep.len, s.len)
}

View File

@ -676,6 +676,15 @@ pub fn (mut f Fmt) prefix_expr_cast_expr(fexpr ast.Expr) {
pub fn (f &Fmt) type_to_str(t table.Type) string {
mut res := f.table.type_to_str(t)
map_prefix := 'map[string]'
cur_mod := f.cur_mod + '.'
has_map_prefix := res.starts_with(map_prefix)
if has_map_prefix {
res = res.replace(map_prefix, '')
}
no_symbols := res.trim_left('&[]')
should_shorten := no_symbols.starts_with(cur_mod)
//
for res.ends_with('_ptr') {
// type_ptr => &type
res = res[0..res.len - 4]
@ -686,12 +695,18 @@ pub fn (f &Fmt) type_to_str(t table.Type) string {
prefix := '[]fixed_'
res = res[prefix.len..]
last_underscore_idx := res.last_index('_') or {
return '[]' + res.replace(f.cur_mod + '.', '')
return '[]' + if should_shorten { res.replace_once(cur_mod, '') } else { res }
}
limit := res[last_underscore_idx + 1..]
res = '[' + limit + ']' + res[..last_underscore_idx]
}
return res.replace(f.cur_mod + '.', '')
if should_shorten {
res = res.replace_once(cur_mod, '')
}
if has_map_prefix {
res = map_prefix + res
}
return res
}
pub fn (mut f Fmt) expr(node ast.Expr) {

View File

@ -0,0 +1,23 @@
module proto
import vproto
struct Xyz {
x int
}
struct Abcde {
f1 vproto.Xyz
f2 &vproto.Xyz
f3 []vproto.Xyz
f4 []&vproto.Xyz
f5 map[string]vproto.Xyz
f6 map[string]&vproto.Xyz
//
p1 Xyz
p2 &Xyz
p3 []Xyz
p4 []&Xyz
p5 map[string]Xyz
p6 map[string]&Xyz
}