From e172d7394e20757b688a1a93eab79e75d20562ab Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Wed, 30 Oct 2024 11:24:15 +0100 Subject: [PATCH] fix: Ensure that start_col and end_col selection is awlays within bounds Closes #432 Signed-off-by: Tomas Slusny --- lua/CopilotChat/select.lua | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/lua/CopilotChat/select.lua b/lua/CopilotChat/select.lua index 29ea6bf..cb95872 100644 --- a/lua/CopilotChat/select.lua +++ b/lua/CopilotChat/select.lua @@ -1,27 +1,46 @@ local M = {} local function get_selection_lines(bufnr, start_line, start_col, finish_line, finish_col, full_line) + -- Exit if no actual selection if start_line == finish_line and start_col == finish_col then return nil end + -- Get line lengths before swapping + local function get_line_length(line) + return #vim.api.nvim_buf_get_lines(bufnr, line - 1, line, false)[1] + end + + -- Swap positions if selection is backwards if start_line > finish_line or (start_line == finish_line and start_col > finish_col) then start_line, finish_line = finish_line, start_line start_col, finish_col = finish_col, start_col end + -- Handle full line selection if full_line then start_col = 1 + finish_col = get_line_length(finish_line) + end + + -- Ensure columns are within valid bounds + start_col = math.max(1, math.min(start_col, get_line_length(start_line))) + finish_col = math.max(start_col, math.min(finish_col, get_line_length(finish_line))) + + -- Get selected text + local ok, lines = pcall( + vim.api.nvim_buf_get_text, + bufnr, + start_line - 1, + start_col - 1, + finish_line - 1, + finish_col, + {} + ) + if not ok then + return nil end - local finish_line_len = #vim.api.nvim_buf_get_lines(bufnr, finish_line - 1, finish_line, false)[1] - if finish_col > finish_line_len or full_line then - finish_col = finish_line_len - end - - local lines = - vim.api.nvim_buf_get_text(bufnr, start_line - 1, start_col - 1, finish_line - 1, finish_col, {}) - local lines_content = table.concat(lines, '\n') if vim.trim(lines_content) == '' then return nil