Skip to content

Commit

Permalink
fix: close leaked contexts periodically
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo1321 committed Nov 17, 2024
1 parent 898b505 commit 9ac4403
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lua/treesitter-context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ end

---@param winid integer
local update_single_context = throttle_by_id(function(winid)
-- Remove leaked contexts firstly.
require('treesitter-context.render').close_leaked_contexts()

-- Since the update is performed asynchronously, the window may be closed at this moment.
-- Therefore, we need to check if it is still valid.
if not api.nvim_win_is_valid(winid) or vim.fn.getcmdtype() ~= '' then
Expand Down
17 changes: 17 additions & 0 deletions lua/treesitter-context/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,23 @@ end

local M = {}

-- Contexts may sometimes leak due to reasons like the use of 'noautocmd'.
-- In these cases, affected windows might remain visible, and even ToggleContext
-- won't resolve the issue, as contexts are identified using parent windows.
-- Therefore, it's essential to occasionally perform garbage collection to
-- clean up these leaked contexts.
function M.close_leaked_contexts()
local all_wins = api.nvim_list_wins()

for parent_winid, window_context in pairs(window_contexts) do
if not vim.tbl_contains(all_wins, parent_winid) then
close(window_context.context_winid)
close(window_context.gutter_winid)
window_contexts[parent_winid] = nil
end
end
end

--- @param bufnr integer
--- @param winid integer
--- @param ctx_ranges Range4[]
Expand Down

0 comments on commit 9ac4403

Please sign in to comment.