Skip to content

Commit

Permalink
fix: improve selection validation checks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
deathbeam committed Nov 21, 2024
1 parent 9f3366f commit a3d2429
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lua/CopilotChat/copilot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 6 additions & 1 deletion lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a3d2429

Please sign in to comment.