Skip to content

Commit 643345a

Browse files
tomtomjhjigorlfs
authored andcommitted
fix: properly handle buffer deletion
Problems * If dap-view buffer is :bwipeout-ed, subsequent close() raises invalid buffer error while attempting to nvim_buf_delete (= bwipeout) the buffer because the buffer is not removed from the state. * quit_buf_autocmd is weird. BufDelete is invoked when a buffer becomes unlisted. So, since dap-view buffer is already unlisted, closing it with commands like :q does not invoke the BufDelete autocmd. Solution: * Let close() check the validity of the buffer first. * Use BufWipeout autocmd. * Let the BufWipeout callback first remove the buffer from dap-view state to avoid deleting the buffer inside BufWipeout (E937).
1 parent 8e5ae13 commit 643345a

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

lua/dap-view/actions.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ M.close = function(hide_terminal)
2727
end
2828
if state.winnr and api.nvim_win_is_valid(state.winnr) then
2929
api.nvim_win_close(state.winnr, true)
30-
state.winnr = nil
3130
end
32-
if state.bufnr then
31+
state.winnr = nil
32+
if state.bufnr and api.nvim_buf_is_valid(state.bufnr) then
3333
api.nvim_buf_delete(state.bufnr, { force = true })
34-
state.bufnr = nil
3534
end
35+
state.bufnr = nil
3636
if hide_terminal then
3737
term.hide_term_buf_win()
3838
end
@@ -80,8 +80,12 @@ M.open = function()
8080
winbar.set_winbar_action_keymaps()
8181
winbar.show_content(state.current_section)
8282

83-
-- Properly handle deleting the buffer
84-
autocmd.quit_buf_autocmd(state.bufnr, M.close)
83+
-- Clean up states dap-view buffer is wiped out
84+
autocmd.quit_buf_autocmd(state.bufnr, function()
85+
-- The buffer is already being wiped out, so prevent close() from doing it again.
86+
state.bufnr = nil
87+
M.close()
88+
end)
8589
end
8690

8791
---@param expr? string

lua/dap-view/options/autocmd.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local api = vim.api
55
---@param bufnr integer
66
---@param callback fun(): nil
77
M.quit_buf_autocmd = function(bufnr, callback)
8-
api.nvim_create_autocmd("BufDelete", {
8+
api.nvim_create_autocmd("BufWipeout", {
99
buffer = bufnr,
1010
callback = callback,
1111
})

0 commit comments

Comments
 (0)