From 7418f3000958c9e1d69360551710134c8bff0664 Mon Sep 17 00:00:00 2001 From: Alan Martinovic Date: Sun, 22 May 2022 21:04:05 +0200 Subject: [PATCH 1/3] Force buffer removal for terminal --- lua/telescope/actions/init.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index 4d35cf1185..8bc4ca8893 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -1020,7 +1020,9 @@ end actions.delete_buffer = function(prompt_bufnr) local current_picker = action_state.get_current_picker(prompt_bufnr) current_picker:delete_selection(function(selection) - vim.api.nvim_buf_delete(selection.bufnr, { force = false }) + local force = vim.api.nvim_buf_get_option(selection.bufnr, "buftype") == "terminal" + local success, function_return = pcall(vim.api.nvim_buf_delete, selection.bufnr, { force = force }) + return success end) end From 708b7c44c125e4c7ff95330bfb09b1f1f452beac Mon Sep 17 00:00:00 2001 From: Alan Martinovic Date: Sun, 22 May 2022 21:04:15 +0200 Subject: [PATCH 2/3] Don't delete selection if callback function returned nil The motivation behind this logic came from the use case of deleting buffers. Before this commit this was the behaviour: * try to delete a buffer which wasn't saved * error pops up, buffer not deleted * regardless of the error the selection gets removed from telescope's list After this commit: * try to delete a buffer which wasn't saved * error pops up, buffer not deleted * regardless of the error the selection gets removed from telescope's --- lua/telescope/pickers.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 76fdb5153e..9a5c53198b 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -718,8 +718,11 @@ function Picker:delete_selection(delete_cb) return x > y end) for _, index in ipairs(selection_index) do - local selection = table.remove(self.finder.results, index) - delete_cb(selection) + local selection = self.finder.results[index] + local delete_cb_return = delete_cb(selection) + if delete_cb_return then + local selection = table.remove(self.finder.results, index) + end end if used_multi_select then From 055e006df6c34f6601a636c56d5b187d6fa7a0a4 Mon Sep 17 00:00:00 2001 From: Simon Hauser Date: Sun, 29 May 2022 19:46:34 +0200 Subject: [PATCH 3/3] chore: cleanup, docs and no longer breaking change --- lua/telescope/actions/init.lua | 4 ++-- lua/telescope/pickers.lua | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index 8bc4ca8893..7f7c349da8 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -1021,8 +1021,8 @@ actions.delete_buffer = function(prompt_bufnr) local current_picker = action_state.get_current_picker(prompt_bufnr) current_picker:delete_selection(function(selection) local force = vim.api.nvim_buf_get_option(selection.bufnr, "buftype") == "terminal" - local success, function_return = pcall(vim.api.nvim_buf_delete, selection.bufnr, { force = force }) - return success + local ok = pcall(vim.api.nvim_buf_delete, selection.bufnr, { force = force }) + return ok end) end diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 9a5c53198b..46592906bb 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -692,7 +692,7 @@ end --- --- Example usage in telescope: --- - `actions.delete_buffer()` ----@param delete_cb function: called with each deleted selection +---@param delete_cb function: called for each selection fn(s) -> bool|nil (true|nil removes the entry from the results) function Picker:delete_selection(delete_cb) vim.validate { delete_cb = { delete_cb, "f" } } local original_selection_strategy = self.selection_strategy @@ -718,10 +718,9 @@ function Picker:delete_selection(delete_cb) return x > y end) for _, index in ipairs(selection_index) do - local selection = self.finder.results[index] - local delete_cb_return = delete_cb(selection) - if delete_cb_return then - local selection = table.remove(self.finder.results, index) + local delete_cb_return = delete_cb(self.finder.results[index]) + if delete_cb_return == nil or delete_cb_return == true then + table.remove(self.finder.results, index) end end