From dea67e29722599d2592a713a464e26bceb51055d Mon Sep 17 00:00:00 2001 From: Jonas Widen Date: Mon, 17 Mar 2025 07:22:46 +0100 Subject: [PATCH] debug completion --- lua/gemini/api.lua | 17 ++++++++++++++++ lua/gemini/completion.lua | 43 ++++++++++++++------------------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/lua/gemini/api.lua b/lua/gemini/api.lua index b5d2f10..731f178 100644 --- a/lua/gemini/api.lua +++ b/lua/gemini/api.lua @@ -67,9 +67,26 @@ function M.get_response(prompt, context, callback) return end + -- Validate prompt + if not prompt or prompt:match("^%s*$") then + vim.schedule(function() + callback(nil, "Empty prompt") + end) + return + end + store_message("user", prompt) local contents = create_contents(prompt, context) + + -- Validate contents before sending + if not contents or #contents == 0 then + vim.schedule(function() + callback(nil, "Failed to create request contents") + end) + return + end + local payload = vim.json.encode({contents = contents}) local url = string.format( diff --git a/lua/gemini/completion.lua b/lua/gemini/completion.lua index fbc234a..4026351 100644 --- a/lua/gemini/completion.lua +++ b/lua/gemini/completion.lua @@ -190,8 +190,6 @@ function M.trigger_completion() -- Set up debounce timer current_suggestion.timer = vim.fn.timer_start(300, function() - debug_print("Triggering completion...") - -- Get current buffer and cursor info local cursor = vim.api.nvim_win_get_cursor(0) local line = cursor[1] @@ -202,7 +200,7 @@ function M.trigger_completion() local current_line = lines[line] -- Don't trigger completion if line is empty or cursor is at the start - if col == 0 or current_line:match("^%s*$") then + if not current_line or col == 0 or current_line:match("^%s*$") then clear_suggestion() return end @@ -218,26 +216,9 @@ function M.trigger_completion() return end - -- Check cache first - local cache_key = get_cache_key(prefix) - if current_suggestion.cache[cache_key] then - debug_print("Using cached completion") - show_suggestion(current_suggestion.cache[cache_key], col) - return - end - - -- Check rate limiting - if should_rate_limit() then - debug_print("Rate limited, skipping completion request") - return - end - - debug_print("Getting completion for: %s", prefix) - -- Construct context with line numbers and cursor position local context = {} local cursor_line = line - local cursor_col = col -- Add up to 10 previous lines for context local start_line = math.max(1, line - 10) @@ -249,7 +230,7 @@ function M.trigger_completion() -- Add current line with cursor position marker (^) if current_line and #current_line > 0 then - local cursor_marker = string.rep(" ", cursor_col) .. "^" + local cursor_marker = string.rep(" ", col) .. "^" table.insert(context, string.format("L%d (current): %s", cursor_line, current_line)) table.insert(context, cursor_marker) end @@ -273,19 +254,25 @@ function M.trigger_completion() local file_type = vim.bo.filetype -- Construct prompt for Gemini - local prompt = string.format([[ -I'm editing a %s file. Here's the context around line %d where my cursor (^) is: + local prompt = string.format([[I'm editing a %s file. Here's the context around line %d where my cursor (^) is: %s Complete the code at the cursor position. Consider the syntax and style of the surrounding code. Return ONLY the completion that would naturally follow, no explanation. -The completion can be multiple lines if appropriate for the context. -]], file_type, cursor_line, full_context) +The completion can be multiple lines if appropriate for the context.]], file_type, cursor_line, full_context) - -- Validate prompt before sending - if #prompt == 0 then - debug_print("Empty prompt, skipping completion request") + -- Check cache first + local cache_key = get_cache_key(prompt) + if current_suggestion.cache[cache_key] then + debug_print("Using cached completion") + show_suggestion(current_suggestion.cache[cache_key], col) + return + end + + -- Check rate limiting + if should_rate_limit() then + debug_print("Rate limited, skipping completion request") return end