fmt: keep module alias in struct decl field types (#7305)

pull/7285/head
Lukas Neubert 2020-12-14 05:03:43 +01:00 committed by GitHub
parent d59c5b01e1
commit 9b139c58ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -615,7 +615,10 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
if comments_len + field.name.len > max {
max = comments_len + field.name.len
}
ft := f.no_cur_mod(f.table.type_to_str(field.typ))
mut ft := f.no_cur_mod(f.table.type_to_str(field.typ))
if !ft.starts_with('C.') && !ft.starts_with('JS.') {
ft = f.short_module(ft)
}
field_types << ft
if ft.len > max_type {
max_type = ft.len
@ -1290,13 +1293,13 @@ pub fn (mut f Fmt) short_module(name string) string {
if vals.len < 2 {
return name
}
mname := vals[vals.len - 2]
mname, tprefix := f.get_modname_prefix(vals[vals.len - 2])
symname := vals[vals.len - 1]
aname := f.mod2alias[mname]
if aname == '' {
return symname
}
return '${aname}.$symname'
return '$tprefix${aname}.$symname'
}
pub fn (mut f Fmt) lock_expr(lex ast.LockExpr) {
@ -1987,3 +1990,14 @@ fn (mut f Fmt) is_external_name(name string) bool {
}
return false
}
fn (f Fmt) get_modname_prefix(mname string) (string, string) {
// ./tests/proto_module_importing_vproto_keep.vv to know, why here is checked for ']' and '&'
if !mname.contains(']') && !mname.contains('&') {
return mname, ''
}
after_rbc := mname.all_after_last(']')
after_ref := mname.all_after_last('&')
modname := if after_rbc.len < after_ref.len { after_rbc } else { after_ref }
return modname, mname.trim_suffix(modname)
}

View File

@ -1,6 +1,15 @@
import os
import time as t
struct Foo {
bar t.Time
bars []t.Time
barref &t.Time
barrefs []&t.Time
c_type C.some_struct
js_type JS.other_struct
}
fn main() {
println('start')
t.sleep_ms(500)