summaryrefslogtreecommitdiff
path: root/lua/telescope/pickers.lua
diff options
context:
space:
mode:
authorDhruv Manilawala <dhruvmanila@gmail.com>2021-06-09 23:28:36 +0530
committerGitHub <noreply@github.com>2021-06-09 19:58:36 +0200
commit495f84fd35e7b5bb0bb670e232725924b3b85b88 (patch)
treea975832ce28ea2f53fb27a60ddc2609652b3cf29 /lua/telescope/pickers.lua
parent618e0e6075b4215e43c6a848daa37ef4e354b5dc (diff)
feat: delete entry from the picker without closing telescope (#828)
This action is not mapped but you can do it yourself in your telescope setup call Example config: ```lua require("telescope").setup { pickers = { buffers = { mappings = { i = { ["<c-d>"] = "delete_buffer", } } } } } ```
Diffstat (limited to 'lua/telescope/pickers.lua')
-rw-r--r--lua/telescope/pickers.lua62
1 files changed, 59 insertions, 3 deletions
diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua
index 7d0efae..9d1377a 100644
--- a/lua/telescope/pickers.lua
+++ b/lua/telescope/pickers.lua
@@ -491,6 +491,62 @@ function Picker:hide_preview()
-- 2. Resize prompt & results windows accordingly
end
+-- TODO: update multi-select with the correct tag name when available
+--- A simple interface to remove an entry from the results window without
+--- closing telescope. This either deletes the current selection or all the
+--- selections made using multi-select. It can be used to define actions
+--- such as deleting buffers or files.
+---
+--- Example usage:
+--- <pre>
+--- actions.delete_something = function(prompt_bufnr)
+--- local current_picker = action_state.get_current_picker(prompt_bufnr)
+--- current_picker:delete_selection(function(selection)
+--- -- delete the selection outside of telescope
+--- end)
+--- end
+--- </pre>
+---
+--- Example usage in telescope:
+--- - `actions.delete_buffer()`
+---@param delete_cb function: called with each deleted selection
+function Picker:delete_selection(delete_cb)
+ vim.validate { delete_cb = { delete_cb, "f" } }
+ local original_selection_strategy = self.selection_strategy
+ self.selection_strategy = "row"
+
+ local delete_selections = self._multi:get()
+ local used_multi_select = true
+ if vim.tbl_isempty(delete_selections) then
+ table.insert(delete_selections, self:get_selection())
+ used_multi_select = false
+ end
+
+ local selection_index = {}
+ for result_index, result_entry in ipairs(self.finder.results) do
+ if vim.tbl_contains(delete_selections, result_entry) then
+ table.insert(selection_index, result_index)
+ end
+ end
+
+ -- Sort in reverse order as removing an entry from the table shifts down the
+ -- other elements to close the hole.
+ table.sort(selection_index, function(x, y) return x > y end)
+ for _, index in ipairs(selection_index) do
+ local selection = table.remove(self.finder.results, index)
+ delete_cb(selection)
+ end
+
+ if used_multi_select then
+ self._multi = MultiSelect:new()
+ end
+
+ self:refresh()
+ vim.schedule(function()
+ self.selection_strategy = original_selection_strategy
+ end)
+end
+
function Picker.close_windows(status)
local prompt_win = status.prompt_win
@@ -639,10 +695,10 @@ function Picker:refresh(finder, opts)
end
if opts.reset_prompt then self:reset_prompt() end
- self.finder:close()
if finder then
- self.finder = finder
- self._multi = MultiSelect:new()
+ self.finder:close()
+ self.finder = finder
+ self._multi = MultiSelect:new()
end
self.__on_lines(nil, nil, nil, 0, 1)