feat(builtin.buffers): enhance and bind delete_buffer action (#3145)
* feat(builtin.buffers): enhance and bind `delete_buffer` action * change default bind and add docs * [docgen] Update doc/telescope.txt skip-checks: true --------- Co-authored-by: Github Actions <actions@github>
This commit is contained in:
@@ -1434,6 +1434,8 @@ builtin.reloader({opts}) *telescope.builtin.reloader()*
|
|||||||
builtin.buffers({opts}) *telescope.builtin.buffers()*
|
builtin.buffers({opts}) *telescope.builtin.buffers()*
|
||||||
Lists open buffers in current neovim instance, opens selected buffer on
|
Lists open buffers in current neovim instance, opens selected buffer on
|
||||||
`<cr>`
|
`<cr>`
|
||||||
|
- Default keymaps:
|
||||||
|
- `<M-d>`: delete the currently selected buffer
|
||||||
|
|
||||||
|
|
||||||
Parameters: ~
|
Parameters: ~
|
||||||
|
|||||||
@@ -1176,9 +1176,22 @@ end
|
|||||||
---@param prompt_bufnr number: The prompt bufnr
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
actions.delete_buffer = function(prompt_bufnr)
|
actions.delete_buffer = function(prompt_bufnr)
|
||||||
local current_picker = action_state.get_current_picker(prompt_bufnr)
|
local current_picker = action_state.get_current_picker(prompt_bufnr)
|
||||||
|
|
||||||
current_picker:delete_selection(function(selection)
|
current_picker:delete_selection(function(selection)
|
||||||
local force = vim.api.nvim_buf_get_option(selection.bufnr, "buftype") == "terminal"
|
local force = vim.api.nvim_buf_get_option(selection.bufnr, "buftype") == "terminal"
|
||||||
local ok = pcall(vim.api.nvim_buf_delete, selection.bufnr, { force = force })
|
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
|
return ok
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -972,6 +972,9 @@ internal.buffers = function(opts)
|
|||||||
previewer = conf.grep_previewer(opts),
|
previewer = conf.grep_previewer(opts),
|
||||||
sorter = conf.generic_sorter(opts),
|
sorter = conf.generic_sorter(opts),
|
||||||
default_selection_index = default_selection_idx,
|
default_selection_index = default_selection_idx,
|
||||||
|
attach_mappings = function(_, map)
|
||||||
|
map({ "i", "n" }, "<M-d>", actions.delete_buffer)
|
||||||
|
end,
|
||||||
})
|
})
|
||||||
:find()
|
:find()
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -339,6 +339,8 @@ builtin.man_pages = require_on_exported_call("telescope.builtin.__internal").man
|
|||||||
builtin.reloader = require_on_exported_call("telescope.builtin.__internal").reloader
|
builtin.reloader = require_on_exported_call("telescope.builtin.__internal").reloader
|
||||||
|
|
||||||
--- Lists open buffers in current neovim instance, opens selected buffer on `<cr>`
|
--- Lists open buffers in current neovim instance, opens selected buffer on `<cr>`
|
||||||
|
--- - Default keymaps:
|
||||||
|
--- - `<M-d>`: delete the currently selected buffer
|
||||||
---@param opts table: options to pass to the picker
|
---@param opts table: options to pass to the picker
|
||||||
---@field cwd string: specify a working directory to filter buffers list by
|
---@field cwd string: specify a working directory to filter buffers list by
|
||||||
---@field show_all_buffers boolean: if true, show all buffers, including unloaded buffers (default: true)
|
---@field show_all_buffers boolean: if true, show all buffers, including unloaded buffers (default: true)
|
||||||
|
|||||||
@@ -535,6 +535,7 @@ function Picker:find()
|
|||||||
self.__original_mousemoveevent = vim.o.mousemoveevent
|
self.__original_mousemoveevent = vim.o.mousemoveevent
|
||||||
vim.o.mousemoveevent = true
|
vim.o.mousemoveevent = true
|
||||||
|
|
||||||
|
self.original_bufnr = a.nvim_get_current_buf()
|
||||||
self.original_win_id = a.nvim_get_current_win()
|
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>")
|
||||||
_, self.original_cWORD = pcall(vim.fn.expand, "<cWORD>")
|
_, self.original_cWORD = pcall(vim.fn.expand, "<cWORD>")
|
||||||
|
|||||||
Reference in New Issue
Block a user