From 649223a607599ad3aafa367f0a48a724d68d0d3c Mon Sep 17 00:00:00 2001 From: bkellam Date: Tue, 17 Sep 2024 23:58:29 -0700 Subject: [PATCH] Add safety to range highlighting for search result panel --- src/app/search/searchResultsPanel.tsx | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/app/search/searchResultsPanel.tsx b/src/app/search/searchResultsPanel.tsx index 893c8ad8..79ee69c9 100644 --- a/src/app/search/searchResultsPanel.tsx +++ b/src/app/search/searchResultsPanel.tsx @@ -239,9 +239,26 @@ const CodePreview = ({ .sort((a, b) => { return a.Start.ByteOffset - b.Start.ByteOffset; }) + .filter(({ Start, End }) => { + const startLine = Start.LineNumber - lineOffset; + const endLine = End.LineNumber - lineOffset; + + if ( + startLine < 1 || + endLine < 1 || + startLine > document.lines || + endLine > document.lines + ) { + return false; + } + return true; + }) .map(({ Start, End }) => { - const from = document.line(Start.LineNumber - lineOffset).from + Start.Column - 1; - const to = document.line(End.LineNumber - lineOffset).from + End.Column - 1; + const startLine = Start.LineNumber - lineOffset; + const endLine = End.LineNumber - lineOffset; + + const from = document.line(startLine).from + Start.Column - 1; + const to = document.line(endLine).from + End.Column - 1; return markDecoration.range(from, to); }); @@ -254,7 +271,7 @@ const CodePreview = ({ }), cmTheme ]; - }, [ranges]); + }, [ranges, lineOffset]); const extensions = useMemo(() => { return [