Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(oldfiles) : delete duplicated items in Windows #3103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lua/telescope/builtin/__internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -525,30 +525,50 @@ end
internal.oldfiles = function(opts)
opts = apply_cwd_only_aliases(opts)
opts.include_current_session = vim.F.if_nil(opts.include_current_session, true)
local has_win = vim.fn.has('win32') == 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use utils.iswin()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I'll try it.


local current_buffer = vim.api.nvim_get_current_buf()
local current_file = vim.api.nvim_buf_get_name(current_buffer)
local results = {}

-- add gsub() : if output of nvim_buf_get_name() has '/' in the string, file~=current_file cannot filter the path properly
if has_win then current_file = current_file:gsub('/','\\') end

-- in here, results has absolute paths in current open session
if opts.include_current_session then
for _, buffer in ipairs(vim.split(vim.fn.execute ":buffers! t", "\n")) do
local match = tonumber(string.match(buffer, "%s*(%d+)"))
local open_by_lsp = string.match(buffer, "line 0$")
if match and not open_by_lsp then
local file = vim.api.nvim_buf_get_name(match)
-- add gsub() : if output of nvim_buf_get_name() has '/' in the string, file~=current_file cannot filter the path properly
if has_win then file = file:gsub('/','\\') end
if vim.loop.fs_stat(file) and match ~= current_buffer then
table.insert(results, file)
end
end
end
end

-- in here, add path in oldfiles which is not included in current session, current file
-- (Problem)Previous code adds the list to the 'results' variable which includes files in current session already.
-- It makes the oldfiles picker show files which are loaded current session.
-- (Solution)so 'results' and 'results_other' are separated
--
-- (Problem) sometimes the list has duplicated directory.
-- (Solution) so I add a condition which checks whether the file are added in 'results_other'
local results_other = {}
for _, file in ipairs(vim.v.oldfiles) do
-- (problem) sometimes path in vim.v.oldfiles has '/' as delimiters caused by neovim.
-- (solution) the '/' replaced by '\\'
if has_win then file = file:gsub('/','\\') end
local file_stat = vim.loop.fs_stat(file)
if file_stat and file_stat.type == "file" and not vim.tbl_contains(results, file) and file ~= current_file then
table.insert(results, file)
if file_stat and file_stat.type == "file" and not vim.tbl_contains(results, file)
and not vim.tbl_contains(results_other, file) and file ~= current_file then
table.insert(results_other, file)
end
end
results = results_other

if opts.cwd_only or opts.cwd then
local cwd = opts.cwd_only and vim.loop.cwd() or opts.cwd
Expand Down
Loading