Added completion
This commit is contained in:
		
							
								
								
									
										71
									
								
								lua/gemini/completion.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								lua/gemini/completion.lua
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,71 @@ | ||||
| local api = require("gemini.api") | ||||
| local M = {} | ||||
|  | ||||
| -- Cache for completion items | ||||
| local completion_cache = {} | ||||
|  | ||||
| -- Helper function to create completion items from Gemini response | ||||
| local function parse_completion_response(response) | ||||
|     -- Split response into lines and filter out empty ones | ||||
|     local lines = vim.split(response, "\n") | ||||
|     local items = {} | ||||
|      | ||||
|     for _, line in ipairs(lines) do | ||||
|         if line and line:match("^%s*[^%s]") then -- Skip empty lines | ||||
|             table.insert(items, { | ||||
|                 label = line, | ||||
|                 kind = vim.lsp.protocol.CompletionItemKind.Text, | ||||
|                 detail = "Gemini suggestion", | ||||
|                 documentation = { | ||||
|                     kind = "markdown", | ||||
|                     value = "```\n" .. line .. "\n```" | ||||
|                 } | ||||
|             }) | ||||
|         end | ||||
|     end | ||||
|      | ||||
|     return items | ||||
| end | ||||
|  | ||||
| function M.get_completion(params, callback) | ||||
|     local cursor = vim.api.nvim_win_get_cursor(0) | ||||
|     local line = cursor[1] | ||||
|     local col = cursor[2] | ||||
|      | ||||
|     -- Get current buffer content for context | ||||
|     local lines = vim.api.nvim_buf_get_lines(0, math.max(0, line - 10), line, false) | ||||
|     local prefix = table.concat(lines, "\n") | ||||
|      | ||||
|     -- Get current line up to cursor | ||||
|     local current_line = vim.api.nvim_get_current_line() | ||||
|     local before_cursor = string.sub(current_line, 1, col) | ||||
|      | ||||
|     -- Construct prompt for Gemini | ||||
|     local prompt = string.format( | ||||
|         "Complete this code. Only provide the completion, no explanation:\n%s\n%s", | ||||
|         prefix, | ||||
|         before_cursor | ||||
|     ) | ||||
|      | ||||
|     -- Check cache first | ||||
|     local cache_key = prefix .. before_cursor | ||||
|     if completion_cache[cache_key] then | ||||
|         callback(completion_cache[cache_key]) | ||||
|         return | ||||
|     end | ||||
|      | ||||
|     -- Get completion from Gemini | ||||
|     api.get_response(prompt, nil, function(response, error) | ||||
|         if error then | ||||
|             vim.notify("Completion error: " .. error, vim.log.levels.ERROR) | ||||
|             callback({}) | ||||
|             return | ||||
|         end | ||||
|          | ||||
|         local items = parse_completion_response(response) | ||||
|         completion_cache[cache_key] = items | ||||
|         callback(items) | ||||
|     end) | ||||
| end | ||||
|  | ||||
| return M | ||||
| @@ -3,6 +3,7 @@ | ||||
| local api = require("gemini.api") | ||||
| local chat = require("gemini.chat") | ||||
| local config = require("gemini.config") | ||||
| local completion = require("gemini.completion") | ||||
|  | ||||
| local M = {} | ||||
|  | ||||
| @@ -42,6 +43,13 @@ 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" | ||||
|         end, | ||||
|     }) | ||||
|  | ||||
|     vim.api.nvim_create_user_command("Gemini", function(opts) | ||||
|         if opts.args == "" then | ||||
|             vim.notify("Please provide a prompt for Gemini.", vim.log.levels.WARN) | ||||
| @@ -75,6 +83,29 @@ function M.setup(opts) | ||||
|         chat.clear() | ||||
|         vim.notify("Chat history cleared", vim.log.levels.INFO) | ||||
|     end, { desc = "Clear Gemini chat history" }) | ||||
|  | ||||
|     -- Map <C-Space> to trigger Gemini completion | ||||
|     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' }) | ||||
| end | ||||
|  | ||||
| function M.complete(findstart, base) | ||||
|     if findstart == 1 then | ||||
|         local line = vim.api.nvim_get_current_line() | ||||
|         local col = vim.api.nvim_win_get_cursor(0)[2] | ||||
|         local start = col | ||||
|         while start > 0 and string.match(line:sub(start, start), "[%w_]") do | ||||
|             start = start - 1 | ||||
|         end | ||||
|         return start | ||||
|     else | ||||
|         local items = {} | ||||
|         completion.get_completion({ word = base }, function(completions) | ||||
|             items = completions | ||||
|         end) | ||||
|         return items | ||||
|     end | ||||
| end | ||||
|  | ||||
| return M | ||||
|   | ||||
		Reference in New Issue
	
	Block a user