From 0847120c40c377129beca6c8c14164a05a679e76 Mon Sep 17 00:00:00 2001 From: Sofronie Cristian <53446505+cristiansofronie@users.noreply.github.com> Date: Sun, 3 Sep 2023 23:42:09 +0300 Subject: [PATCH] fix(builtin.pickers): fix wrong picker resuming when using filtering (#2682) When filtering is applied, `picker:get_index(picker:get_selection_row())` returns index relative to the filtered entry list rather than the original full results. This causes inaccurate indexing into the `cache_pickers` table. (cherry picked from commit 74be3c3bba1abbf72f3eabeac95a7dddaca87f93) --- lua/telescope/actions/init.lua | 19 ++++++++++++++++--- lua/telescope/builtin/__internal.lua | 16 ++++++++++++++-- lua/telescope/utils.lua | 26 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index 6d3e4f7..ec4e790 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -1060,12 +1060,25 @@ end --- This action is not mapped by default and only intended for |builtin.pickers|. ---@param prompt_bufnr number: The prompt bufnr actions.remove_selected_picker = function(prompt_bufnr) - local current_picker = action_state.get_current_picker(prompt_bufnr) - local selection_index = current_picker:get_index(current_picker:get_selection_row()) + local curr_picker = action_state.get_current_picker(prompt_bufnr) + local curr_entry = action_state.get_selected_entry() local cached_pickers = state.get_global_key "cached_pickers" - current_picker:delete_selection(function() + + if not curr_entry then + return + end + + local selection_index, _ = utils.list_find(function(v) + if curr_entry.value == v.value then + return true + end + return false + end, curr_picker.finder.results) + + curr_picker:delete_selection(function() table.remove(cached_pickers, selection_index) end) + if #cached_pickers == 0 then actions.close(prompt_bufnr) end diff --git a/lua/telescope/builtin/__internal.lua b/lua/telescope/builtin/__internal.lua index f64d8a6..c0d7aa2 100644 --- a/lua/telescope/builtin/__internal.lua +++ b/lua/telescope/builtin/__internal.lua @@ -194,9 +194,21 @@ internal.pickers = function(opts) cache_picker = false, attach_mappings = function(_, map) actions.select_default:replace(function(prompt_bufnr) - local current_picker = action_state.get_current_picker(prompt_bufnr) - local selection_index = current_picker:get_index(current_picker:get_selection_row()) + local curr_picker = action_state.get_current_picker(prompt_bufnr) + local curr_entry = action_state.get_selected_entry() + if not curr_entry then + return + end + actions.close(prompt_bufnr) + + local selection_index, _ = utils.list_find(function(v) + if curr_entry.value == v.value then + return true + end + return false + end, curr_picker.finder.results) + opts.cache_picker = opts._cache_picker opts["cache_index"] = selection_index opts["initial_mode"] = cached_pickers[selection_index].initial_mode diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index a279993..a5486e8 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -555,4 +555,30 @@ utils.__warn_no_selection = function(name) }) end +--- Generate git command optionally with git env variables +---@param args string[] +---@param opts? table +---@return string[] +utils.__git_command = function(args, opts) + opts = opts or {} + + local _args = { "git" } + if opts.gitdir then + vim.list_extend(_args, { "--git-dir", opts.gitdir }) + end + if opts.toplevel then + vim.list_extend(_args, { "--work-tree", opts.toplevel }) + end + + return vim.list_extend(_args, args) +end + +utils.list_find = function(func, list) + for i, v in ipairs(list) do + if func(v, i, list) then + return i, v + end + end +end + return utils