@@ -5,6 +5,7 @@ local timers = require("tiny-inline-diagnostic.timer")
55local AUGROUP_NAME = " TinyInlineDiagnosticAutocmds"
66
77local 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 )
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 })
4767end
4868
0 commit comments