I was unsatisfied of how the IDE diffirentiates between opened files with the same name. So I came up with this. This was coded in lua with some Neovim API. So it would need some adjustments.
I'm aware that this an opinionated (why not show ? for hidden root folders) solution and there is probably some algorithm out there with a fancy name that does the same thing... so feel free to adjust it to your needs.
I've seen a VSCode solution would do
'a\b\c\d', 'a\f\b\c\d' -> 'a\b\...', '...\f\...' vs 'a\...', 'f\...' (mine)
'a\b\a', 'b\b\a' -> 'a\...', 'b\b\...' vs 'a\...' , 'b\...'
(Other than their running time of
input = {
["0"] = "/home/f4",
["1"] = "/home/f1/f2",
["2"] = "/b2/home/f1/f2",
["3"] = "/b3/home/f1/f2",
["4"] = "/b1/b2/home/f1/f2",
["5"] = "/home/b2/f3",
["6"] = "/f3",
["7"] = "/home/b3/f3",
["8"] = "/b4/lome/f1/f2",
["9"] = "/b4/gome/f1/f2"
}
First, I did some endoding to translate the paths to short strings in reverse order:
input = {
["0"] = "KE ",
["1"] = "DCE ",
["2"] = "DCEJ ",
["3"] = "DCEG ",
["4"] = "DCEJI",
["5"] = "HJE ",
["6"] = "H ",
["7"] = "HGE ",
["8"] = "DCFA ",
["9"] = "DCBA "
}
- We recursively group by first letter, it the group has one element, then we stop,
- otherwise we recurse:
- If a group member reached its end we keep the first element and we go to differentiate between the other elements
- Since all elements are unique, we reach a level where all groups have 1 element, so the recursion should terminate
-- encoded shorts
{
firsts = { "D", "I", "K" },
hash = {
["0"] = "K",
["1"] = "?B",
["2"] = "?BA",
["3"] = "?BH",
["4"] = "?BAJ",
["5"] = "IA",
["6"] = "I",
["7"] = "IH",
["8"] = "?G",
["9"] = "?F"
}
}
-- reduce_paths(input)
{
["0"] = "f4",
["1"] = "home/?",
["2"] = "b2/home/?",
["3"] = "b3/home/?",
["4"] = "b1/b2/home/?",
["5"] = "b2/f3",
["6"] = "f3",
["7"] = "b3/f3",
["8"] = "lome/?",
["9"] = "gome/?"
}