Review and cleanup

This commit is contained in:
Jonas Widen 2025-03-18 18:09:25 +01:00
parent 2611a0624b
commit 051399b1bd

View File

@ -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,
})