From 81c200df66978570662e3d51c5be2a8e170a5e72 Mon Sep 17 00:00:00 2001 From: Jonas Widen Date: Tue, 18 Mar 2025 18:13:13 +0100 Subject: [PATCH] Review and cleanup --- lua/gemini/completion.lua | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lua/gemini/completion.lua b/lua/gemini/completion.lua index 4cf9ed0..647975d 100644 --- a/lua/gemini/completion.lua +++ b/lua/gemini/completion.lua @@ -177,7 +177,17 @@ function M.accept_suggestion() end vim.api.nvim_win_set_cursor(0, {final_line + 1, final_col}) + -- Clear suggestion and cache for the current context clear_suggestion() + current_suggestion.cache = {} -- Clear cache to force new suggestions + + -- Schedule next completion after a short delay + vim.defer_fn(function() + if vim.b.gemini_completion_enabled then + M.trigger_completion() + end + end, 100) + return true end @@ -203,7 +213,7 @@ function M.trigger_completion() vim.fn.timer_stop(current_suggestion.timer) end - -- Set up debounce timer with shorter delay + -- Set up debounce timer current_suggestion.timer = vim.fn.timer_start(150, function() local cursor = vim.api.nvim_win_get_cursor(0) local line = cursor[1] @@ -211,6 +221,12 @@ function M.trigger_completion() local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) local current_line = lines[line] + -- Don't trigger if at end of file or empty line + if not current_line or current_line == "" then + clear_suggestion() + return + end + -- Get text before cursor local prefix = string.sub(current_line, 1, col) @@ -248,7 +264,8 @@ function M.trigger_completion() Return ONLY the completion text that would naturally continue from the cursor position. Focus on completing the current statement or block. -Consider the syntax, style, and patterns in the surrounding code.]], file_type, full_context) +Consider the syntax, style, and patterns in the surrounding code. +Do not repeat any text that appears before the cursor.]], file_type, full_context) -- Check cache and rate limiting local cache_key = get_cache_key(prompt) @@ -270,8 +287,9 @@ Consider the syntax, style, and patterns in the surrounding code.]], file_type, end if type(response) == "string" and #response > 0 then - -- Clean up response + -- Clean up response and remove any leading whitespace/indentation response = vim.trim(response) + response = response:gsub("^%s+", "") current_suggestion.cache[cache_key] = response vim.schedule(function()