debug completion
This commit is contained in:
parent
bc1b033ca1
commit
4cc8a3efd1
@ -6,7 +6,10 @@ local current_suggestion = {
|
||||
text = nil,
|
||||
start_col = nil,
|
||||
namespace_id = vim.api.nvim_create_namespace('gemini_suggestion'),
|
||||
timer = nil
|
||||
timer = nil,
|
||||
last_request = 0,
|
||||
cache = {},
|
||||
debounce_ms = 1000, -- Wait 1 second between requests
|
||||
}
|
||||
|
||||
-- Debug function
|
||||
@ -28,6 +31,21 @@ local function clear_suggestion()
|
||||
end
|
||||
end
|
||||
|
||||
-- Check if we should rate limit
|
||||
local function should_rate_limit()
|
||||
local now = vim.loop.now()
|
||||
if (now - current_suggestion.last_request) < current_suggestion.debounce_ms then
|
||||
return true
|
||||
end
|
||||
current_suggestion.last_request = now
|
||||
return false
|
||||
end
|
||||
|
||||
-- Cache key generation
|
||||
local function get_cache_key(context)
|
||||
return vim.fn.sha256(context)
|
||||
end
|
||||
|
||||
-- Helper function to show suggestion
|
||||
local function show_suggestion(suggestion, start_col)
|
||||
clear_suggestion()
|
||||
@ -100,6 +118,8 @@ function M.trigger_completion()
|
||||
vim.fn.timer_stop(current_suggestion.timer)
|
||||
end
|
||||
|
||||
-- Set up debounce timer
|
||||
current_suggestion.timer = vim.fn.timer_start(300, function()
|
||||
debug_print("Triggering completion...")
|
||||
|
||||
-- Get current buffer and cursor info
|
||||
@ -128,13 +148,27 @@ function M.trigger_completion()
|
||||
return
|
||||
end
|
||||
|
||||
-- Check cache first
|
||||
local cache_key = get_cache_key(prefix)
|
||||
if current_suggestion.cache[cache_key] then
|
||||
debug_print("Using cached completion")
|
||||
show_suggestion(current_suggestion.cache[cache_key], col)
|
||||
return
|
||||
end
|
||||
|
||||
-- Check rate limiting
|
||||
if should_rate_limit() then
|
||||
debug_print("Rate limited, skipping completion request")
|
||||
return
|
||||
end
|
||||
|
||||
debug_print("Getting completion for: %s", prefix)
|
||||
|
||||
-- Construct context from buffer
|
||||
local context = {}
|
||||
|
||||
-- Add up to 10 previous lines for context
|
||||
local start_line = math.max(1, line - 10)
|
||||
-- Add up to 5 previous lines for context (reduced from 10)
|
||||
local start_line = math.max(1, line - 5)
|
||||
for i = start_line, line - 1 do
|
||||
table.insert(context, lines[i])
|
||||
end
|
||||
@ -159,6 +193,9 @@ function M.trigger_completion()
|
||||
end
|
||||
|
||||
if type(response) == "string" then
|
||||
-- Cache the response
|
||||
current_suggestion.cache[cache_key] = response
|
||||
|
||||
vim.schedule(function()
|
||||
-- Check if cursor position is still valid
|
||||
local new_cursor = vim.api.nvim_win_get_cursor(0)
|
||||
@ -168,6 +205,7 @@ function M.trigger_completion()
|
||||
end)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
-- Setup function to create highlight group and keymaps
|
||||
|
Loading…
x
Reference in New Issue
Block a user