61
61
--- @return table
62
62
function M .filter_diags_under_cursor (opts , buf , diagnostics )
63
63
if
64
- not vim .api .nvim_buf_is_valid (buf )
65
- or vim .api .nvim_get_current_buf () ~= buf
66
- or not diagnostics
67
- or # diagnostics == 0
64
+ not vim .api .nvim_buf_is_valid (buf )
65
+ or vim .api .nvim_get_current_buf () ~= buf
66
+ or not diagnostics
67
+ or # diagnostics == 0
68
68
then
69
69
return {}
70
70
end
@@ -83,25 +83,25 @@ local function filter_by_severity(opts, diagnostics)
83
83
end
84
84
85
85
--- @param opts DiagnosticConfig
86
- --- @param event table
86
+ --- @param bufnr number
87
87
--- @param diagnostics table
88
88
--- @return table
89
- local function filter_diagnostics (opts , event , diagnostics )
89
+ local function filter_diagnostics (opts , bufnr , diagnostics )
90
90
local filtered = filter_by_severity (opts , diagnostics )
91
91
92
92
table.sort (filtered , function (a , b )
93
93
return a .severity < b .severity
94
94
end )
95
95
96
96
if not opts .options .multilines .enabled then
97
- return M .filter_diags_under_cursor (opts , event . buf , filtered )
97
+ return M .filter_diags_under_cursor (opts , bufnr , filtered )
98
98
end
99
99
100
100
if opts .options .multilines .always_show then
101
101
return filtered
102
102
end
103
103
104
- local under_cursor = M .filter_diags_under_cursor (opts , event . buf , filtered )
104
+ local under_cursor = M .filter_diags_under_cursor (opts , bufnr , filtered )
105
105
return not vim .tbl_isempty (under_cursor ) and under_cursor or filtered
106
106
end
107
107
@@ -123,36 +123,36 @@ local function get_visible_diagnostics(diagnostics)
123
123
end
124
124
125
125
--- @param opts DiagnosticConfig
126
- --- @param event table
127
- local function apply_virtual_texts (opts , event )
126
+ --- @param bufnr number
127
+ local function apply_virtual_texts (opts , bufnr )
128
128
-- Validate window and state
129
129
local current_win = vim .api .nvim_get_current_win ()
130
130
if not vim .api .nvim_win_is_valid (current_win ) then
131
131
return
132
132
end
133
133
134
134
if
135
- not M .user_toggle_state
136
- or not (M .enabled and vim .diagnostic .is_enabled () and vim .api .nvim_buf_is_valid (event . buf ))
135
+ not M .user_toggle_state
136
+ or not (M .enabled and vim .diagnostic .is_enabled () and vim .api .nvim_buf_is_valid (bufnr ))
137
137
then
138
- extmarks .clear (event . buf )
138
+ extmarks .clear (bufnr )
139
139
return
140
140
end
141
141
142
142
-- Get and validate diagnostics
143
- local ok , diagnostics = pcall (vim .diagnostic .get , event . buf )
143
+ local ok , diagnostics = pcall (vim .diagnostic .get , bufnr )
144
144
if not ok or vim .tbl_isempty (diagnostics ) then
145
- extmarks .clear (event . buf )
145
+ extmarks .clear (bufnr )
146
146
return
147
147
end
148
148
149
149
-- Process diagnostics
150
- local filtered_diags = filter_diagnostics (opts , event , diagnostics )
150
+ local filtered_diags = filter_diagnostics (opts , bufnr , diagnostics )
151
151
local cursor_line = vim .api .nvim_win_get_cursor (0 )[1 ] - 1
152
152
local visible_diags = get_visible_diagnostics (filtered_diags )
153
153
154
154
-- Clear existing extmarks
155
- extmarks .clear (event . buf )
155
+ extmarks .clear (bufnr )
156
156
157
157
local diags_dims = {}
158
158
local to_render = {}
@@ -166,11 +166,13 @@ local function apply_virtual_texts(opts, event)
166
166
167
167
if lnum == cursor_line then
168
168
virt_lines , offset , need_to_be_under =
169
- virtual_text_forge .from_diagnostics (opts , diags , diagnostic_pos , event . buf )
169
+ virtual_text_forge .from_diagnostics (opts , diags , diagnostic_pos , bufnr )
170
170
else
171
- local chunks = chunk_utils .get_chunks (opts , diags , 1 , diagnostic_pos [1 ], cursor_line , event .buf )
171
+ local chunks = chunk_utils .get_chunks (opts , diags , 1 , diagnostic_pos [1 ], cursor_line ,
172
+ bufnr )
172
173
local max_width = chunk_utils .get_max_width_from_chunks (chunks .chunks )
173
- virt_lines , offset , need_to_be_under = virtual_text_forge .from_diagnostic (opts , chunks , 1 , max_width , 1 )
174
+ virt_lines , offset , need_to_be_under = virtual_text_forge .from_diagnostic (opts , chunks , 1 ,
175
+ max_width , 1 )
174
176
end
175
177
176
178
table.insert (diags_dims , { lnum , # virt_lines })
@@ -198,7 +200,7 @@ local function apply_virtual_texts(opts, event)
198
200
199
201
extmarks .create_extmarks (
200
202
opts ,
201
- event ,
203
+ bufnr ,
202
204
diagnostic_pos [1 ],
203
205
diags_dims ,
204
206
virt_lines ,
@@ -218,17 +220,17 @@ end
218
220
219
221
--- @param autocmd_ns number
220
222
--- @param opts DiagnosticConfig
221
- --- @param event table
222
- --- @param throttle_apply function
223
- local function setup_cursor_autocmds (autocmd_ns , opts , event , throttle_apply )
223
+ --- @param bufnr number
224
+ --- @param throttle_apply fun ( bufnr : number ): nil
225
+ local function setup_cursor_autocmds (autocmd_ns , opts , bufnr , throttle_apply )
224
226
local events = opts .options .enable_on_insert and { " CursorMoved" , " CursorMovedI" } or { " CursorMoved" }
225
227
226
228
vim .api .nvim_create_autocmd (events , {
227
229
group = autocmd_ns ,
228
- buffer = event . buf ,
229
- callback = function ()
230
+ buffer = bufnr ,
231
+ callback = function (event )
230
232
if vim .api .nvim_buf_is_valid (event .buf ) then
231
- throttle_apply ()
233
+ throttle_apply (event . buf )
232
234
else
233
235
detach_buffer (event .buf )
234
236
end
@@ -238,11 +240,12 @@ local function setup_cursor_autocmds(autocmd_ns, opts, event, throttle_apply)
238
240
end
239
241
240
242
--- @param autocmd_ns number
241
- --- @param event table
242
- local function setup_mode_change_autocmds (autocmd_ns , event )
243
+ --- @param bufnr number
244
+ local function setup_mode_change_autocmds (autocmd_ns , bufnr )
243
245
vim .api .nvim_create_autocmd (" ModeChanged" , {
244
246
group = autocmd_ns ,
245
- callback = function ()
247
+ buffer = bufnr ,
248
+ callback = function (event )
246
249
local mode = vim .api .nvim_get_mode ().mode
247
250
248
251
if not vim .api .nvim_buf_is_valid (event .buf ) then
@@ -262,19 +265,20 @@ end
262
265
263
266
--- @param autocmd_ns number
264
267
--- @param opts DiagnosticConfig
265
- --- @param event table
266
- --- @param throttled_apply function
267
- local function setup_buffer_autocmds (autocmd_ns , opts , event , throttled_apply )
268
- if not vim .api .nvim_buf_is_valid (event . buf ) or attached_buffers [event . buf ] then
268
+ --- @param bufnr number
269
+ --- @param throttled_apply fun ( bufnr : number ): nil
270
+ local function setup_buffer_autocmds (autocmd_ns , opts , bufnr , throttled_apply )
271
+ if not vim .api .nvim_buf_is_valid (bufnr ) or attached_buffers [bufnr ] then
269
272
return
270
273
end
271
274
272
- attached_buffers [event . buf ] = true
275
+ attached_buffers [bufnr ] = true
273
276
274
277
-- Setup diagnostic change events
275
278
vim .api .nvim_create_autocmd (" DiagnosticChanged" , {
276
279
group = autocmd_ns ,
277
- callback = function ()
280
+ buffer = bufnr ,
281
+ callback = function (event )
278
282
if vim .api .nvim_buf_is_valid (event .buf ) then
279
283
vim .api .nvim_exec_autocmds (" User" , { pattern = USER_EVENT })
280
284
end
@@ -286,19 +290,19 @@ local function setup_buffer_autocmds(autocmd_ns, opts, event, throttled_apply)
286
290
group = autocmd_ns ,
287
291
pattern = USER_EVENT ,
288
292
callback = function ()
289
- if not vim .api .nvim_buf_is_valid (event . buf ) then
290
- detach_buffer (event . buf )
293
+ if not vim .api .nvim_buf_is_valid (bufnr ) then
294
+ detach_buffer (bufnr )
291
295
return
292
296
end
293
- apply_virtual_texts (opts , event )
297
+ apply_virtual_texts (opts , bufnr )
294
298
end ,
295
299
})
296
300
297
301
-- Setup buffer cleanup events
298
302
vim .api .nvim_create_autocmd ({ " LspDetach" , " BufDelete" , " BufUnload" , " BufWipeout" }, {
299
303
group = autocmd_ns ,
300
- buffer = event . buf ,
301
- callback = function ()
304
+ buffer = bufnr ,
305
+ callback = function (event )
302
306
detach_buffer (event .buf )
303
307
end ,
304
308
})
@@ -308,19 +312,19 @@ local function setup_buffer_autocmds(autocmd_ns, opts, event, throttled_apply)
308
312
group = autocmd_ns ,
309
313
pattern = USER_EVENT_THROTTLED ,
310
314
callback = function ()
311
- if not vim .api .nvim_buf_is_valid (event . buf ) then
312
- detach_buffer (event . buf )
315
+ if not vim .api .nvim_buf_is_valid (bufnr ) then
316
+ detach_buffer (bufnr )
313
317
return
314
318
end
315
- throttled_apply ()
319
+ throttled_apply (bufnr )
316
320
end ,
317
321
})
318
322
319
323
-- Setup window resize handling
320
324
vim .api .nvim_create_autocmd (" VimResized" , {
321
325
group = autocmd_ns ,
322
- buffer = event . buf ,
323
- callback = function ()
326
+ buffer = bufnr ,
327
+ callback = function (event )
324
328
if vim .api .nvim_buf_is_valid (event .buf ) then
325
329
vim .api .nvim_exec_autocmds (" User" , { pattern = USER_EVENT })
326
330
else
@@ -341,37 +345,38 @@ function M.set_diagnostic_autocmds(opts)
341
345
342
346
local events = opts .options .overwrite_events or { " LspAttach" }
343
347
348
+ if not opts .options .enable_on_select then
349
+ table.insert (DISABLED_MODES , " s" )
350
+ end
351
+
352
+ if not opts .options .enable_on_insert then
353
+ table.insert (DISABLED_MODES , " i" )
354
+ table.insert (DISABLED_MODES , " v" )
355
+ table.insert (DISABLED_MODES , " V" )
356
+ end
357
+
344
358
vim .api .nvim_create_autocmd (events , {
345
359
callback = function (event )
346
360
if not vim .api .nvim_buf_is_valid (event .buf ) then
347
361
return
348
362
end
349
363
350
- if not opts .options .enable_on_select then
351
- table.insert (DISABLED_MODES , " s" )
352
- end
353
-
354
- if not opts .options .enable_on_insert then
355
- table.insert (DISABLED_MODES , " i" )
356
- table.insert (DISABLED_MODES , " v" )
357
- table.insert (DISABLED_MODES , " V" )
358
- end
359
-
360
364
if vim .tbl_contains (opts .disabled_ft , vim .bo [event .buf ].filetype ) then
361
365
return
362
366
end
363
367
364
- local throttled_fn , timer = utils .throttle (function ()
365
- if vim .api .nvim_buf_is_valid (event .buf ) then
366
- apply_virtual_texts (opts , event )
368
+ --- @type fun ( bufnr : number ): nil
369
+ local throttled_fn , timer = utils .throttle (function (bufnr )
370
+ if vim .api .nvim_buf_is_valid (bufnr ) then
371
+ apply_virtual_texts (opts , bufnr )
367
372
end
368
373
end , opts .options .throttle )
369
374
370
375
timers .add (event .buf , timer )
371
376
372
- setup_buffer_autocmds (autocmd_ns , opts , event , throttled_fn )
373
- setup_cursor_autocmds (autocmd_ns , opts , event , throttled_fn )
374
- setup_mode_change_autocmds (autocmd_ns , event )
377
+ setup_buffer_autocmds (autocmd_ns , opts , event . buf , throttled_fn )
378
+ setup_cursor_autocmds (autocmd_ns , opts , event . buf , throttled_fn )
379
+ setup_mode_change_autocmds (autocmd_ns , event . buf )
375
380
end ,
376
381
desc = " Setup diagnostic display system" ,
377
382
})
0 commit comments