Skip to content

Commit 1147c42

Browse files
apollo1321lewis6991
authored andcommitted
fix: close leaked contexts periodically
1 parent 898b505 commit 1147c42

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lua/treesitter-context.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ end
6969

7070
---@param winid integer
7171
local update_single_context = throttle_by_id(function(winid)
72+
-- Remove leaked contexts firstly.
73+
local current_win = api.nvim_get_current_win()
74+
if config.multiwindow then
75+
require('treesitter-context.render').close_leaked_contexts()
76+
else
77+
require('treesitter-context.render').close_other_contexts(current_win)
78+
end
79+
7280
-- Since the update is performed asynchronously, the window may be closed at this moment.
7381
-- Therefore, we need to check if it is still valid.
7482
if not api.nvim_win_is_valid(winid) or vim.fn.getcmdtype() ~= '' then
@@ -77,7 +85,7 @@ local update_single_context = throttle_by_id(function(winid)
7785

7886
local bufnr = api.nvim_win_get_buf(winid)
7987

80-
if cannot_open(bufnr, winid) or not config.multiwindow and winid ~= api.nvim_get_current_win() then
88+
if cannot_open(bufnr, winid) or not config.multiwindow and winid ~= current_win then
8189
require('treesitter-context.render').close(winid)
8290
return
8391
end

lua/treesitter-context/render.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,34 @@ end
386386

387387
local M = {}
388388

389+
-- Contexts may sometimes leak due to reasons like the use of 'noautocmd'.
390+
-- In these cases, affected windows might remain visible, and even ToggleContext
391+
-- won't resolve the issue, as contexts are identified using parent windows.
392+
-- Therefore, it's essential to occasionally perform garbage collection to
393+
-- clean up these leaked contexts.
394+
function M.close_leaked_contexts()
395+
local all_wins = api.nvim_list_wins()
396+
397+
for parent_winid, window_context in pairs(window_contexts) do
398+
if not vim.tbl_contains(all_wins, parent_winid) then
399+
close(window_context.context_winid)
400+
close(window_context.gutter_winid)
401+
window_contexts[parent_winid] = nil
402+
end
403+
end
404+
end
405+
406+
--- @param winid integer The only window for which the context should be displayed.
407+
function M.close_other_contexts(winid)
408+
for parent_winid, window_context in pairs(window_contexts) do
409+
if parent_winid ~= winid then
410+
close(window_context.context_winid)
411+
close(window_context.gutter_winid)
412+
window_contexts[parent_winid] = nil
413+
end
414+
end
415+
end
416+
389417
--- @param bufnr integer
390418
--- @param winid integer
391419
--- @param ctx_ranges Range4[]

0 commit comments

Comments
 (0)