Skip to content

Commit 9e751a6

Browse files
committed
feat(autocmds): update diagnostics directly on jumps
1 parent 64de636 commit 9e751a6

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

lua/tiny-inline-diagnostic/autocmds.lua

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local timers = require("tiny-inline-diagnostic.timer")
55
local AUGROUP_NAME = "TinyInlineDiagnosticAutocmds"
66

77
local attached_buffers = {}
8+
local last_cursor_positions = {}
89

910
---@param bufnr number
1011
---@return boolean
@@ -18,6 +19,7 @@ function M.detach(bufnr, cleanup_callback)
1819
local cache = require("tiny-inline-diagnostic.cache")
1920
timers.close(bufnr)
2021
attached_buffers[bufnr] = nil
22+
last_cursor_positions[bufnr] = nil
2123
cache.clear(bufnr)
2224
if cleanup_callback then
2325
cleanup_callback(bufnr)
@@ -28,21 +30,39 @@ end
2830
---@param opts table
2931
---@param bufnr number
3032
---@param throttle_apply function
31-
function M.setup_cursor_autocmds(autocmd_ns, opts, bufnr, throttle_apply)
33+
---@param direct_apply function
34+
function M.setup_cursor_autocmds(autocmd_ns, opts, bufnr, throttle_apply, direct_apply)
3235
local events = opts.options.enable_on_insert and { "CursorMoved", "CursorMovedI" }
3336
or { "CursorMoved" }
3437

3538
vim.api.nvim_create_autocmd(events, {
3639
group = autocmd_ns,
3740
buffer = bufnr,
3841
callback = function(event)
39-
if vim.api.nvim_buf_is_valid(event.buf) then
40-
throttle_apply(event.buf)
41-
else
42+
if not vim.api.nvim_buf_is_valid(event.buf) then
4243
M.detach(event.buf)
44+
return
45+
end
46+
47+
local current_pos = vim.api.nvim_win_get_cursor(0)
48+
local current_line = current_pos[1]
49+
local last_pos = last_cursor_positions[event.buf]
50+
51+
if last_pos then
52+
local line_diff = math.abs(current_line - last_pos[1])
53+
last_cursor_positions[event.buf] = current_pos
54+
55+
if line_diff > 1 then
56+
direct_apply(event.buf)
57+
else
58+
throttle_apply(event.buf)
59+
end
60+
else
61+
last_cursor_positions[event.buf] = current_pos
62+
throttle_apply(event.buf)
4363
end
4464
end,
45-
desc = "Update diagnostics on cursor move (throttled)",
65+
desc = "Update diagnostics on cursor move (throttled for small moves, direct for jumps)",
4666
})
4767
end
4868

lua/tiny-inline-diagnostic/diagnostic.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function M.set_diagnostic_autocmds(opts)
5959
direct_renderer,
6060
on_diagnostic_change
6161
)
62-
autocmds.setup_cursor_autocmds(autocmd_ns, opts, event.buf, throttler.fn)
62+
autocmds.setup_cursor_autocmds(autocmd_ns, opts, event.buf, throttler.fn, direct_renderer)
6363
autocmds.setup_mode_change_autocmds(autocmd_ns, event.buf, on_mode_change)
6464

6565
local existing_diagnostics = vim.diagnostic.get(event.buf)

0 commit comments

Comments
 (0)