@@ -164,12 +164,42 @@ fn stringify_fn_after_name(node &FnDecl, mut f strings.Builder, t &Table, cur_mo
164164 }
165165 f.write_string (')' )
166166 if node.return_type != void_type {
167- mut rs := util.no_cur_mod (t.type_to_str (node.return_type), cur_mod)
168- for mod, alias in m2 a {
169- rs = rs.replace (mod, alias)
167+ sreturn_type := util.no_cur_mod (t.type_to_str (node.return_type), cur_mod)
168+ short_sreturn_type := shorten_full_name_based_on_aliases (sreturn_type, m2 a)
169+ f.write_string (' ' + short_sreturn_type)
170+ }
171+ }
172+
173+ struct StringifyModReplacement {
174+ mod string
175+ alias string
176+ weight int
177+ }
178+
179+ fn shorten_full_name_based_on_aliases (input string , m2 a map [string ]string ) string {
180+ // Shorten the full names to their aliases, but replace the longer mods first, so that:
181+ // `import user.project`
182+ // `import user.project.routes`
183+ // will lead to replacing `user.project.routes` first to `routes`, NOT `user.project.routes` to `project.routes`.
184+ // Also take into account the nesting level, so `a.e.c.d` will be shortened before `a.xyz.b`, even though they are the same length.
185+ mut replacements := []StringifyModReplacement{cap: m2 a.len}
186+ for mod, alias in m2 a {
187+ if input.contains (mod) {
188+ replacements << StringifyModReplacement{
189+ mod: mod
190+ alias: alias
191+ weight: mod.count ('.' ) * 100 + mod.len
192+ }
193+ }
194+ }
195+ mut res := input.clone ()
196+ if replacements.len > 0 {
197+ replacements.sort (a.weight > b.weight)
198+ for r in replacements {
199+ res = res.replace (r.mod, r.alias)
170200 }
171- f.write_string (' ' + rs)
172201 }
202+ return res
173203}
174204
175205// Expressions in string interpolations may have to be put in braces if they
0 commit comments