From 0f4b8c92061be5dfd0bdc6c2d79d2623929813e6 Mon Sep 17 00:00:00 2001 From: Jaehaks Date: Wed, 15 May 2024 19:18:55 +0900 Subject: [PATCH 1/2] fix(oldfiles) : delete duplicated items in Windows /// Problem : 1) Duplicated items are listed in oldfiles because of neovim problem. Example is like below. c:\Users\USERS\test\telescope.lua c:\Users\USERS/test/telescope.lua c:/Users/USERS/test/telescope.lua slash(/) makes neovim lua api cannot work properly in windows. and string comparison don't distinguish it is the same path This makes `file ~= current_file` condition cannot filter although it is same with the file 2) oldfiles show current session files also. /// Solution : 1-1) Taking gsub('/', '\\') all files from neovim lua api. 1-2) Add additional condition which checks whether the file is listed in results (='results_other') 2) separate `results` variable to 2 parts. First, 'results' as current session file list Second, 'results_other' as old file list which are filtered unnecessary file --- lua/telescope/builtin/__internal.lua | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lua/telescope/builtin/__internal.lua b/lua/telescope/builtin/__internal.lua index 21735a7d64..c01db94d01 100644 --- a/lua/telescope/builtin/__internal.lua +++ b/lua/telescope/builtin/__internal.lua @@ -525,17 +525,24 @@ 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 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 @@ -543,12 +550,25 @@ internal.oldfiles = function(opts) 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 From 7c8f0c8f6bc67a69d071943c07ad35a0a7d77432 Mon Sep 17 00:00:00 2001 From: Jaehaks Date: Sat, 6 Jul 2024 10:08:19 +0900 Subject: [PATCH 2/2] fix(oldfiles) : remove unnecessary comment, use utils.iswin 1) remove unnecessary comments 2) formatting with stylua 3) use utils.iswin instead of vim.fn.has('win32') --- lua/telescope/builtin/__internal.lua | 35 +++++++++++++--------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/lua/telescope/builtin/__internal.lua b/lua/telescope/builtin/__internal.lua index 2a2e4634aa..0c894a3a62 100644 --- a/lua/telescope/builtin/__internal.lua +++ b/lua/telescope/builtin/__internal.lua @@ -525,24 +525,23 @@ 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 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 utils.iswin then + current_file = current_file:gsub("/", "\\") + end if opts.include_current_session then for _, buffer in ipairs(utils.split_lines(vim.fn.execute ":buffers! t")) 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 utils.iswin then + file = file:gsub("/", "\\") + end if vim.loop.fs_stat(file) and match ~= current_buffer then table.insert(results, file) end @@ -550,21 +549,19 @@ internal.oldfiles = function(opts) 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 + if utils.iswin 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 not vim.tbl_contains(results_other, file) and file ~= current_file then + 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