Skip to content

Commit 825abf0

Browse files
committed
ref: don't pass event around but bufnr, use autocmd event
1 parent 220cba7 commit 825abf0

File tree

2 files changed

+74
-69
lines changed

2 files changed

+74
-69
lines changed

lua/tiny-inline-diagnostic/diagnostic.lua

Lines changed: 67 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ end
6161
---@return table
6262
function M.filter_diags_under_cursor(opts, buf, diagnostics)
6363
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
6868
then
6969
return {}
7070
end
@@ -83,25 +83,25 @@ local function filter_by_severity(opts, diagnostics)
8383
end
8484

8585
---@param opts DiagnosticConfig
86-
---@param event table
86+
---@param bufnr number
8787
---@param diagnostics table
8888
---@return table
89-
local function filter_diagnostics(opts, event, diagnostics)
89+
local function filter_diagnostics(opts, bufnr, diagnostics)
9090
local filtered = filter_by_severity(opts, diagnostics)
9191

9292
table.sort(filtered, function(a, b)
9393
return a.severity < b.severity
9494
end)
9595

9696
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)
9898
end
9999

100100
if opts.options.multilines.always_show then
101101
return filtered
102102
end
103103

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)
105105
return not vim.tbl_isempty(under_cursor) and under_cursor or filtered
106106
end
107107

@@ -123,36 +123,36 @@ local function get_visible_diagnostics(diagnostics)
123123
end
124124

125125
---@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)
128128
-- Validate window and state
129129
local current_win = vim.api.nvim_get_current_win()
130130
if not vim.api.nvim_win_is_valid(current_win) then
131131
return
132132
end
133133

134134
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))
137137
then
138-
extmarks.clear(event.buf)
138+
extmarks.clear(bufnr)
139139
return
140140
end
141141

142142
-- Get and validate diagnostics
143-
local ok, diagnostics = pcall(vim.diagnostic.get, event.buf)
143+
local ok, diagnostics = pcall(vim.diagnostic.get, bufnr)
144144
if not ok or vim.tbl_isempty(diagnostics) then
145-
extmarks.clear(event.buf)
145+
extmarks.clear(bufnr)
146146
return
147147
end
148148

149149
-- Process diagnostics
150-
local filtered_diags = filter_diagnostics(opts, event, diagnostics)
150+
local filtered_diags = filter_diagnostics(opts, bufnr, diagnostics)
151151
local cursor_line = vim.api.nvim_win_get_cursor(0)[1] - 1
152152
local visible_diags = get_visible_diagnostics(filtered_diags)
153153

154154
-- Clear existing extmarks
155-
extmarks.clear(event.buf)
155+
extmarks.clear(bufnr)
156156

157157
local diags_dims = {}
158158
local to_render = {}
@@ -166,11 +166,13 @@ local function apply_virtual_texts(opts, event)
166166

167167
if lnum == cursor_line then
168168
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)
170170
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)
172173
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)
174176
end
175177

176178
table.insert(diags_dims, { lnum, #virt_lines })
@@ -198,7 +200,7 @@ local function apply_virtual_texts(opts, event)
198200

199201
extmarks.create_extmarks(
200202
opts,
201-
event,
203+
bufnr,
202204
diagnostic_pos[1],
203205
diags_dims,
204206
virt_lines,
@@ -218,17 +220,17 @@ end
218220

219221
---@param autocmd_ns number
220222
---@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)
224226
local events = opts.options.enable_on_insert and { "CursorMoved", "CursorMovedI" } or { "CursorMoved" }
225227

226228
vim.api.nvim_create_autocmd(events, {
227229
group = autocmd_ns,
228-
buffer = event.buf,
229-
callback = function()
230+
buffer = bufnr,
231+
callback = function(event)
230232
if vim.api.nvim_buf_is_valid(event.buf) then
231-
throttle_apply()
233+
throttle_apply(event.buf)
232234
else
233235
detach_buffer(event.buf)
234236
end
@@ -238,11 +240,12 @@ local function setup_cursor_autocmds(autocmd_ns, opts, event, throttle_apply)
238240
end
239241

240242
---@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)
243245
vim.api.nvim_create_autocmd("ModeChanged", {
244246
group = autocmd_ns,
245-
callback = function()
247+
buffer = bufnr,
248+
callback = function(event)
246249
local mode = vim.api.nvim_get_mode().mode
247250

248251
if not vim.api.nvim_buf_is_valid(event.buf) then
@@ -262,19 +265,20 @@ end
262265

263266
---@param autocmd_ns number
264267
---@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
269272
return
270273
end
271274

272-
attached_buffers[event.buf] = true
275+
attached_buffers[bufnr] = true
273276

274277
-- Setup diagnostic change events
275278
vim.api.nvim_create_autocmd("DiagnosticChanged", {
276279
group = autocmd_ns,
277-
callback = function()
280+
buffer = bufnr,
281+
callback = function(event)
278282
if vim.api.nvim_buf_is_valid(event.buf) then
279283
vim.api.nvim_exec_autocmds("User", { pattern = USER_EVENT })
280284
end
@@ -286,19 +290,19 @@ local function setup_buffer_autocmds(autocmd_ns, opts, event, throttled_apply)
286290
group = autocmd_ns,
287291
pattern = USER_EVENT,
288292
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)
291295
return
292296
end
293-
apply_virtual_texts(opts, event)
297+
apply_virtual_texts(opts, bufnr)
294298
end,
295299
})
296300

297301
-- Setup buffer cleanup events
298302
vim.api.nvim_create_autocmd({ "LspDetach", "BufDelete", "BufUnload", "BufWipeout" }, {
299303
group = autocmd_ns,
300-
buffer = event.buf,
301-
callback = function()
304+
buffer = bufnr,
305+
callback = function(event)
302306
detach_buffer(event.buf)
303307
end,
304308
})
@@ -308,19 +312,19 @@ local function setup_buffer_autocmds(autocmd_ns, opts, event, throttled_apply)
308312
group = autocmd_ns,
309313
pattern = USER_EVENT_THROTTLED,
310314
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)
313317
return
314318
end
315-
throttled_apply()
319+
throttled_apply(bufnr)
316320
end,
317321
})
318322

319323
-- Setup window resize handling
320324
vim.api.nvim_create_autocmd("VimResized", {
321325
group = autocmd_ns,
322-
buffer = event.buf,
323-
callback = function()
326+
buffer = bufnr,
327+
callback = function(event)
324328
if vim.api.nvim_buf_is_valid(event.buf) then
325329
vim.api.nvim_exec_autocmds("User", { pattern = USER_EVENT })
326330
else
@@ -341,37 +345,38 @@ function M.set_diagnostic_autocmds(opts)
341345

342346
local events = opts.options.overwrite_events or { "LspAttach" }
343347

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+
344358
vim.api.nvim_create_autocmd(events, {
345359
callback = function(event)
346360
if not vim.api.nvim_buf_is_valid(event.buf) then
347361
return
348362
end
349363

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-
360364
if vim.tbl_contains(opts.disabled_ft, vim.bo[event.buf].filetype) then
361365
return
362366
end
363367

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)
367372
end
368373
end, opts.options.throttle)
369374

370375
timers.add(event.buf, timer)
371376

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)
375380
end,
376381
desc = "Setup diagnostic display system",
377382
})

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)