Skip to content

Commit

Permalink
fix: guard against invalid window ID errors (stevearc#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Sep 20, 2022
1 parent f7d6ca8 commit b3f8df4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 33 deletions.
45 changes: 25 additions & 20 deletions lua/aerial/autocommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,38 @@ end

local function update_aerial_windows()
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if not vim.api.nvim_win_is_valid(winid) then
goto continue
end
local winbuf = vim.api.nvim_win_get_buf(winid)
if util.is_aerial_buffer(winbuf) then
local close = false
if config.attach_mode == "global" then
window.open_aerial_in_win(0, 0, winid)
elseif config.attach_mode == "window" then
local src_win = util.get_source_win(winid)
if src_win then
local src_buf = vim.api.nvim_win_get_buf(src_win)

-- Close the aerial window if its source window has switched buffers
if config.close_automatic_events.switch_buffer then
if src_buf ~= util.get_source_buffer(winbuf) then
close = true
end
if not util.is_aerial_buffer(winbuf) then
goto continue
end
local close = false
if config.attach_mode == "global" then
window.open_aerial_in_win(0, 0, winid)
elseif config.attach_mode == "window" then
local src_win = util.get_source_win(winid)
if src_win then
local src_buf = vim.api.nvim_win_get_buf(src_win)

-- Close the aerial window if its source window has switched buffers
if config.close_automatic_events.switch_buffer then
if src_buf ~= util.get_source_buffer(winbuf) then
close = true
end
end

if util.get_source_win(winid) == vim.api.nvim_get_current_win() then
window.open_aerial_in_win(src_buf, src_win, winid)
end
if util.get_source_win(winid) == vim.api.nvim_get_current_win() then
window.open_aerial_in_win(src_buf, src_win, winid)
end
end
end

if close or should_close_aerial(winid) then
vim.api.nvim_win_close(winid, true)
end
if close or should_close_aerial(winid) then
vim.api.nvim_win_close(winid, true)
end
::continue::
end
end

Expand Down
2 changes: 1 addition & 1 deletion lua/aerial/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ local function _post_tree_mutate(bufnr, new_cursor_pos)
local _, aer_bufnr = util.get_buffers(bufnr)
if new_cursor_pos then
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if vim.api.nvim_win_get_buf(winid) == aer_bufnr then
if vim.api.nvim_win_is_valid(winid) and vim.api.nvim_win_get_buf(winid) == aer_bufnr then
vim.api.nvim_win_set_cursor(winid, { new_cursor_pos, 0 })
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/aerial/navigation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ local function get_target_win()
end
if winid == nil then
for _, tabwin in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if vim.api.nvim_win_get_buf(tabwin) == bufnr then
if vim.api.nvim_win_is_valid(tabwin) and vim.api.nvim_win_get_buf(tabwin) == bufnr then
winid = tabwin
break
end
Expand Down
8 changes: 6 additions & 2 deletions lua/aerial/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ end

M.buf_first_win_in_tabpage = function(bufnr)
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if vim.api.nvim_win_get_buf(winid) == bufnr then
if vim.api.nvim_win_is_valid(winid) and vim.api.nvim_win_get_buf(winid) == bufnr then
return winid
end
end
Expand Down Expand Up @@ -160,7 +160,11 @@ end
M.get_fixed_wins = function(bufnr)
local wins = {}
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if not M.is_floating_win(winid) and (not bufnr or vim.api.nvim_win_get_buf(winid) == bufnr) then
if
vim.api.nvim_win_is_valid(winid)
and not M.is_floating_win(winid)
and (not bufnr or vim.api.nvim_win_get_buf(winid) == bufnr)
then
table.insert(wins, winid)
end
end
Expand Down
28 changes: 19 additions & 9 deletions lua/aerial/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,12 @@ M.close = function()
-- look for other aerial windows and close the first
if backend == nil or not data:has_symbols(0) then
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
local winbuf = vim.api.nvim_win_get_buf(winid)
if util.is_aerial_buffer(winbuf) then
vim.api.nvim_win_close(winid, false)
break
if vim.api.nvim_win_is_valid(winid) then
local winbuf = vim.api.nvim_win_get_buf(winid)
if util.is_aerial_buffer(winbuf) then
vim.api.nvim_win_close(winid, false)
break
end
end
end
end
Expand All @@ -215,7 +217,9 @@ end

M.close_all = function()
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if util.is_aerial_buffer(vim.api.nvim_win_get_buf(winid)) then
if
vim.api.nvim_win_is_valid(winid) and util.is_aerial_buffer(vim.api.nvim_win_get_buf(winid))
then
vim.api.nvim_win_close(winid, false)
end
end
Expand All @@ -224,9 +228,11 @@ end
M.close_all_but_current = function()
local _, aer_winid = util.get_winids(0)
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
local bufnr = vim.api.nvim_win_get_buf(winid)
if winid ~= aer_winid and util.is_aerial_buffer(bufnr) then
vim.api.nvim_win_close(winid, false)
if vim.api.nvim_win_is_valid(winid) then
local bufnr = vim.api.nvim_win_get_buf(winid)
if winid ~= aer_winid and util.is_aerial_buffer(bufnr) then
vim.api.nvim_win_close(winid, false)
end
end
end
end
Expand Down Expand Up @@ -279,7 +285,11 @@ M.open_all = function()
return M.open()
end
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if not util.is_ignored_win(winid) and not util.is_floating_win(winid) then
if
vim.api.nvim_win_is_valid(winid)
and not util.is_ignored_win(winid)
and not util.is_floating_win(winid)
then
vim.api.nvim_win_call(winid, function()
M.open()
end)
Expand Down

0 comments on commit b3f8df4

Please sign in to comment.