From f0594c6ecaaf91c7f0d5ed4cbf53e207e66cad89 Mon Sep 17 00:00:00 2001 From: Jonas Widen Date: Mon, 17 Mar 2025 07:51:50 +0100 Subject: [PATCH] debug completion --- lua/gemini/completion.lua | 55 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/lua/gemini/completion.lua b/lua/gemini/completion.lua index 047eb74..be5d254 100644 --- a/lua/gemini/completion.lua +++ b/lua/gemini/completion.lua @@ -193,8 +193,59 @@ function M.trigger_completion() -- Don't trigger completion if line is empty if not current_line or current_line:match("^%s*$") then - debug_print("Skipping: empty line") - clear_suggestion() + -- Instead of skipping, let's ask for what should come next + local context = {} + local cursor_line = line + + -- Add previous lines for context + for i = 1, line - 1 do + if lines[i] and #lines[i] > 0 then + table.insert(context, string.format("L%d: %s", i, lines[i])) + end + end + + -- Add empty current line with cursor position + table.insert(context, string.format("L%d (current): ", cursor_line)) + table.insert(context, "^") + + -- Add following lines for context + for i = line + 1, #lines do + if lines[i] and #lines[i] > 0 then + table.insert(context, string.format("L%d: %s", i, lines[i])) + end + end + + local full_context = table.concat(context, "\n") + if #full_context == 0 then + debug_print("Empty context, skipping completion request") + return + end + + -- Special prompt for empty line suggestions + local prompt = string.format([[I'm editing a %s file. Here's the context around line %d where my cursor (^) is on an empty line: + +%s + +Based on the surrounding code context, what would be the most appropriate code to add at the cursor position? +Return ONLY the code that should be added, no explanation.]], vim.bo.filetype, cursor_line, full_context) + + -- Get completion from Gemini + api.get_response(prompt, nil, function(response, error) + if error then + debug_print("Completion error: %s", error) + return + end + + if type(response) == "string" and #response > 0 then + vim.schedule(function() + -- Check if cursor position is still valid + local new_cursor = vim.api.nvim_win_get_cursor(0) + if new_cursor[1] == line then + show_suggestion(response, 0) -- Start from column 0 since line is empty + end + end) + end + end) return end