Skip to content

Commit 8ff45d2

Browse files
committed
Split handling of highlighted code lines into helper function
1 parent 8ec1977 commit 8ff45d2

2 files changed

Lines changed: 68 additions & 34 deletions

File tree

extensions/ql-vscode/src/pure/sarif-utils.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as Sarif from 'sarif';
2+
import { HighlightedRegion } from '../remote-queries/shared/analysis-result';
23
import { ResolvableLocationValue } from './bqrs-cli-types';
34

45
export interface SarifLink {
@@ -173,3 +174,65 @@ export function parseSarifRegion(
173174
export function isNoLocation(loc: ParsedSarifLocation): loc is NoLocation {
174175
return 'hint' in loc;
175176
}
177+
178+
// Some helpers for highlighting specific regions from a SARIF code snippet
179+
180+
/**
181+
* Checks whether a particular line (determined by its line number in the original file)
182+
* is part of the highlighted region of a SARIF code snippet.
183+
*/
184+
export function shouldHighlightLine(
185+
lineNumber: number,
186+
highlightedRegion: HighlightedRegion
187+
): boolean {
188+
if (lineNumber < highlightedRegion.startLine) {
189+
return false;
190+
}
191+
192+
if (highlightedRegion.endLine == undefined) {
193+
return lineNumber == highlightedRegion.startLine;
194+
}
195+
196+
return lineNumber <= highlightedRegion.endLine;
197+
}
198+
199+
/**
200+
* A line of code split into: plain text before the highlighted section, the highlighted
201+
* text itself, and plain text after the highlighted section.
202+
*/
203+
interface partiallyHighlightedLine {
204+
plainSection1: string;
205+
highlightedSection: string;
206+
plainSection2: string;
207+
}
208+
209+
/**
210+
* Splits a line of code into the highlighted and non-highlighted sections.
211+
*/
212+
export function parseHighlightedLine(
213+
line: string,
214+
lineNumber: number,
215+
highlightedRegion: HighlightedRegion
216+
): partiallyHighlightedLine {
217+
const isSingleLineHighlight = highlightedRegion.endLine === undefined;
218+
const isFirstHighlightedLine = lineNumber === highlightedRegion.startLine;
219+
const isLastHighlightedLine = lineNumber === highlightedRegion.endLine;
220+
221+
const highlightStartColumn = isSingleLineHighlight
222+
? highlightedRegion.startColumn
223+
: isFirstHighlightedLine
224+
? highlightedRegion.startColumn
225+
: 0;
226+
227+
const highlightEndColumn = isSingleLineHighlight
228+
? highlightedRegion.endColumn
229+
: isLastHighlightedLine
230+
? highlightedRegion.endColumn
231+
: line.length + 1;
232+
233+
const plainSection1 = line.substring(0, highlightStartColumn - 1);
234+
const highlightedSection = line.substring(highlightStartColumn - 1, highlightEndColumn - 1);
235+
const plainSection2 = line.substring(highlightEndColumn - 1, line.length);
236+
237+
return { plainSection1, highlightedSection, plainSection2 };
238+
}

extensions/ql-vscode/src/remote-queries/view/FileCodeSnippet.tsx

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CodeSnippet, FileLink, HighlightedRegion, AnalysisMessage, ResultSeveri
44
import { Box, Link } from '@primer/react';
55
import VerticalSpace from './VerticalSpace';
66
import { createRemoteFileRef } from '../../pure/location-link-utils';
7+
import { parseHighlightedLine, shouldHighlightLine } from '../../pure/sarif-utils';
78

89
const borderColor = 'var(--vscode-editor-snippetFinalTabstopHighlightBorder)';
910
const warningColor = '#966C23';
@@ -22,18 +23,6 @@ const getSeverityColor = (severity: ResultSeverity) => {
2223

2324
const replaceSpaceChar = (text: string) => text.replaceAll(' ', '\u00a0');
2425

25-
const shouldHighlightLine = (lineNumber: number, highlightedRegion: HighlightedRegion) => {
26-
if (lineNumber < highlightedRegion.startLine) {
27-
return false;
28-
}
29-
30-
if (highlightedRegion.endLine == undefined) {
31-
return lineNumber == highlightedRegion.startLine;
32-
}
33-
34-
return lineNumber <= highlightedRegion.endLine;
35-
};
36-
3726
const Container = styled.div`
3827
font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace;
3928
font-size: x-small;
@@ -142,31 +131,13 @@ const CodeLine = ({
142131
return <PlainLine text={line} />;
143132
}
144133

145-
const isSingleLineHighlight = highlightedRegion.endLine === undefined;
146-
const isFirstHighlightedLine = lineNumber === highlightedRegion.startLine;
147-
const isLastHighlightedLine = lineNumber === highlightedRegion.endLine;
148-
149-
const highlightStartColumn = isSingleLineHighlight
150-
? highlightedRegion.startColumn
151-
: isFirstHighlightedLine
152-
? highlightedRegion.startColumn
153-
: 0;
154-
155-
const highlightEndColumn = isSingleLineHighlight
156-
? highlightedRegion.endColumn
157-
: isLastHighlightedLine
158-
? highlightedRegion.endColumn
159-
: line.length + 1;
160-
161-
const section1 = line.substring(0, highlightStartColumn - 1);
162-
const section2 = line.substring(highlightStartColumn - 1, highlightEndColumn - 1);
163-
const section3 = line.substring(highlightEndColumn - 1, line.length);
134+
const partiallyHighlightedLine = parseHighlightedLine(line, lineNumber, highlightedRegion);
164135

165136
return (
166137
<>
167-
<PlainLine text={section1} />
168-
<HighlightedLine text={section2} />
169-
<PlainLine text={section3} />
138+
<PlainLine text={partiallyHighlightedLine.plainSection1} />
139+
<HighlightedLine text={partiallyHighlightedLine.highlightedSection} />
140+
<PlainLine text={partiallyHighlightedLine.plainSection2} />
170141
</>
171142
);
172143
};

0 commit comments

Comments
 (0)