70
70
--- @return table
71
71
function M .filter_diags_under_cursor (opts , buf , diagnostics )
72
72
if
73
- not vim .api .nvim_buf_is_valid (buf )
74
- or vim .api .nvim_get_current_buf () ~= buf
75
- or not diagnostics
76
- or # diagnostics == 0
73
+ not vim .api .nvim_buf_is_valid (buf )
74
+ or vim .api .nvim_get_current_buf () ~= buf
75
+ or not diagnostics
76
+ or # diagnostics == 0
77
77
then
78
78
return {}
79
79
end
@@ -92,19 +92,19 @@ local function filter_by_severity(opts, diagnostics)
92
92
end
93
93
94
94
--- @param opts DiagnosticConfig
95
- --- @param event table
95
+ --- @param bufnr number
96
96
--- @param diagnostics table
97
97
--- @return table
98
- local function filter_diagnostics (opts , event , diagnostics )
98
+ local function filter_diagnostics (opts , bufnr , diagnostics )
99
99
if not opts .options .multilines .enabled then
100
- return M .filter_diags_under_cursor (opts , event . buf , diagnostics )
100
+ return M .filter_diags_under_cursor (opts , bufnr , diagnostics )
101
101
end
102
102
103
103
if opts .options .multilines .always_show then
104
104
return diagnostics
105
105
end
106
106
107
- local under_cursor = M .filter_diags_under_cursor (opts , event . buf , diagnostics )
107
+ local under_cursor = M .filter_diags_under_cursor (opts , bufnr , diagnostics )
108
108
return not vim .tbl_isempty (under_cursor ) and under_cursor or diagnostics
109
109
end
110
110
@@ -173,36 +173,36 @@ local function update_diagnostics_cache(opts, bufnr, diagnostics)
173
173
end
174
174
175
175
--- @param opts DiagnosticConfig
176
- --- @param event table
177
- local function apply_virtual_texts (opts , event )
176
+ --- @param bufnr number
177
+ local function apply_virtual_texts (opts , bufnr )
178
178
-- Validate window and state
179
179
local current_win = vim .api .nvim_get_current_win ()
180
180
if not vim .api .nvim_win_is_valid (current_win ) then
181
181
return
182
182
end
183
183
184
184
if
185
- not M .user_toggle_state
186
- or not (M .enabled and vim .diagnostic .is_enabled () and vim .api .nvim_buf_is_valid (event . buf ))
185
+ not M .user_toggle_state
186
+ or not (M .enabled and vim .diagnostic .is_enabled () and vim .api .nvim_buf_is_valid (bufnr ))
187
187
then
188
- extmarks .clear (event . buf )
188
+ extmarks .clear (bufnr )
189
189
return
190
190
end
191
191
192
192
-- Get diagnostics and clear them if needed
193
- local diagnostics = diagnostics_cache [event . buf ] or {}
193
+ local diagnostics = diagnostics_cache [bufnr ] or {}
194
194
if vim .tbl_isempty (diagnostics ) then
195
- extmarks .clear (event . buf )
195
+ extmarks .clear (bufnr )
196
196
return
197
197
end
198
198
199
199
-- Process diagnostics
200
- local filtered_diags = filter_diagnostics (opts , event , diagnostics )
200
+ local filtered_diags = filter_diagnostics (opts , bufnr , diagnostics )
201
201
local cursor_line = vim .api .nvim_win_get_cursor (0 )[1 ] - 1
202
202
local visible_diags = get_visible_diagnostics (filtered_diags )
203
203
204
204
-- Clear existing extmarks
205
- extmarks .clear (event . buf )
205
+ extmarks .clear (bufnr )
206
206
207
207
local diags_dims = {}
208
208
local to_render = {}
@@ -216,11 +216,13 @@ local function apply_virtual_texts(opts, event)
216
216
217
217
if lnum == cursor_line then
218
218
virt_lines , offset , need_to_be_under =
219
- virtual_text_forge .from_diagnostics (opts , diags , diagnostic_pos , event . buf )
219
+ virtual_text_forge .from_diagnostics (opts , diags , diagnostic_pos , bufnr )
220
220
else
221
- local chunks = chunk_utils .get_chunks (opts , diags , 1 , diagnostic_pos [1 ], cursor_line , event .buf )
221
+ local chunks = chunk_utils .get_chunks (opts , diags , 1 , diagnostic_pos [1 ], cursor_line ,
222
+ bufnr )
222
223
local max_width = chunk_utils .get_max_width_from_chunks (chunks .chunks )
223
- virt_lines , offset , need_to_be_under = virtual_text_forge .from_diagnostic (opts , chunks , 1 , max_width , 1 )
224
+ virt_lines , offset , need_to_be_under = virtual_text_forge .from_diagnostic (opts , chunks , 1 ,
225
+ max_width , 1 )
224
226
end
225
227
226
228
table.insert (diags_dims , { lnum , # virt_lines })
@@ -248,7 +250,7 @@ local function apply_virtual_texts(opts, event)
248
250
249
251
extmarks .create_extmarks (
250
252
opts ,
251
- event ,
253
+ bufnr ,
252
254
diagnostic_pos [1 ],
253
255
diags_dims ,
254
256
virt_lines ,
@@ -269,17 +271,17 @@ end
269
271
270
272
--- @param autocmd_ns number
271
273
--- @param opts DiagnosticConfig
272
- --- @param event table
273
- --- @param throttle_apply function
274
- local function setup_cursor_autocmds (autocmd_ns , opts , event , throttle_apply )
274
+ --- @param bufnr number
275
+ --- @param throttle_apply fun ( bufnr : number ): nil
276
+ local function setup_cursor_autocmds (autocmd_ns , opts , bufnr , throttle_apply )
275
277
local events = opts .options .enable_on_insert and { " CursorMoved" , " CursorMovedI" } or { " CursorMoved" }
276
278
277
279
vim .api .nvim_create_autocmd (events , {
278
280
group = autocmd_ns ,
279
- buffer = event . buf ,
280
- callback = function ()
281
+ buffer = bufnr ,
282
+ callback = function (event )
281
283
if vim .api .nvim_buf_is_valid (event .buf ) then
282
- throttle_apply ()
284
+ throttle_apply (event . buf )
283
285
else
284
286
detach_buffer (event .buf )
285
287
end
@@ -289,11 +291,12 @@ local function setup_cursor_autocmds(autocmd_ns, opts, event, throttle_apply)
289
291
end
290
292
291
293
--- @param autocmd_ns number
292
- --- @param event table
293
- local function setup_mode_change_autocmds (autocmd_ns , event )
294
+ --- @param bufnr number
295
+ local function setup_mode_change_autocmds (autocmd_ns , bufnr )
294
296
vim .api .nvim_create_autocmd (" ModeChanged" , {
295
297
group = autocmd_ns ,
296
- callback = function ()
298
+ buffer = bufnr ,
299
+ callback = function (event )
297
300
local mode = vim .api .nvim_get_mode ().mode
298
301
299
302
if not vim .api .nvim_buf_is_valid (event .buf ) then
@@ -313,18 +316,19 @@ end
313
316
314
317
--- @param autocmd_ns number
315
318
--- @param opts DiagnosticConfig
316
- --- @param event table
317
- --- @param throttled_apply function
318
- local function setup_buffer_autocmds (autocmd_ns , opts , event , throttled_apply )
319
- if not vim .api .nvim_buf_is_valid (event . buf ) or attached_buffers [event . buf ] then
319
+ --- @param bufnr number
320
+ --- @param throttled_apply fun ( bufnr : number ): nil
321
+ local function setup_buffer_autocmds (autocmd_ns , opts , bufnr , throttled_apply )
322
+ if not vim .api .nvim_buf_is_valid (bufnr ) or attached_buffers [bufnr ] then
320
323
return
321
324
end
322
325
323
- attached_buffers [event . buf ] = true
326
+ attached_buffers [bufnr ] = true
324
327
325
328
-- Setup diagnostic change events
326
329
vim .api .nvim_create_autocmd (" DiagnosticChanged" , {
327
330
group = autocmd_ns ,
331
+ buffer = bufnr ,
328
332
callback = function (args )
329
333
if vim .api .nvim_buf_is_valid (args .buf ) then
330
334
update_diagnostics_cache (opts , args .buf , args .data .diagnostics )
@@ -338,19 +342,19 @@ local function setup_buffer_autocmds(autocmd_ns, opts, event, throttled_apply)
338
342
group = autocmd_ns ,
339
343
pattern = USER_EVENT ,
340
344
callback = function ()
341
- if not vim .api .nvim_buf_is_valid (event . buf ) then
342
- detach_buffer (event . buf )
345
+ if not vim .api .nvim_buf_is_valid (bufnr ) then
346
+ detach_buffer (bufnr )
343
347
return
344
348
end
345
- apply_virtual_texts (opts , event )
349
+ apply_virtual_texts (opts , bufnr )
346
350
end ,
347
351
})
348
352
349
353
-- Setup buffer cleanup events
350
354
vim .api .nvim_create_autocmd ({ " LspDetach" , " BufDelete" , " BufUnload" , " BufWipeout" }, {
351
355
group = autocmd_ns ,
352
- buffer = event . buf ,
353
- callback = function ()
356
+ buffer = bufnr ,
357
+ callback = function (event )
354
358
detach_buffer (event .buf )
355
359
end ,
356
360
})
@@ -360,19 +364,19 @@ local function setup_buffer_autocmds(autocmd_ns, opts, event, throttled_apply)
360
364
group = autocmd_ns ,
361
365
pattern = USER_EVENT_THROTTLED ,
362
366
callback = function ()
363
- if not vim .api .nvim_buf_is_valid (event . buf ) then
364
- detach_buffer (event . buf )
367
+ if not vim .api .nvim_buf_is_valid (bufnr ) then
368
+ detach_buffer (bufnr )
365
369
return
366
370
end
367
- throttled_apply ()
371
+ throttled_apply (bufnr )
368
372
end ,
369
373
})
370
374
371
375
-- Setup window resize handling
372
376
vim .api .nvim_create_autocmd (" VimResized" , {
373
377
group = autocmd_ns ,
374
- buffer = event . buf ,
375
- callback = function ()
378
+ buffer = bufnr ,
379
+ callback = function (event )
376
380
if vim .api .nvim_buf_is_valid (event .buf ) then
377
381
vim .api .nvim_exec_autocmds (" User" , { pattern = USER_EVENT })
378
382
else
@@ -393,37 +397,38 @@ function M.set_diagnostic_autocmds(opts)
393
397
394
398
local events = opts .options .overwrite_events or { " LspAttach" }
395
399
400
+ if not opts .options .enable_on_select then
401
+ table.insert (DISABLED_MODES , " s" )
402
+ end
403
+
404
+ if not opts .options .enable_on_insert then
405
+ table.insert (DISABLED_MODES , " i" )
406
+ table.insert (DISABLED_MODES , " v" )
407
+ table.insert (DISABLED_MODES , " V" )
408
+ end
409
+
396
410
vim .api .nvim_create_autocmd (events , {
397
411
callback = function (event )
398
412
if not vim .api .nvim_buf_is_valid (event .buf ) then
399
413
return
400
414
end
401
415
402
- if not opts .options .enable_on_select then
403
- table.insert (DISABLED_MODES , " s" )
404
- end
405
-
406
- if not opts .options .enable_on_insert then
407
- table.insert (DISABLED_MODES , " i" )
408
- table.insert (DISABLED_MODES , " v" )
409
- table.insert (DISABLED_MODES , " V" )
410
- end
411
-
412
416
if vim .tbl_contains (opts .disabled_ft , vim .bo [event .buf ].filetype ) then
413
417
return
414
418
end
415
419
416
- local throttled_fn , timer = utils .throttle (function ()
417
- if vim .api .nvim_buf_is_valid (event .buf ) then
418
- apply_virtual_texts (opts , event )
420
+ --- @type fun ( bufnr : number ): nil
421
+ local throttled_fn , timer = utils .throttle (function (bufnr )
422
+ if vim .api .nvim_buf_is_valid (bufnr ) then
423
+ apply_virtual_texts (opts , bufnr )
419
424
end
420
425
end , opts .options .throttle )
421
426
422
427
timers .add (event .buf , timer )
423
428
424
- setup_buffer_autocmds (autocmd_ns , opts , event , throttled_fn )
425
- setup_cursor_autocmds (autocmd_ns , opts , event , throttled_fn )
426
- setup_mode_change_autocmds (autocmd_ns , event )
429
+ setup_buffer_autocmds (autocmd_ns , opts , event . buf , throttled_fn )
430
+ setup_cursor_autocmds (autocmd_ns , opts , event . buf , throttled_fn )
431
+ setup_mode_change_autocmds (autocmd_ns , event . buf )
427
432
end ,
428
433
desc = " Setup diagnostic display system" ,
429
434
})
0 commit comments