From a3d24298b95d0aa6dd8e512406ec705b10852039 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Thu, 21 Nov 2024 14:44:54 +0100 Subject: [PATCH] fix: improve selection validation checks Enhance selection validation by properly checking for existence of start_line and end_line values. This prevents potential nil access errors when handling invalid selections. Previously the code only checked if end_line was greater than 0, which could lead to errors if start_line or end_line were nil. --- lua/CopilotChat/copilot.lua | 2 +- lua/CopilotChat/init.lua | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lua/CopilotChat/copilot.lua b/lua/CopilotChat/copilot.lua index 397e078b..71633291 100644 --- a/lua/CopilotChat/copilot.lua +++ b/lua/CopilotChat/copilot.lua @@ -161,7 +161,7 @@ local function generate_selection_messages(selection) local out = string.format('# FILE:%s CONTEXT\n', filename:upper()) out = out .. "User's active selection:\n" - if selection.start_line and selection.end_line > 0 then + if selection.start_line and selection.end_line then out = out .. string.format( 'Excerpt from %s, lines %s to %s:\n', diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index ffcc1161..cca38511 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -75,7 +75,12 @@ local function highlight_selection(clear) end local selection = get_selection(state.config) - if not selection or not utils.buf_valid(selection.bufnr) then + if + not selection + or not utils.buf_valid(selection.bufnr) + or not selection.start_line + or not selection.end_line + then return end