fix(action.delete_buffer): improve behavior with splits (#3194)

Previously, when having window splits, with deleting a buffer involving
deleting a window, getting the jumplist for said deleted window would
result in an invalid jumplist. Trying to iterate over this invalid
jumplist would error out.

When there are split, there's no need to find a valid buffer to switch
the current window to (as the window is deleted). Instead, what's needed
is the updating of telescope's `picker.original_win_id` state. This is
important for when chaining buffer deletes (ie. closing many splits).

Also improve behavior when the "current" buffer is the only valid buffer
-> it will now open an empty buffer (same as when doing `:bdelete`).
This commit is contained in:
James Trew
2024-07-01 15:06:50 -04:00
committed by GitHub
parent 7bd2f9b72f
commit bfcc7d5c6f
2 changed files with 21 additions and 6 deletions

View File

@@ -462,7 +462,6 @@ actions.edit_register = function(prompt_bufnr)
v.content = updated_value
end
end
-- print(vim.inspect(picker.finder.results))
end
--- Paste the selected register into the buffer
@@ -1184,13 +1183,28 @@ actions.delete_buffer = function(prompt_bufnr)
-- 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
if vim.api.nvim_win_is_valid(current_picker.original_win_id) 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)
current_picker.original_bufnr = jumplist[i].bufnr
return ok
end
end
-- no more valid buffers in jumplist, create an empty buffer
local empty_buf = vim.api.nvim_create_buf(true, true)
vim.api.nvim_win_set_buf(current_picker.original_win_id, empty_buf)
current_picker.original_bufnr = empty_buf
vim.api.nvim_buf_delete(selection.bufnr, { force = true })
return ok
end
-- window of the selected buffer got wiped, switch to first valid window
local win_id = vim.fn.win_getid(1, current_picker.original_tabpage)
current_picker.original_win_id = win_id
current_picker.original_bufnr = vim.api.nvim_win_get_buf(win_id)
end
return ok
end)