Skip to content

Commit 8ea688b

Browse files
authored
ref: don't pass event around but bufnr, use autocmd event (#116)
1 parent b658e37 commit 8ea688b

File tree

2 files changed

+73
-68
lines changed

2 files changed

+73
-68
lines changed

lua/tiny-inline-diagnostic/diagnostic.lua

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ end
7070
---@return table
7171
function M.filter_diags_under_cursor(opts, buf, diagnostics)
7272
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
7777
then
7878
return {}
7979
end
@@ -92,19 +92,19 @@ local function filter_by_severity(opts, diagnostics)
9292
end
9393

9494
---@param opts DiagnosticConfig
95-
---@param event table
95+
---@param bufnr number
9696
---@param diagnostics table
9797
---@return table
98-
local function filter_diagnostics(opts, event, diagnostics)
98+
local function filter_diagnostics(opts, bufnr, diagnostics)
9999
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)
101101
end
102102

103103
if opts.options.multilines.always_show then
104104
return diagnostics
105105
end
106106

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)
108108
return not vim.tbl_isempty(under_cursor) and under_cursor or diagnostics
109109
end
110110

@@ -173,36 +173,36 @@ local function update_diagnostics_cache(opts, bufnr, diagnostics)
173173
end
174174

175175
---@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)
178178
-- Validate window and state
179179
local current_win = vim.api.nvim_get_current_win()
180180
if not vim.api.nvim_win_is_valid(current_win) then
181181
return
182182
end
183183

184184
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))
187187
then
188-
extmarks.clear(event.buf)
188+
extmarks.clear(bufnr)
189189
return
190190
end
191191

192192
-- Get diagnostics and clear them if needed
193-
local diagnostics = diagnostics_cache[event.buf] or {}
193+
local diagnostics = diagnostics_cache[bufnr] or {}
194194
if vim.tbl_isempty(diagnostics) then
195-
extmarks.clear(event.buf)
195+
extmarks.clear(bufnr)
196196
return
197197
end
198198

199199
-- Process diagnostics
200-
local filtered_diags = filter_diagnostics(opts, event, diagnostics)
200+
local filtered_diags = filter_diagnostics(opts, bufnr, diagnostics)
201201
local cursor_line = vim.api.nvim_win_get_cursor(0)[1] - 1
202202
local visible_diags = get_visible_diagnostics(filtered_diags)
203203

204204
-- Clear existing extmarks
205-
extmarks.clear(event.buf)
205+
extmarks.clear(bufnr)
206206

207207
local diags_dims = {}
208208
local to_render = {}
@@ -216,11 +216,13 @@ local function apply_virtual_texts(opts, event)
216216

217217
if lnum == cursor_line then
218218
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)
220220
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)
222223
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)
224226
end
225227

