debug completion
This commit is contained in:
		| @@ -67,9 +67,26 @@ function M.get_response(prompt, context, callback) | |||||||
| 		return | 		return | ||||||
| 	end | 	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) | 	store_message("user", prompt) | ||||||
|  |  | ||||||
| 	local contents = create_contents(prompt, context) | 	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 payload = vim.json.encode({contents = contents}) | ||||||
| 	 | 	 | ||||||
| 	local url = string.format( | 	local url = string.format( | ||||||
|   | |||||||
| @@ -190,8 +190,6 @@ function M.trigger_completion() | |||||||
|      |      | ||||||
|     -- Set up debounce timer |     -- Set up debounce timer | ||||||
|     current_suggestion.timer = vim.fn.timer_start(300, function() |     current_suggestion.timer = vim.fn.timer_start(300, function() | ||||||
|         debug_print("Triggering completion...") |  | ||||||
|          |  | ||||||
|         -- Get current buffer and cursor info |         -- Get current buffer and cursor info | ||||||
|         local cursor = vim.api.nvim_win_get_cursor(0) |         local cursor = vim.api.nvim_win_get_cursor(0) | ||||||
|         local line = cursor[1] |         local line = cursor[1] | ||||||
| @@ -202,7 +200,7 @@ function M.trigger_completion() | |||||||
|         local current_line = lines[line] |         local current_line = lines[line] | ||||||
|          |          | ||||||
|         -- Don't trigger completion if line is empty or cursor is at the start |         -- 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() |             clear_suggestion() | ||||||
|             return |             return | ||||||
|         end |         end | ||||||
| @@ -218,26 +216,9 @@ function M.trigger_completion() | |||||||
|             return |             return | ||||||
|         end |         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 |         -- Construct context with line numbers and cursor position | ||||||
|         local context = {} |         local context = {} | ||||||
|         local cursor_line = line |         local cursor_line = line | ||||||
|         local cursor_col = col |  | ||||||
|          |          | ||||||
|         -- Add up to 10 previous lines for context |         -- Add up to 10 previous lines for context | ||||||
|         local start_line = math.max(1, line - 10) |         local start_line = math.max(1, line - 10) | ||||||
| @@ -249,7 +230,7 @@ function M.trigger_completion() | |||||||
|          |          | ||||||
|         -- Add current line with cursor position marker (^) |         -- Add current line with cursor position marker (^) | ||||||
|         if current_line and #current_line > 0 then |         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, string.format("L%d (current): %s", cursor_line, current_line)) | ||||||
|             table.insert(context, cursor_marker) |             table.insert(context, cursor_marker) | ||||||
|         end |         end | ||||||
| @@ -273,19 +254,25 @@ function M.trigger_completion() | |||||||
|         local file_type = vim.bo.filetype |         local file_type = vim.bo.filetype | ||||||
|          |          | ||||||
|         -- Construct prompt for Gemini |         -- Construct prompt for Gemini | ||||||
|         local prompt = string.format([[ |         local prompt = string.format([[I'm editing a %s file. Here's the context around line %d where my cursor (^) is: | ||||||
| I'm editing a %s file. Here's the context around line %d where my cursor (^) is: |  | ||||||
|  |  | ||||||
| %s | %s | ||||||
|  |  | ||||||
| Complete the code at the cursor position. Consider the syntax and style of the surrounding code. | 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. | Return ONLY the completion that would naturally follow, no explanation. | ||||||
| The completion can be multiple lines if appropriate for the context. | The completion can be multiple lines if appropriate for the context.]], file_type, cursor_line, full_context) | ||||||
| ]], file_type, cursor_line, full_context) |  | ||||||
|          |          | ||||||
|         -- Validate prompt before sending |         -- Check cache first | ||||||
|         if #prompt == 0 then |         local cache_key = get_cache_key(prompt) | ||||||
|             debug_print("Empty prompt, skipping completion request") |         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 |             return | ||||||
|         end |         end | ||||||
|          |          | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user