Added completion

This commit is contained in:
Jonas Widen 2025-03-16 21:30:11 +01:00
parent 8d69ca7340
commit a91239f4c1
2 changed files with 37 additions and 28 deletions

View File

@ -6,6 +6,10 @@ local completion_cache = {}
-- Helper function to create completion items from Gemini response
local function parse_completion_response(response)
if type(response) ~= "string" then
return {}
end
-- Split response into lines and filter out empty ones
local lines = vim.split(response, "\n")
local items = {}
@ -13,14 +17,25 @@ 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, {
word = line, -- Changed from label to word
word = line:match("^%s*(.-)%s*$"), -- Trim whitespace
kind = vim.lsp.protocol.CompletionItemKind.Text,
menu = "[Gemini]", -- Added menu indicator
info = line, -- Added info
menu = "[Gemini]",
info = line,
})
end
end
-- Always return at least one item
if #items == 0 then
items = {
{
word = "No completions found",
kind = vim.lsp.protocol.CompletionItemKind.Text,
menu = "[Gemini]"
}
}
end
return items
end
@ -51,17 +66,19 @@ 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)
vim.schedule(function()
callback({})
end)
callback({
{
word = "Error: " .. error,
kind = vim.lsp.protocol.CompletionItemKind.Text,
menu = "[Gemini]"
}
})
return
end
vim.notify("Received completion response", vim.log.levels.INFO)
local items = parse_completion_response(response)
vim.schedule(function()
callback(items)
end)
callback(items)
end)
end

View File

@ -103,28 +103,20 @@ function M.complete(findstart, base)
end
return start
else
-- Create a completion timer
local timer = vim.loop.new_timer()
local items = {}
local done = false
-- Return empty list immediately to keep menu open
local items = {
{ word = "Loading...", kind = vim.lsp.protocol.CompletionItemKind.Text }
}
-- 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 {}
if #completions > 0 then
vim.schedule(function()
-- Update the completion menu
vim.fn.complete(vim.fn.col('.') - #base, completions)
end)
end
timer:stop()
end))
end)
return items
end