From 051399b1bdf262060661ceebccd9e1913c8bcb96 Mon Sep 17 00:00:00 2001 From: Jonas Widen Date: Tue, 18 Mar 2025 18:09:25 +0100 Subject: [PATCH] Review and cleanup --- lua/gemini/completion.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lua/gemini/completion.lua b/lua/gemini/completion.lua index a6bf187..b9125f3 100644 --- a/lua/gemini/completion.lua +++ b/lua/gemini/completion.lua @@ -60,6 +60,11 @@ local function show_suggestion(suggestion, start_col) local line = vim.api.nvim_win_get_cursor(0)[1] - 1 local line_text = vim.api.nvim_buf_get_lines(0, line, line + 1, true)[1] + -- Validate start_col + if not line_text then return end + start_col = math.min(start_col, #line_text) + if start_col < 0 then start_col = 0 end + -- Get text before cursor on current line local input_before_cursor = string.sub(line_text, 1, start_col) local first_line = suggestion_lines[1] @@ -76,7 +81,10 @@ local function show_suggestion(suggestion, start_col) -- Show first line as virtual text if first_line ~= "" then - vim.api.nvim_buf_set_extmark(0, current_suggestion.namespace_id, line, start_col, { + -- Ensure we're not exceeding line length + local safe_col = math.min(start_col, #line_text) + + vim.api.nvim_buf_set_extmark(0, current_suggestion.namespace_id, line, safe_col, { virt_text = {{first_line, 'GeminiSuggestion'}}, virt_text_pos = 'inline', virt_text_hide = true, @@ -90,7 +98,10 @@ local function show_suggestion(suggestion, start_col) table.insert(virt_lines, {{suggestion_lines[i], 'GeminiSuggestion'}}) end - vim.api.nvim_buf_set_extmark(0, current_suggestion.namespace_id, line, start_col, { + -- Use safe column position for virtual lines + local safe_col = math.min(start_col, #line_text) + + vim.api.nvim_buf_set_extmark(0, current_suggestion.namespace_id, line, safe_col, { virt_lines = virt_lines, virt_lines_above = false, })