Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lua/treesitter-context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ end

---@param winid integer
local update_single_context = throttle_by_id(function(winid)
-- Remove leaked contexts firstly.
local current_win = api.nvim_get_current_win()
if config.multiwindow then
require('treesitter-context.render').close_leaked_contexts()
else
require('treesitter-context.render').close_other_contexts(current_win)
end

-- 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 All @@ -77,7 +85,7 @@ local update_single_context = throttle_by_id(function(winid)

local bufnr = api.nvim_win_get_buf(winid)

if cannot_open(bufnr, winid) or not config.multiwindow and winid ~= api.nvim_get_current_win() then
if cannot_open(bufnr, winid) or not config.multiwindow and winid ~= current_win then
require('treesitter-context.render').close(winid)
return
end
Expand Down
28 changes: 28 additions & 0 deletions lua/treesitter-context/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,34 @@ end

local M = {}

-- Contexts may sometimes leak due to reasons like the use of 'noautocmd'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should be putting in provisions for handling noautocmd cases. If a bug is caused by noautocmd, then it means it was used incorrectly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly. However, I’m not sure how to reproduce this bug. satellite.nvim has similar protection, which might contribute to its reliability.
https://github.com/lewis6991/satellite.nvim/blob/main/lua/satellite/view.lua#L260

-- 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 winid integer The only window for which the context should be displayed.
function M.close_other_contexts(winid)
for parent_winid, window_context in pairs(window_contexts) do
if parent_winid ~= 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