@@ -37,20 +37,6 @@ local function create_or_get_buf()
3737 return buf
3838end
3939
40- local function delete_excess_buffers ()
41- if fn .getcmdwintype () ~= ' ' then
42- -- Can't delete buffers when the command-line window is open.
43- return
44- end
45-
46- while # buffer_pool > MAX_BUFFER_POOL_SIZE do
47- local buf = table.remove (buffer_pool , # buffer_pool )
48- if api .nvim_buf_is_valid (buf ) then
49- api .nvim_buf_delete (buf , { force = true })
50- end
51- end
52- end
53-
5440--- @param winid integer
5541--- @param context_winid integer ?
5642--- @param width integer
@@ -138,11 +124,13 @@ local function get_hl(buf_query, capture)
138124end
139125
140126--- Is a position a after another position b?
141- --- @param a [integer , integer] [row , col]
142- --- @param b [integer , integer] [row , col]
127+ --- @param arow integer
128+ --- @param acol integer
129+ --- @param brow integer
130+ --- @param bcol integer
143131--- @return boolean
144- local function is_after (a , b )
145- return a [ 1 ] > b [ 1 ] or (a [ 1 ] == b [ 1 ] and a [ 2 ] > b [ 2 ] )
132+ local function is_after (arow , acol , brow , bcol )
133+ return arow > brow or (arow == brow and acol > bcol )
146134end
147135
148136--- @param bufnr integer
@@ -182,10 +170,10 @@ local function highlight_contexts(bufnr, ctx_bufnr, contexts)
182170 local nsrow , nscol , nerow , necol = range [1 ], range [2 ], range [4 ], range [5 ]
183171
184172 if nsrow >= start_row then
185- if is_after ({ nsrow , nscol }, { end_row , end_col } ) then
173+ if is_after (nsrow , nscol , end_row , end_col ) then
186174 -- Node range begins after the context range, skip it
187175 break
188- elseif is_after ({ nerow , necol }, { end_row , end_col } ) then
176+ elseif is_after (nerow , necol , end_row , end_col ) then
189177 -- Node range extends beyond the context range, clip it
190178 nerow , necol = end_row , end_col
191179 end
@@ -355,18 +343,28 @@ end
355343--- @param context_winid ? integer
356344local function close (context_winid )
357345 vim .schedule (function ()
358- if context_winid == nil or not api .nvim_win_is_valid (context_winid ) then
346+ if not context_winid or not api .nvim_win_is_valid (context_winid ) then
359347 return
360348 end
361349
362350 local bufnr = api .nvim_win_get_buf (context_winid )
363351 api .nvim_win_close (context_winid , true )
364- if bufnr ~= nil and api .nvim_buf_is_valid (bufnr ) then
365- -- We can't delete the buffer in-place if the pool is full and the command-line window is open.
366- -- Instead, add the buffer to the pool and let delete_excess_buffers() address this situation.
367- table.insert (buffer_pool , bufnr )
352+
353+ -- Add the buffer back to the pool for reuse.
354+ if bufnr and api .nvim_buf_is_valid (bufnr ) then
355+ buffer_pool [# buffer_pool + 1 ] = bufnr
356+ end
357+
358+ -- Delete excess buffers in the pool.
359+ -- Can't delete buffers when the command-line window is open.
360+ if fn .getcmdwintype () == ' ' then
361+ while # buffer_pool > MAX_BUFFER_POOL_SIZE do
362+ local buf = table.remove (buffer_pool , # buffer_pool )
363+ if api .nvim_buf_is_valid (buf ) then
364+ api .nvim_buf_delete (buf , { force = true })
365+ end
366+ end
368367 end
369- delete_excess_buffers ()
370368 end )
371369end
372370
@@ -400,18 +398,15 @@ local function copy_extmarks(bufnr, ctx_bufnr, contexts)
400398 )
401399
402400 for _ , m in ipairs (extmarks ) do
403- local id = m [1 ]
404- local row = m [2 ]
405- local col = m [3 ] --[[ @as integer]]
401+ local id , row , col = m [1 ], m [2 ], m [3 ]
406402 local opts = m [4 ] --[[ @as vim.api.keyset.extmark_details]]
407-
408403 local start_row = offset + (row - ctx_srow )
409404
410405 local end_row --- @type integer ?
411406 local end_col = opts .end_col
412407 local mend_row = opts .end_row
413408 if mend_row then
414- if is_after ({ mend_row , end_col }, { ctx_erow , ctx_ecol } ) then
409+ if is_after (mend_row , assert ( end_col ), ctx_erow , ctx_ecol ) then
415410 mend_row = ctx_erow
416411 end_col = ctx_ecol
417412 end
454449
455450local M = {}
456451
457- -- Contexts may sometimes leak due to reasons like the use of 'noautocmd'.
458- -- In these cases, affected windows might remain visible, and even ToggleContext
459- -- won't resolve the issue, as contexts are identified using parent windows.
460- -- Therefore, it's essential to occasionally perform garbage collection to
461- -- clean up these leaked contexts.
462- function M .close_leaked_contexts ()
463- local all_wins = api .nvim_list_wins ()
464-
465- for parent_winid , window_context in pairs (window_contexts ) do
466- if not vim .tbl_contains (all_wins , parent_winid ) then
467- close (window_context .context_winid )
468- close (window_context .gutter_winid )
469- window_contexts [parent_winid ] = nil
470- end
471- end
472- end
473-
474- --- @param winid integer The only window for which the context should be displayed.
475- function M .close_other_contexts (winid )
476- for parent_winid , window_context in pairs (window_contexts ) do
477- if parent_winid ~= winid then
478- close (window_context .context_winid )
479- close (window_context .gutter_winid )
480- window_contexts [parent_winid ] = nil
481- end
482- end
483- end
484-
485452--- @param bufnr integer
486453--- @param winid integer
487454--- @param ctx_ranges Range4[]
@@ -544,24 +511,23 @@ function M.open(bufnr, winid, ctx_ranges, ctx_lines)
544511 horizontal_scroll_contexts (winid , window_context .context_winid )
545512end
546513
547- --- @param winid ? integer
548- function M .close (winid )
549- -- Can't close other windows when the command-line window is open
550- if fn .getcmdwintype () ~= ' ' then
551- return
552- end
553-
554- if winid == nil then
555- return
514+ --- @param exclude_winids integer[] The only window for which the context should be displayed.
515+ function M .close_contexts (exclude_winids )
516+ for winid in pairs (window_contexts ) do
517+ if not vim .tbl_contains (exclude_winids , winid ) then
518+ M .close (winid )
519+ end
556520 end
521+ end
557522
523+ --- @param winid integer
524+ function M .close (winid )
558525 local window_context = window_contexts [winid ]
559526 if window_context then
560527 close (window_context .context_winid )
561528 close (window_context .gutter_winid )
529+ window_contexts [winid ] = nil
562530 end
563-
564- window_contexts [winid ] = nil
565531end
566532
567533return M
0 commit comments