From 9b139c58ca0db29b9dea9c4f099884620e2fced2 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Mon, 14 Dec 2020 05:03:43 +0100 Subject: [PATCH] fmt: keep module alias in struct decl field types (#7305) --- vlib/v/fmt/fmt.v | 20 +++++++++++++++++--- vlib/v/fmt/tests/import_with_alias_keep.vv | 9 +++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 92dcce9296..4fd17cd3bf 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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) +} diff --git a/vlib/v/fmt/tests/import_with_alias_keep.vv b/vlib/v/fmt/tests/import_with_alias_keep.vv index db8f4d3d8d..e9d6f20404 100644 --- a/vlib/v/fmt/tests/import_with_alias_keep.vv +++ b/vlib/v/fmt/tests/import_with_alias_keep.vv @@ -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)