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,73 +118,93 @@ function M.trigger_completion()
|
||||
vim.fn.timer_stop(current_suggestion.timer)
|
||||
end
|
||||
|
||||
debug_print("Triggering completion...")
|
||||
|
||||
-- Get current buffer and cursor info
|
||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||
local line = cursor[1]
|
||||
local col = cursor[2]
|
||||
|
||||
-- Get entire buffer content
|
||||
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
|
||||
local current_line = lines[line]
|
||||
|
||||
-- Don't trigger completion if line is empty or cursor is at the start
|
||||
if col == 0 or current_line:match("^%s*$") then
|
||||
clear_suggestion()
|
||||
return
|
||||
end
|
||||
|
||||
-- Get text before cursor on current line
|
||||
local prefix = string.sub(current_line, 1, col)
|
||||
|
||||
-- Don't trigger on certain conditions
|
||||
if prefix:match("^%s*$") or -- empty or whitespace
|
||||
prefix:match("[%s%-]$") or -- ends with space or dash
|
||||
#prefix < 3 then -- too short
|
||||
clear_suggestion()
|
||||
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)
|
||||
for i = start_line, line - 1 do
|
||||
table.insert(context, lines[i])
|
||||
end
|
||||
|
||||
-- Add current line
|
||||
table.insert(context, current_line)
|
||||
|
||||
-- Combine all lines
|
||||
local full_context = table.concat(context, "\n")
|
||||
|
||||
-- Construct prompt for Gemini
|
||||
local prompt = string.format(
|
||||
"Complete this code. Return ONLY the completion that would naturally follow, no explanation:\n%s",
|
||||
full_context
|
||||
)
|
||||
|
||||
-- Get completion from Gemini
|
||||
api.get_response(prompt, nil, function(response, error)
|
||||
if error then
|
||||
debug_print("Completion error: %s", error)
|
||||
return
|
||||
-- Set up debounce timer
|
||||
current_suggestion.timer = vim.fn.timer_start(300, function()
|
||||
debug_print("Triggering completion...")
|
||||
|
||||
-- Get current buffer and cursor info
|
||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||
local line = cursor[1]
|
||||
local col = cursor[2]
|
||||
|
||||
-- Get entire buffer content
|
||||
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
|
||||
local current_line = lines[line]
|
||||
|
||||
-- Don't trigger completion if line is empty or cursor is at the start
|
||||
if col == 0 or current_line:match("^%s*$") then
|
||||
clear_suggestion()
|
||||
return
|
||||
end
|
||||
|
||||
if type(response) == "string" then
|
||||
vim.schedule(function()
|
||||
-- Check if cursor position is still valid
|
||||
local new_cursor = vim.api.nvim_win_get_cursor(0)
|
||||
if new_cursor[1] == line and new_cursor[2] >= col then
|
||||
show_suggestion(response, col)
|
||||
end
|
||||
end)
|
||||
-- Get text before cursor on current line
|
||||
local prefix = string.sub(current_line, 1, col)
|
||||
|
||||
-- Don't trigger on certain conditions
|
||||
if prefix:match("^%s*$") or -- empty or whitespace
|
||||
prefix:match("[%s%-]$") or -- ends with space or dash
|
||||
#prefix < 3 then -- too short
|
||||
clear_suggestion()
|
||||
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 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
|
||||
|
||||
-- Add current line
|
||||
table.insert(context, current_line)
|
||||
|
||||
-- Combine all lines
|
||||
local full_context = table.concat(context, "\n")
|
||||
|
||||
-- Construct prompt for Gemini
|
||||
local prompt = string.format(
|
||||
"Complete this code. Return ONLY the completion that would naturally follow, no explanation:\n%s",
|
||||
full_context
|
||||
)
|
||||
|
||||
-- Get completion from Gemini
|
||||
api.get_response(prompt, nil, function(response, error)
|
||||
if error then
|
||||
debug_print("Completion error: %s", error)
|
||||
return
|
||||
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)
|
||||
if new_cursor[1] == line and new_cursor[2] >= col then
|
||||
show_suggestion(response, col)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user