@@ -37,20 +37,6 @@ local function create_or_get_buf()
37
37
return buf
38
38
end
39
39
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
-
54
40
--- @param winid integer
55
41
--- @param context_winid integer ?
56
42
--- @param width integer
@@ -138,11 +124,13 @@ local function get_hl(buf_query, capture)
138
124
end
139
125
140
126
--- 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
143
131
--- @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 )
146
134
end
147
135
148
136
--- @param bufnr integer
@@ -182,10 +170,10 @@ local function highlight_contexts(bufnr, ctx_bufnr, contexts)
182
170
local nsrow , nscol , nerow , necol = range [1 ], range [2 ], range [4 ], range [5 ]
183
171
184
172
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
186
174
-- Node range begins after the context range, skip it
187
175
break
188
- elseif is_after ({ nerow , necol }, { end_row , end_col } ) then
176
+ elseif is_after (nerow , necol , end_row , end_col ) then
189
177
-- Node range extends beyond the context range, clip it
190
178
nerow , necol = end_row , end_col
191
179
end
@@ -355,18 +343,28 @@ end
355
343
--- @param context_winid ? integer
356
344
local function close (context_winid )
357
345
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
359
347
return
360
348
end
361
349
362
350
local bufnr = api .nvim_win_get_buf (context_winid )
363
351
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
368
367
end
369
- delete_excess_buffers ()
370
368
end )
371
369
end
372
370
@@ -400,18 +398,15 @@ local function copy_extmarks(bufnr, ctx_bufnr, contexts)
400
398
)
401
399
402
400
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 ]
406
402
local opts = m [4 ] --[[ @as vim.api.keyset.extmark_details]]
407
-
408
403
local start_row = offset + (row - ctx_srow )
409
404
410
405
local end_row --- @type integer ?
411
406
local end_col = opts .end_col
412
407
local mend_row = opts .end_row
413
408
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
415
410
mend_row = ctx_erow
416
411
end_col = ctx_ecol
417
412
end
454
449
455
450
local M = {}
456
451
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
-
485
452
--- @param bufnr integer
486
453
--- @param winid integer
487
454
--- @param ctx_ranges Range4[]
@@ -544,24 +511,23 @@ function M.open(bufnr, winid, ctx_ranges, ctx_lines)
544
511
horizontal_scroll_contexts (winid , window_context .context_winid )
545
512
end
546
513
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
556
520
end
521
+ end
557
522
523
+ --- @param winid integer
524
+ function M .close (winid )
558
525
local window_context = window_contexts [winid ]
559
526
if window_context then
560
527
close (window_context .context_winid )
561
528
close (window_context .gutter_winid )
529
+ window_contexts [winid ] = nil
562
530
end
563
-
564
- window_contexts [winid ] = nil
565
531
end
566
532
567
533
return M
0 commit comments