Skip to content

Commit bfcc7d5

Browse files
authored
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`).
1 parent 7bd2f9b commit bfcc7d5

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

lua/telescope/actions/init.lua

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ actions.edit_register = function(prompt_bufnr)
462462
v.content = updated_value
463463
end
464464
end
465-
-- print(vim.inspect(picker.finder.results))
466465
end
467466

468467
--- Paste the selected register into the buffer
@@ -1184,13 +1183,28 @@ actions.delete_buffer = function(prompt_bufnr)
11841183
-- If the current buffer is deleted, switch to the previous buffer
11851184
-- according to bdelete behavior
11861185
if ok and selection.bufnr == current_picker.original_bufnr then
1187-
local jumplist = vim.fn.getjumplist(current_picker.original_win_id)[1]
1188-
for i = #jumplist, 1, -1 do
1189-
if jumplist[i].bufnr ~= selection.bufnr and vim.fn.bufloaded(jumplist[i].bufnr) == 1 then
1190-
vim.api.nvim_win_set_buf(current_picker.original_win_id, jumplist[i].bufnr)
1191-
break
1186+
if vim.api.nvim_win_is_valid(current_picker.original_win_id) then
1187+
local jumplist = vim.fn.getjumplist(current_picker.original_win_id)[1]
1188+
for i = #jumplist, 1, -1 do
1189+
if jumplist[i].bufnr ~= selection.bufnr and vim.fn.bufloaded(jumplist[i].bufnr) == 1 then
1190+
vim.api.nvim_win_set_buf(current_picker.original_win_id, jumplist[i].bufnr)
1191+
current_picker.original_bufnr = jumplist[i].bufnr
1192+
return ok
1193+
end
11921194
end
1195+
1196+
-- no more valid buffers in jumplist, create an empty buffer
1197+
local empty_buf = vim.api.nvim_create_buf(true, true)
1198+
vim.api.nvim_win_set_buf(current_picker.original_win_id, empty_buf)
1199+
current_picker.original_bufnr = empty_buf
1200+
vim.api.nvim_buf_delete(selection.bufnr, { force = true })
1201+
return ok
11931202
end
1203+
1204+
-- window of the selected buffer got wiped, switch to first valid window
1205+
local win_id = vim.fn.win_getid(1, current_picker.original_tabpage)
1206+
current_picker.original_win_id = win_id
1207+
current_picker.original_bufnr = vim.api.nvim_win_get_buf(win_id)
11941208
end
11951209
return ok
11961210
end)

lua/telescope/pickers.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ function Picker:find()
537537

538538
self.original_bufnr = a.nvim_get_current_buf()
539539
self.original_win_id = a.nvim_get_current_win()
540+
self.original_tabpage = a.nvim_get_current_tabpage()
540541
_, self.original_cword = pcall(vim.fn.expand, "<cword>")
541542
_, self.original_cWORD = pcall(vim.fn.expand, "<cWORD>")
542543
_, self.original_cfile = pcall(vim.fn.expand, "<cfile>")

0 commit comments

Comments
 (0)