226228
table.insert(diags_dims, { lnum, #virt_lines })
@@ -248,7 +250,7 @@ local function apply_virtual_texts(opts, event)
248250

249251
extmarks.create_extmarks(
250252
opts,
251-
event,
253+
bufnr,
252254
diagnostic_pos[1],
253255
diags_dims,
254256
virt_lines,
@@ -269,17 +271,17 @@ end
269271

270272
---@param autocmd_ns number
271273
---@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)
275277
local events = opts.options.enable_on_insert and { "CursorMoved", "CursorMovedI" } or { "CursorMoved" }
276278

277279
vim.api.nvim_create_autocmd(events, {
278280
group = autocmd_ns,
279-
buffer = event.buf,
280-
callback = function()
281+
buffer = bufnr,
282+
callback = function(event)
281283
if vim.api.nvim_buf_is_valid(event.buf) then
282-
throttle_apply()
284+
throttle_apply(event.buf)
283285
else
284286
detach_buffer(event.buf)
285287
end
@@ -289,11 +291,12 @@ local function setup_cursor_autocmds(autocmd_ns, opts, event, throttle_apply)
289291
end
290292

291293
---@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)
294296
vim.api.nvim_create_autocmd("ModeChanged", {
295297
group = autocmd_ns,
296-
callback = function()
298+
buffer = bufnr,
299+
callback = function(event)
297300
local mode = vim.api.nvim_get_mode().mode
298301

299302
if not vim.api.nvim_buf_is_valid(event.buf) then
@@ -313,18 +316,19 @@ end
313316

314317
---@param autocmd_ns number
315318
---@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
320323
return
321324
end
322325

323-
attached_buffers[event.buf] = true
326+
attached_buffers[bufnr] = true
324327

325328
-- Setup diagnostic change events
326329
vim.api.nvim_create_autocmd("DiagnosticChanged", {
327330
group = autocmd_ns,
331+
buffer = bufnr,
328332
callback = function(args)
329333
if vim.api.nvim_buf_is_valid(args.buf) then
330334
update_diagnostics_cache(opts, args.buf, args.data.diagnostics)
@@ -338,19 +342,19 @@ local function setup_buffer_autocmds(autocmd_ns, opts, event, throttled_apply)
338342
group = autocmd_ns,
339343
pattern = USER_EVENT,
340344
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)
343347
return
344348
end
345-
apply_virtual_texts(opts, event)
349+
apply_virtual_texts(opts, bufnr)
346350
end,
347351
})
348352

349353
-- Setup buffer cleanup events
350354
vim.api.nvim_create_autocmd({ "LspDetach", "BufDelete", "BufUnload", "BufWipeout" }, {
351355
group = autocmd_ns,
352-
buffer = event.buf,
353-
callback = function()
356+
buffer = bufnr,
357+
callback = function(event)
354358
detach_buffer(event.buf)
355359
end,
356360
})
@@ -360,19 +364,19 @@ local function setup_buffer_autocmds(autocmd_ns, opts, event, throttled_apply)
360364
group = autocmd_ns,
361365
pattern = USER_EVENT_THROTTLED,
362366
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)
365369
return
366370
end
367-
throttled_apply()
371+
throttled_apply(bufnr)
368372
end,
369373
})
370374

371375
-- Setup window resize handling
372376
vim.api.nvim_create_autocmd("VimResized", {
373377
group = autocmd_ns,
374-
buffer = event.buf,
375-
callback = function()
378+
buffer = bufnr,
379+
callback = function(event)
376380
if vim.api.nvim_buf_is_valid(event.buf) then
377381
vim.api.nvim_exec_autocmds("User", { pattern = USER_EVENT })
378382
else
@@ -393,37 +397,38 @@ function M.set_diagnostic_autocmds(opts)
393397

394398
local events = opts.options.overwrite_events or { "LspAttach" }
395399

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+
396410
vim.api.nvim_create_autocmd(events, {
397411
callback = function(event)
398412
if not vim.api.nvim_buf_is_valid(event.buf) then
399413
return
400414
end
401415

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-
412416
if vim.tbl_contains(opts.disabled_ft, vim.bo[event.buf].filetype) then
413417
return
414418
end
415419

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)
419424
end
420425
end, opts.options.throttle)
421426

422427
timers.add(event.buf, timer)
423428

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)
427432
end,
428433
desc = "Setup diagnostic display system",
429434
})

lua/tiny-inline-diagnostic/extmarks.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ end
237237

238238
---Create extmarks
239239
---@param opts table
240-
---@param event table
240+
---@param bufnr number
241241
---@param diag_line number
242242
---@param virt_lines table
243243
---@param offset number
@@ -246,7 +246,7 @@ end
246246
---@param virt_priority number
247247
function M.create_extmarks(
248248
opts,
249-
event,
249+
bufnr,
250250
diag_line,
251251
diags_dims,
252252
virt_lines,
@@ -255,11 +255,11 @@ function M.create_extmarks(
255255
need_to_be_under,
256256
virt_priority
257257
)
258-
if not is_valid_buffer(event.buf) or not virt_lines or vim.tbl_isempty(virt_lines) then
258+
if not is_valid_buffer(bufnr) or not virt_lines or vim.tbl_isempty(virt_lines) then
259259
return
260260
end
261261

262-
local buf_lines_count = vim.api.nvim_buf_line_count(event.buf)
262+
local buf_lines_count = vim.api.nvim_buf_line_count(bufnr)
263263
if buf_lines_count == 0 then
264264
return
265265
end
@@ -273,13 +273,13 @@ function M.create_extmarks(
273273
return
274274
end
275275

276-
create_multiline_extmark(event.buf, diag_line, virt_lines, virt_priority)
276+
create_multiline_extmark(bufnr, diag_line, virt_lines, virt_priority)
277277

278278
return
279279
end
280280

281281
if need_to_be_under or diag_line - 1 + #virt_lines > buf_lines_count - 1 then
282-
handle_overflow_case(event.buf, {
282+
handle_overflow_case(bufnr, {
283283
curline = diag_line,
284284
virt_lines = virt_lines,
285285
win_col = win_col,
@@ -293,7 +293,7 @@ function M.create_extmarks(
293293
for i = 1, #virt_lines do
294294
local col_offset = i == 1 and win_col or (win_col + offset + signs_offset)
295295
set_extmark(
296-
event.buf,
296+
bufnr,
297297
diag_line + i - 1,
298298
virt_lines[i],
299299
col_offset,

0 commit comments

Comments
 (0)