Skip to content

Commit

Permalink
vfmt: fix formatting of submodules with common prefixes (fix #15582)
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Aug 29, 2022
1 parent 1915bf8 commit 9703410
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
38 changes: 34 additions & 4 deletions vlib/v/ast/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,42 @@ fn stringify_fn_after_name(node &FnDecl, mut f strings.Builder, t &Table, cur_mo
}
f.write_string(')')
if node.return_type != void_type {
mut rs := util.no_cur_mod(t.type_to_str(node.return_type), cur_mod)
for mod, alias in m2a {
rs = rs.replace(mod, alias)
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 {
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)
}
f.write_string(' ' + rs)
}
return res
}

// Expressions in string interpolations may have to be put in braces if they
Expand Down
16 changes: 16 additions & 0 deletions vlib/v/fmt/tests/shorten_longer_module_paths_first_keep.vv
Original file line number Diff line number Diff line change
@@ -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'
}
}

0 comments on commit 9703410

Please sign in to comment.