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:
parent
1915bf81d0
commit
9703410391
@ -164,12 +164,42 @@ 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)
|
||||||
|
short_sreturn_type := shorten_full_name_based_on_aliases(sreturn_type, m2a)
|
||||||
|
f.write_string(' ' + short_sreturn_type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
for mod, alias in m2a {
|
||||||
rs = rs.replace(mod, alias)
|
if input.contains(mod) {
|
||||||
|
replacements << StringifyModReplacement{
|
||||||
|
mod: mod
|
||||||
|
alias: alias
|
||||||
|
weight: mod.count('.') * 100 + mod.len
|
||||||
}
|
}
|
||||||
f.write_string(' ' + rs)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
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
|
||||||
|
16
vlib/v/fmt/tests/shorten_longer_module_paths_first_keep.vv
Normal file
16
vlib/v/fmt/tests/shorten_longer_module_paths_first_keep.vv
Normal 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'
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user