debug completion

This commit is contained in:
Jonas Widen 2025-03-17 07:34:06 +01:00
parent 541607cfbc
commit 168e01100e
2 changed files with 41 additions and 6 deletions

View File

@ -183,6 +183,8 @@ function M.accept_suggestion()
end
function M.trigger_completion()
debug_print("Triggering completion...")
-- Clear any existing timer
if current_suggestion.timer then
vim.fn.timer_stop(current_suggestion.timer)
@ -190,6 +192,8 @@ function M.trigger_completion()
-- Set up debounce timer
current_suggestion.timer = vim.fn.timer_start(300, function()
debug_print("Timer fired, checking conditions...")
-- Get current buffer and cursor info
local cursor = vim.api.nvim_win_get_cursor(0)
local line = cursor[1]
@ -199,8 +203,11 @@ function M.trigger_completion()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local current_line = lines[line]
debug_print("Current line: %s, col: %d", current_line or "nil", col)
-- Don't trigger completion if line is empty or cursor is at the start
if not current_line or col == 0 or current_line:match("^%s*$") then
debug_print("Skipping: empty line or cursor at start")
clear_suggestion()
return
end
@ -208,14 +215,18 @@ function M.trigger_completion()
-- Get text before cursor on current line
local prefix = string.sub(current_line, 1, col)
debug_print("Prefix: %s", prefix)
-- 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
debug_print("Skipping: prefix conditions not met")
clear_suggestion()
return
end
debug_print("Preparing context...")
-- Construct context with line numbers and cursor position
local context = {}
local cursor_line = line

View File

@ -41,10 +41,27 @@ end
function M.setup(opts)
config.setup(opts)
pcall(vim.treesitter.language.require_language, "markdown")
-- Set up completion
completion.setup()
-- Enable completion by default for all buffers
vim.api.nvim_create_autocmd("BufEnter", {
pattern = "*",
callback = function()
vim.b.gemini_completion_enabled = true
end
})
-- Completion keymaps
vim.keymap.set('i', '<leader>gg', function()
debug_print("Keymap triggered")
-- Enable completion for current buffer
vim.b.gemini_completion_enabled = true
completion.trigger_completion()
end, { desc = 'Trigger Gemini completion' })
pcall(vim.treesitter.language.require_language, "markdown")
-- Set up commands
vim.api.nvim_create_user_command("Gemini", function(opts)
@ -82,10 +99,17 @@ function M.setup(opts)
vim.notify("Chat history cleared", vim.log.levels.INFO)
end, { desc = "Clear Gemini chat history" })
-- Completion keymaps
vim.keymap.set('i', '<leader>gg', function()
completion.trigger_completion()
end, { desc = 'Trigger Gemini completion' })
-- Add a command to toggle completion
vim.api.nvim_create_user_command("GeminiToggleCompletion", function()
vim.b.gemini_completion_enabled = not vim.b.gemini_completion_enabled
vim.notify(
string.format("Gemini completion %s",
vim.b.gemini_completion_enabled and "enabled" or "disabled"),
vim.log.levels.INFO
)
end, {
desc = "Toggle Gemini completion"
})
end
function M.complete(findstart, base)