Skip to content

Commit 6808d7d

Browse files
authored
MRVA: Display alert text even if location is undefined (#1407)
1 parent 3480aa5 commit 6808d7d

File tree

1 file changed

+117
-85
lines changed

1 file changed

+117
-85
lines changed

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

Lines changed: 117 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -55,71 +55,61 @@ const MessageContainer = styled.div`
5555
padding-bottom: 0.5em;
5656
`;
5757

58-
const PlainLine = ({ text }: { text: string }) => {
58+
const PlainCode = ({ text }: { text: string }) => {
5959
return <span>{replaceSpaceAndTabChar(text)}</span>;
6060
};
6161

62-
const HighlightedLine = ({ text }: { text: string }) => {
62+
const HighlightedCode = ({ text }: { text: string }) => {
6363
return <span style={{ backgroundColor: highlightColor }}>{replaceSpaceAndTabChar(text)}</span>;
6464
};
6565

6666
const Message = ({
6767
message,
68-
currentLineNumber,
69-
highlightedRegion,
7068
borderLeftColor,
7169
children
7270
}: {
7371
message: AnalysisMessage,
74-
currentLineNumber: number,
75-
highlightedRegion?: HighlightedRegion,
7672
borderLeftColor: string,
7773
children: React.ReactNode
7874
}) => {
79-
if (!highlightedRegion || highlightedRegion.endLine !== currentLineNumber) {
80-
return <></>;
81-
}
82-
83-
return <MessageContainer>
84-
<div style={{
85-
borderColor: borderColor,
86-
borderWidth: '0.1em',
87-
borderStyle: 'solid',
88-
borderLeftColor: borderLeftColor,
89-
borderLeftWidth: '0.3em',
90-
paddingTop: '1em',
91-
paddingBottom: '1em'
92-
}}>
93-
<MessageText>
94-
{message.tokens.map((token, index) => {
95-
switch (token.t) {
96-
case 'text':
97-
return <span key={`token-${index}`}>{token.text}</span>;
98-
case 'location':
99-
return <VSCodeLink
100-
style={{ fontFamily: 'var(--vscode-editor-font-family)' }}
101-
key={`token-${index}`}
102-
href={createRemoteFileRef(
103-
token.location.fileLink,
104-
token.location.highlightedRegion?.startLine,
105-
token.location.highlightedRegion?.endLine)}>
106-
{token.text}
107-
</VSCodeLink>;
108-
default:
109-
return <></>;
110-
}
111-
})}
112-
{children && <>
113-
<VerticalSpace size={2} />
114-
{children}
115-
</>
75+
return <div style={{
76+
borderColor: borderColor,
77+
borderWidth: '0.1em',
78+
borderStyle: 'solid',
79+
borderLeftColor: borderLeftColor,
80+
borderLeftWidth: '0.3em',
81+
paddingTop: '1em',
82+
paddingBottom: '1em'
83+
}}>
84+
<MessageText>
85+
{message.tokens.map((token, index) => {
86+
switch (token.t) {
87+
case 'text':
88+
return <span key={`token-${index}`}>{token.text}</span>;
89+
case 'location':
90+
return <VSCodeLink
91+
style={{ fontFamily: 'var(--vscode-editor-font-family)' }}
92+
key={`token-${index}`}
93+
href={createRemoteFileRef(
94+
token.location.fileLink,
95+
token.location.highlightedRegion?.startLine,
96+
token.location.highlightedRegion?.endLine)}>
97+
{token.text}
98+
</VSCodeLink>;
99+
default:
100+
return <></>;
116101
}
117-
</MessageText>
118-
</div>
119-
</MessageContainer>;
102+
})}
103+
{children && <>
104+
<VerticalSpace size={2} />
105+
{children}
106+
</>
107+
}
108+
</MessageText>
109+
</div>;
120110
};
121111

122-
const CodeLine = ({
112+
const Code = ({
123113
line,
124114
lineNumber,
125115
highlightedRegion
@@ -129,20 +119,80 @@ const CodeLine = ({
129119
highlightedRegion?: HighlightedRegion
130120
}) => {
131121
if (!highlightedRegion || !shouldHighlightLine(lineNumber, highlightedRegion)) {
132-
return <PlainLine text={line} />;
122+
return <PlainCode text={line} />;
133123
}
134124

135125
const partiallyHighlightedLine = parseHighlightedLine(line, lineNumber, highlightedRegion);
136126

137127
return (
138128
<>
139-
<PlainLine text={partiallyHighlightedLine.plainSection1} />
140-
<HighlightedLine text={partiallyHighlightedLine.highlightedSection} />
141-
<PlainLine text={partiallyHighlightedLine.plainSection2} />
129+
<PlainCode text={partiallyHighlightedLine.plainSection1} />
130+
<HighlightedCode text={partiallyHighlightedLine.highlightedSection} />
131+
<PlainCode text={partiallyHighlightedLine.plainSection2} />
142132
</>
143133
);
144134
};
145135

136+
const Line = ({
137+
line,
138+
lineIndex,
139+
startingLineIndex,
140+
highlightedRegion,
141+
severity,
142+
message,
143+
messageChildren
144+
}: {
145+
line: string,
146+
lineIndex: number,
147+
startingLineIndex: number,
148+
highlightedRegion?: HighlightedRegion,
149+
severity?: ResultSeverity,
150+
message?: AnalysisMessage,
151+
messageChildren?: React.ReactNode,
152+
}) => {
153+
const shouldShowMessage = message &&
154+
severity &&
155+
highlightedRegion &&
156+
highlightedRegion.endLine == startingLineIndex + lineIndex;
157+
158+
return <div>
159+
<div style={{ display: 'flex' }} >
160+
<div style={{
161+
borderStyle: 'none',
162+
paddingTop: '0.01em',
163+
paddingLeft: '0.5em',
164+
paddingRight: '0.5em',
165+
paddingBottom: '0.2em'
166+
}}>
167+
{startingLineIndex + lineIndex}
168+
</div>
169+
<div style={{
170+
flexGrow: 1,
171+
borderStyle: 'none',
172+
paddingTop: '0.01em',
173+
paddingLeft: '1.5em',
174+
paddingRight: '0.5em',
175+
paddingBottom: '0.2em',
176+
wordBreak: 'break-word'
177+
}}>
178+
<Code
179+
line={line}
180+
lineNumber={startingLineIndex + lineIndex}
181+
highlightedRegion={highlightedRegion} />
182+
</div>
183+
</div>
184+
{shouldShowMessage &&
185+
<MessageContainer>
186+
<Message
187+
message={message}
188+
borderLeftColor={getSeverityColor(severity)}>
189+
{messageChildren}
190+
</Message>
191+
</MessageContainer>
192+
}
193+
</div>;
194+
};
195+
146196
const FileCodeSnippet = ({
147197
fileLink,
148198
codeSnippet,
@@ -173,6 +223,12 @@ const FileCodeSnippet = ({
173223
<TitleContainer>
174224
<VSCodeLink href={titleFileUri}>{fileLink.filePath}</VSCodeLink>
175225
</TitleContainer>
226+
{message && severity &&
227+
<Message
228+
message={message}
229+
borderLeftColor={getSeverityColor(severity)}>
230+
{messageChildren}
231+
</Message>}
176232
</Container>
177233
);
178234
}
@@ -186,40 +242,16 @@ const FileCodeSnippet = ({
186242
</TitleContainer>
187243
<CodeContainer>
188244
{code.map((line, index) => (
189-
<div key={index}>
190-
<div style={{ display: 'flex' }} >
191-
<div style={{
192-
borderStyle: 'none',
193-
paddingTop: '0.01em',
194-
paddingLeft: '0.5em',
195-
paddingRight: '0.5em',
196-
paddingBottom: '0.2em'
197-
}}>
198-
{startingLine + index}
199-
</div>
200-
<div style={{
201-
flexGrow: 1,
202-
borderStyle: 'none',
203-
paddingTop: '0.01em',
204-
paddingLeft: '1.5em',
205-
paddingRight: '0.5em',
206-
paddingBottom: '0.2em',
207-
wordBreak: 'break-word'
208-
}}>
209-
<CodeLine
210-
line={line}
211-
lineNumber={startingLine + index}
212-
highlightedRegion={highlightedRegion} />
213-
</div>
214-
</div>
215-
{message && severity && <Message
216-
message={message}
217-
currentLineNumber={startingLine + index}
218-
highlightedRegion={highlightedRegion}
219-
borderLeftColor={getSeverityColor(severity)}>
220-
{messageChildren}
221-
</Message>}
222-
</div>
245+
<Line
246+
key={`line-${index}`}
247+
line={line}
248+
lineIndex={index}
249+
startingLineIndex={startingLine}
250+
highlightedRegion={highlightedRegion}
251+
severity={severity}
252+
message={message}
253+
messageChildren={messageChildren}
254+
/>
223255
))}
224256
</CodeContainer>
225257
</Container>

0 commit comments

Comments
 (0)