From a6ec0025d4a0281f9cb94e1dd61c4712f26c1f3e Mon Sep 17 00:00:00 2001 From: Jonas Widen Date: Sun, 16 Mar 2025 21:24:10 +0100 Subject: [PATCH] Added completion --- lua/gemini/completion.lua | 21 ++++++++++++--------- lua/gemini/init.lua | 30 +++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lua/gemini/completion.lua b/lua/gemini/completion.lua index 3b5fc1f..b2be95a 100644 --- a/lua/gemini/completion.lua +++ b/lua/gemini/completion.lua @@ -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 diff --git a/lua/gemini/init.lua b/lua/gemini/init.lua index 3aa7536..5dd8246 100644 --- a/lua/gemini/init.lua +++ b/lua/gemini/init.lua @@ -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 to trigger Gemini completion + -- Add default completion keymap vim.keymap.set('i', '', function() vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', 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('', 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