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

feat(builtin.buffers): enhance and bind delete_buffer action #3145

Merged
merged 3 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1176,9 +1176,22 @@ end
---@param prompt_bufnr number: The prompt bufnr
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 ok = pcall(vim.api.nvim_buf_delete, selection.bufnr, { force = force })

-- If the current buffer is deleted, switch to the previous buffer
-- according to bdelete behavior
if ok and selection.bufnr == current_picker.original_bufnr then
local jumplist = vim.fn.getjumplist(current_picker.original_win_id)[1]
for i = #jumplist, 1, -1 do
if jumplist[i].bufnr ~= selection.bufnr and vim.fn.bufloaded(jumplist[i].bufnr) == 1 then
vim.api.nvim_win_set_buf(current_picker.original_win_id, jumplist[i].bufnr)
break
end
end
end
return ok
end)
end
Expand Down
3 changes: 3 additions & 0 deletions lua/telescope/builtin/__internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,9 @@ internal.buffers = function(opts)
previewer = conf.grep_previewer(opts),
sorter = conf.generic_sorter(opts),
default_selection_index = default_selection_idx,
attach_mappings = function(_, map)
map({ "i", "n" }, "<C-x>", actions.delete_buffer)
end,
jamestrew marked this conversation as resolved.
Show resolved Hide resolved
})
:find()
end
Expand Down
1 change: 1 addition & 0 deletions lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ function Picker:find()
self.__original_mousemoveevent = vim.o.mousemoveevent
vim.o.mousemoveevent = true

self.original_bufnr = a.nvim_get_current_buf()
self.original_win_id = a.nvim_get_current_win()
_, self.original_cword = pcall(vim.fn.expand, "<cword>")
_, self.original_cWORD = pcall(vim.fn.expand, "<cWORD>")
Expand Down