From 102197c5bbb3e723553b8e7fa2536cfe532dd3d0 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 26 Jun 2025 15:38:57 +0800 Subject: [PATCH] fix: debounce error (#2337) --- lua/avante/suggestion.lua | 7 +++++-- lua/avante/utils/init.lua | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lua/avante/suggestion.lua b/lua/avante/suggestion.lua index e0b6d31..1a5acef 100644 --- a/lua/avante/suggestion.lua +++ b/lua/avante/suggestion.lua @@ -24,7 +24,7 @@ local SUGGESTION_NS = api.nvim_create_namespace("avante_suggestion") ---@field augroup integer ---@field ignore_patterns table ---@field negate_patterns table ----@field _timer? integer +---@field _timer? uv.uv_timer_t | uv_timer_t ---@field _contexts table ---@field is_on_throttle boolean local Suggestion = {} @@ -348,7 +348,10 @@ end function Suggestion:stop_timer() if self._timer then - pcall(function() fn.timer_stop(self._timer) end) + pcall(function() + self._timer:stop() + self._timer:close() + end) self._timer = nil end end diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index 8655b2a..0973015 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -674,19 +674,26 @@ function M.trim_all_line_numbers(content) end function M.debounce(func, delay) - local timer_id = nil + local timer = nil return function(...) local args = { ... } - if timer_id then pcall(function() fn.timer_stop(timer_id) end) end + if timer then + timer:stop() + timer:close() + end - timer_id = fn.timer_start(delay, function() - func(unpack(args)) - timer_id = nil + timer = vim.loop.new_timer() + if not timer then return end + + timer:start(delay, 0, function() + vim.schedule(function() func(unpack(args)) end) + timer:close() + timer = nil end) - return timer_id + return timer end end