diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-07-05 15:46:19 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-07-05 18:27:06 +0900 |
| commit | 8ca6a8005d7515ebc54622f4594bb6073e33c285 (patch) | |
| tree | a115a6c2f04a04aa6589712ca010db2558b90126 | |
| parent | 5ef9ab4ac146adcdf0b94d819ac535107c386409 (diff) | |
| download | dotfiles-8ca6a8005d7515ebc54622f4594bb6073e33c285.tar.gz dotfiles-8ca6a8005d7515ebc54622f4594bb6073e33c285.tar.zst dotfiles-8ca6a8005d7515ebc54622f4594bb6073e33c285.zip | |
nvim: fix uniquify
| -rw-r--r-- | .config/nvim/lua/init/05-appearance.lua | 4 | ||||
| -rw-r--r-- | .config/nvim/lua/tests/init.lua | 2 | ||||
| -rw-r--r-- | .config/nvim/lua/tests/uniquify.lua | 8 | ||||
| -rw-r--r-- | .config/nvim/lua/uniquify.lua | 54 |
4 files changed, 32 insertions, 36 deletions
diff --git a/.config/nvim/lua/init/05-appearance.lua b/.config/nvim/lua/init/05-appearance.lua index 789ceb9..0ced8b9 100644 --- a/.config/nvim/lua/init/05-appearance.lua +++ b/.config/nvim/lua/init/05-appearance.lua @@ -114,11 +114,11 @@ function vimrc.statusline.filename(bufnr) return '*scratch*' end - local this_path = F.expand(('#%s:p'):format(bufnr)) + local this_path = F.expand(('#%d:p'):format(bufnr)) local other_paths = {} for b = 1, F.bufnr('$') do if F.bufexists(b) and b ~= bufnr then - other_paths[#other_paths+1] = F.bufname(b) + other_paths[#other_paths+1] = F.expand(('#%d:p'):format(b)) end end diff --git a/.config/nvim/lua/tests/init.lua b/.config/nvim/lua/tests/init.lua index 210ca0d..733f68c 100644 --- a/.config/nvim/lua/tests/init.lua +++ b/.config/nvim/lua/tests/init.lua @@ -1,4 +1,4 @@ --- lua require('tests').test(TEST_SUITE_NAME) +-- :lua require('tests').test(TEST_SUITE_NAME) local M = {} diff --git a/.config/nvim/lua/tests/uniquify.lua b/.config/nvim/lua/tests/uniquify.lua index a8a190f..46f7ff8 100644 --- a/.config/nvim/lua/tests/uniquify.lua +++ b/.config/nvim/lua/tests/uniquify.lua @@ -9,15 +9,19 @@ function M.test(t) {'foo.txt', 'foo.txt', {}}, {'foo.txt', 'bar/foo.txt', {}}, {'foo.txt', 'foo.txt', {'foo.txt'}}, + {'foo.txt', 'bar/foo.txt', {'bar/foo.txt'}}, + {'foo.txt', 'bar/foo.txt', {'bar/foo.txt', 'bar/foo.txt'}}, {'bar/foo.txt', 'bar/foo.txt', {'foo.txt'}}, {'bar/foo.txt', 'bar/foo.txt', {'baz/foo.txt'}}, + {'bar/foo.txt', 'qux/bar/foo.txt', {'baz/foo.txt'}}, {'foo.txt', 'bar/foo.txt', {'bar.txt', 'baz.txt'}}, {'foo.txt', 'bar/foo.txt', {'bar.txt', 'baz.txt'}}, {'baz.txt', 'foo/bar/baz.txt', {}}, - {'foo/b/baz.txt', 'foo/bar/baz.txt', {'spam/bar/baz.txt'}}, - {'foo/b/baz.txt', 'foo/bar/baz.txt', {'fiz/foo/bar/baz.txt', 'spam/bar/baz.txt'}}, + {'foo/b/baz.txt', 'foo/bar/baz.txt', {'qux/bar/baz.txt'}}, + {'foo/b/baz.txt', 'foo/bar/baz.txt', {'fiz/foo/bar/baz.txt', 'qux/bar/baz.txt'}}, {'fiz/f/b/baz.txt', 'fiz/foo/bar/baz.txt', {'foo/bar/baz.txt'}}, {'foo.txt', 'bar/foo.txt', {'bar/foo.txt'}}, + {'bar/foo.txt', 'fiz/bar/foo.txt', {'bar/baz.txt', 'qux/foo.txt'}}, } for _, row in ipairs(test_cases) do diff --git a/.config/nvim/lua/uniquify.lua b/.config/nvim/lua/uniquify.lua index c7f8482..5b0c7df 100644 --- a/.config/nvim/lua/uniquify.lua +++ b/.config/nvim/lua/uniquify.lua @@ -8,11 +8,10 @@ local stricmp = vim.stricmp function M.uniquify(this_path, other_paths) - for i = 1, #other_paths do - if other_paths[i] == this_path then - table.remove(other_paths, i) - end - end + -- Filter out the same paths. + other_paths = vim.iter(other_paths) + :filter(function(p) return p ~= this_path end) + :totable() -- Split each path into slash-separated parts. for i = 1, #other_paths do @@ -20,46 +19,39 @@ function M.uniquify(this_path, other_paths) end this_path = split(this_path, '[\\/]') - local i = 0 - while true do - local this_path_part = this_path[#this_path+i] or '' + local depth = 0 + while depth < #this_path-1 do local unique = true - local no_parts_remained = true for _, other_path in ipairs(other_paths) do - local other_path_part = other_path[#other_path+i] or '' - if stricmp(this_path_part, other_path_part) == 0 then + local same = true + for i = 0, depth do + local this_path_part = this_path[#this_path-i] + local other_path_part = other_path[#other_path-i] or '' + if stricmp(this_path_part, other_path_part) ~= 0 then + same = false + break + end + end + if same then unique = false break end - if other_path_part ~= '' then - no_parts_remained = false - end end if unique then break end - if this_path_part == '' and no_parts_remained then - break - end - i = i - 1 - end - - if i <= -(#this_path) then - i = -(#this_path) + 1 + depth = depth+1 end - local ret = '' - for k = i, 0 do - if #this_path < 1 - k then - break - end - if k == i or k == 0 then - ret = ret .. '/' .. this_path[#this_path+k] + local ret = {} + for i = depth, 0, -1 do + if i == depth or i == 0 then + ret[#ret+1] = this_path[#this_path-i] else - ret = ret .. '/' .. matchlist(this_path[#this_path+k], '.')[1] + ret[#ret+1] = matchlist(this_path[#this_path-i], '.')[1] end end - return ret:sub(2) + return table.concat(ret, '/') end |
