Added completion

This commit is contained in:
Jonas Widen 2025-03-16 21:24:10 +01:00
parent 9d659a0af4
commit a6ec0025d4
2 changed files with 37 additions and 14 deletions

View File

@ -13,13 +13,10 @@ local function parse_completion_response(response)
for _, line in ipairs(lines) do
if line and line:match("^%s*[^%s]") then -- Skip empty lines
table.insert(items, {
label = line,
word = line, -- Changed from label to word
kind = vim.lsp.protocol.CompletionItemKind.Text,
detail = "Gemini suggestion",
documentation = {
kind = "markdown",
value = "```\n" .. line .. "\n```"
}
menu = "[Gemini]", -- Added menu indicator
info = line, -- Added info
})
end
end
@ -50,7 +47,9 @@ function M.get_completion(params, callback)
-- Check cache first
local cache_key = prefix .. before_cursor
if completion_cache[cache_key] then
callback(completion_cache[cache_key])
vim.schedule(function()
callback(completion_cache[cache_key])
end)
return
end
@ -58,13 +57,17 @@ function M.get_completion(params, callback)
api.get_response(prompt, nil, function(response, error)
if error then
vim.notify("Completion error: " .. error, vim.log.levels.ERROR)
callback({})
vim.schedule(function()
callback({})
end)
return
end
local items = parse_completion_response(response)
completion_cache[cache_key] = items
callback(items)
vim.schedule(function()
callback(items)
end)
end)
end

View File

@ -43,10 +43,11 @@ function M.setup(opts)
config.setup(opts)
pcall(vim.treesitter.language.require_language, "markdown")
-- Register completion source
vim.api.nvim_create_autocmd("LspAttach", {
callback = function(args)
vim.bo[args.buf].omnifunc = "v:lua.require'gemini.completion'.get_completion"
-- Set up omnifunc globally
vim.api.nvim_create_autocmd("FileType", {
pattern = "*",
callback = function()
vim.bo.omnifunc = "v:lua.require'gemini'.complete"
end,
})
@ -84,7 +85,7 @@ function M.setup(opts)
vim.notify("Chat history cleared", vim.log.levels.INFO)
end, { desc = "Clear Gemini chat history" })
-- Map <C-Space> to trigger Gemini completion
-- Add default completion keymap
vim.keymap.set('i', '<C-Space>', function()
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<C-x><C-o>', true, true, true), 'n', true)
end, { desc = 'Trigger Gemini completion' })
@ -100,10 +101,29 @@ function M.complete(findstart, base)
end
return start
else
-- Create a completion timer
local timer = vim.loop.new_timer()
local items = {}
local done = false
-- Start completion request
completion.get_completion({ word = base }, function(completions)
items = completions
done = true
-- Trigger completion menu refresh
vim.schedule(function()
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<C-x><C-o>', true, true, true), 'n', true)
end)
end)
-- Wait for completion (with timeout)
timer:start(0, 100, vim.schedule_wrap(function()
if not done then
return {}
end
timer:stop()
end))
return items
end
end