1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vfmt: fix formatting of submodules with common prefixes (fix #15582)

This commit is contained in:
Delyan Angelov 2022-08-29 15:30:50 +03:00
parent 1915bf81d0
commit 9703410391
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 51 additions and 5 deletions

View File

@ -164,14 +164,44 @@ fn stringify_fn_after_name(node &FnDecl, mut f strings.Builder, t &Table, cur_mo
} }
f.write_string(')') f.write_string(')')
if node.return_type != void_type { if node.return_type != void_type {
mut rs := util.no_cur_mod(t.type_to_str(node.return_type), cur_mod) sreturn_type := util.no_cur_mod(t.type_to_str(node.return_type), cur_mod)
for mod, alias in m2a { short_sreturn_type := shorten_full_name_based_on_aliases(sreturn_type, m2a)
rs = rs.replace(mod, alias) f.write_string(' ' + short_sreturn_type)
}
f.write_string(' ' + rs)
} }
} }
struct StringifyModReplacement {
mod string
alias string
weight int
}
fn shorten_full_name_based_on_aliases(input string, m2a map[string]string) string {
// Shorten the full names to their aliases, but replace the longer mods first, so that:
// `import user.project`
// `import user.project.routes`
// will lead to replacing `user.project.routes` first to `routes`, NOT `user.project.routes` to `project.routes`.
// 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.
mut replacements := []StringifyModReplacement{cap: m2a.len}
for mod, alias in m2a {
if input.contains(mod) {
replacements << StringifyModReplacement{
mod: mod
alias: alias
weight: mod.count('.') * 100 + mod.len
}
}
}
mut res := input.clone()
if replacements.len > 0 {
replacements.sort(a.weight > b.weight)
for r in replacements {
res = res.replace(r.mod, r.alias)
}
}
return res
}
// Expressions in string interpolations may have to be put in braces if they // Expressions in string interpolations may have to be put in braces if they
// are non-trivial, if they would interfere with the next character or if a // are non-trivial, if they would interfere with the next character or if a
// format specification is given. In the latter case // format specification is given. In the latter case

View File

@ -0,0 +1,16 @@
module app
import user.project
import user.project.routes
pub fn new_app() ?project.Application {
return project.Application{
data: get_router()!.route_name
}
}
fn get_router() ?routes.Router {
return routes.Router{
route_name: 'Root'
}
}