Skip to content

Commit

Permalink
fix: some edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone committed Jan 14, 2025
1 parent d5a29e0 commit 7dab57a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ M._defaults = {
-- Options override for custom providers
provider_opts = {},
},
suggestion = {
debounce = 400,
throttle = 400,
},
}

---@type avante.Config
Expand Down
18 changes: 15 additions & 3 deletions lua/avante/suggestion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ local SUGGESTION_NS = api.nvim_create_namespace("avante_suggestion")
---@field negate_patterns table
---@field _timer? table
---@field _contexts table
---@field is_on_throttle boolean
local Suggestion = {}
Suggestion.__index = Suggestion

Expand All @@ -40,6 +41,7 @@ function Suggestion:new(id)
instance._contexts = {}
instance.ignore_patterns = gitignore_patterns
instance.negate_patterns = gitignore_negate_patterns
instance.is_on_throttle = false
if Config.behaviour.auto_suggestions then
if not vim.g.avante_login or vim.g.avante_login == false then
api.nvim_exec_autocmds("User", { pattern = Providers.env.REQUEST_LOGIN_PATTERN })
Expand Down Expand Up @@ -376,10 +378,15 @@ function Suggestion:accept()
if not suggestion then
suggestion = self:get_next_suggestion()
if suggestion then
Utils.debug("next suggestion", suggestion)
local lines = api.nvim_buf_get_lines(0, 0, -1, false)
local line = lines[suggestion.start_row - 1]
local first_line_row = suggestion.start_row
if first_line_row > 1 then first_line_row = first_line_row - 1 end
local line = lines[first_line_row]
local col = 0
if line ~= nil then col = #line end
self:set_internal_move(true)
api.nvim_win_set_cursor(0, { suggestion.start_row - 1, #line })
api.nvim_win_set_cursor(0, { first_line_row, col })
self:set_internal_move(false)
return
end
Expand Down Expand Up @@ -454,13 +461,18 @@ function Suggestion:setup_autocmds()
local last_cursor_pos = {}

local check_for_suggestion = Utils.debounce(function()
if self.is_on_throttle then return end
local current_cursor_pos = api.nvim_win_get_cursor(0)
if last_cursor_pos[1] == current_cursor_pos[1] and last_cursor_pos[2] == current_cursor_pos[2] then
self.is_on_throttle = true
vim.defer_fn(function() self.is_on_throttle = false end, Config.suggestion.throttle)
self:suggest()
end
end, 700)
end, Config.suggestion.debounce)

local function suggest_callback()
if self.is_on_throttle then return end

if self:is_internal_move() then return end

if not vim.bo.buflisted then return end
Expand Down

0 comments on commit 7dab57a

Please sign in to comment.