Added completion
This commit is contained in:
		@@ -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,18 +66,20 @@ 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)
 | 
			
		||||
    end)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
return M
 | 
			
		||||
@@ -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
 | 
			
		||||
            if #completions > 0 then
 | 
			
		||||
                vim.schedule(function()
 | 
			
		||||
                vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<C-x><C-o>', true, true, true), 'n', true)
 | 
			
		||||
                    -- Update the completion menu
 | 
			
		||||
                    vim.fn.complete(vim.fn.col('.') - #base, completions)
 | 
			
		||||
                end)
 | 
			
		||||
        end)
 | 
			
		||||
 | 
			
		||||
        -- Wait for completion (with timeout)
 | 
			
		||||
        timer:start(0, 100, vim.schedule_wrap(function()
 | 
			
		||||
            if not done then
 | 
			
		||||
                return {}
 | 
			
		||||
            end
 | 
			
		||||
            timer:stop()
 | 
			
		||||
        end))
 | 
			
		||||
        end)
 | 
			
		||||
 | 
			
		||||
        return items
 | 
			
		||||
    end
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user