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

View File

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