File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed
Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change 6969
7070--- @param winid integer
7171local 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
Original file line number Diff line number Diff line change 386386
387387local 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[]
You can’t perform that action at this time.
0 commit comments