3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 import * as React from 'react';
21 import { FormattedMessage } from 'react-intl';
22 import { getSources } from '../../../api/components';
23 import IssueMessageBox from '../../../components/issue/IssueMessageBox';
24 import getCoverageStatus from '../../../components/SourceViewer/helpers/getCoverageStatus';
25 import { locationsByLine } from '../../../components/SourceViewer/helpers/indexing';
26 import { Alert } from '../../../components/ui/Alert';
27 import { getBranchLikeQuery } from '../../../helpers/branch-like';
28 import { translate } from '../../../helpers/l10n';
29 import { BranchLike } from '../../../types/branch-like';
30 import { ComponentQualifier, isFile } from '../../../types/component';
31 import { IssueStatus } from '../../../types/issues';
43 } from '../../../types/types';
44 import { IssueSourceViewerScrollContext } from '../components/IssueSourceViewerScrollContext';
45 import IssueSourceViewerHeader from './IssueSourceViewerHeader';
46 import SnippetViewer from './SnippetViewer';
57 branchLike: BranchLike | undefined;
58 duplications?: Duplication[];
59 duplicationsByLine?: { [line: number]: number[] };
60 highlightedLocationMessage: { index: number; text: string | undefined } | undefined;
61 isLastOccurenceOfPrimaryComponent: boolean;
63 issuesByLine: IssuesByLine;
64 lastSnippetGroup: boolean;
65 loadDuplications: (component: string, line: SourceLine) => void;
66 locations: FlowLocation[];
67 onIssueSelect: (issueKey: string) => void;
68 onLocationSelect: (index: number) => void;
69 renderDuplicationPopup: (
70 component: SourceViewerFile,
74 snippetGroup: SnippetGroup;
78 additionalLines: { [line: number]: SourceLine };
79 highlightedSymbols: string[];
84 export default class ComponentSourceSnippetGroupViewer extends React.PureComponent<Props, State> {
87 constructor(props: Props) {
91 highlightedSymbols: [],
99 this.createSnippetsFromProps();
102 componentWillUnmount() {
103 this.mounted = false;
106 createSnippetsFromProps() {
107 const { issue, snippetGroup } = this.props;
109 const snippets = createSnippets({
110 component: snippetGroup.component.key,
113 snippetGroup.locations.length === 0
114 ? [getPrimaryLocation(issue)]
115 : [getPrimaryLocation(issue), ...snippetGroup.locations],
118 this.setState({ snippets });
121 expandBlock = (snippetIndex: number, direction: ExpandDirection): Promise<void> => {
122 const { branchLike, snippetGroup } = this.props;
123 const { key } = snippetGroup.component;
124 const { snippets } = this.state;
125 const snippet = snippets.find((s) => s.index === snippetIndex);
127 return Promise.reject();
129 // Extend by EXPAND_BY_LINES and add buffer for merging snippets
130 const extension = EXPAND_BY_LINES + MERGE_DISTANCE - 1;
134 from: Math.max(1, snippet.start - extension),
135 to: snippet.start - 1,
138 from: snippet.end + 1,
139 to: snippet.end + extension,
144 ...getBranchLikeQuery(branchLike),
147 lines.reduce((lineMap: Dict<SourceLine>, line) => {
148 line.coverageStatus = getCoverageStatus(line);
149 lineMap[line.line] = line;
153 .then((newLinesMapped) => {
154 const newSnippets = expandSnippet({
160 this.setState(({ additionalLines }) => {
161 const combinedLines = { ...additionalLines, ...newLinesMapped };
163 additionalLines: combinedLines,
164 snippets: newSnippets.filter((s) => !s.toDelete),
170 expandComponent = () => {
171 const { branchLike, snippetGroup } = this.props;
172 const { key } = snippetGroup.component;
174 this.setState({ loading: true });
176 getSources({ key, ...getBranchLikeQuery(branchLike) }).then(
179 this.setState(({ additionalLines }) => {
180 const combinedLines = { ...additionalLines, ...lines };
182 additionalLines: combinedLines,
184 snippets: [{ start: 0, end: lines[lines.length - 1].line, index: -1 }],
191 this.setState({ loading: false });
197 handleSymbolClick = (clickedSymbols: string[]) => {
198 this.setState(({ highlightedSymbols }) => {
199 const newHighlightedSymbols = clickedSymbols.filter(
200 (symb) => !highlightedSymbols.includes(symb)
202 return { highlightedSymbols: newHighlightedSymbols };
206 loadDuplications = (line: SourceLine) => {
207 this.props.loadDuplications(this.props.snippetGroup.component.key, line);
210 renderDuplicationPopup = (index: number, line: number) => {
211 return this.props.renderDuplicationPopup(this.props.snippetGroup.component, index, line);
214 renderIssuesList = (line: SourceLine) => {
215 const { isLastOccurenceOfPrimaryComponent, issue, issuesByLine, snippetGroup } = this.props;
217 issue.component === snippetGroup.component.key && issue.textRange !== undefined
218 ? locationsByLine([issue])
221 const isFlow = issue.secondaryLocations.length === 0;
222 const includeIssueLocation = isFlow ? isLastOccurenceOfPrimaryComponent : true;
223 const issuesForLine = issuesByLine[line.line] || [];
224 const issueLocations = includeIssueLocation ? locations[line.line] : [];
227 issuesForLine.length > 0 && (
229 {issuesForLine.map((issueToDisplay) => {
230 const isSelectedIssue = issueToDisplay.key === issue.key;
232 <IssueSourceViewerScrollContext.Consumer key={issueToDisplay.key}>
235 selected={!!(isSelectedIssue && issueLocations.length > 0)}
236 issue={issueToDisplay}
237 onClick={this.props.onIssueSelect}
238 ref={isSelectedIssue ? ctx?.registerPrimaryLocationRef : undefined}
241 </IssueSourceViewerScrollContext.Consumer>
250 const { branchLike, isLastOccurenceOfPrimaryComponent, issue, lastSnippetGroup, snippetGroup } =
252 const { additionalLines, loading, snippets } = this.state;
254 issue.component === snippetGroup.component.key && issue.textRange !== undefined
255 ? locationsByLine([issue])
259 snippets.length === 1 &&
260 snippetGroup.component.measures &&
261 snippets[0].end - snippets[0].start ===
262 parseInt(snippetGroup.component.measures.lines || '', 10);
264 const snippetLines = linesForSnippets(snippets, {
265 ...snippetGroup.sources,
269 const isFlow = issue.secondaryLocations.length === 0;
270 const includeIssueLocation = isFlow ? isLastOccurenceOfPrimaryComponent : true;
272 const issueIsClosed = issue.status === IssueStatus.Closed;
273 const issueIsFileLevel = issue.componentQualifier === ComponentQualifier.File;
274 const closedIssueMessageKey = issueIsFileLevel
275 ? 'issue.closed.file_level'
276 : 'issue.closed.project_level';
281 <Alert variant="success">
283 id={closedIssueMessageKey}
284 defaultMessage={translate(closedIssueMessageKey)}
288 {translate('issue.status', issue.status)} (
289 {issue.resolution ? translate('issue.resolution', issue.resolution) : '-'})
297 <IssueSourceViewerHeader
298 branchLike={branchLike}
299 className={issueIsClosed && !issueIsFileLevel ? 'null-spacer-bottom' : ''}
300 expandable={!fullyShown && isFile(snippetGroup.component.q)}
302 onExpand={this.expandComponent}
303 sourceViewerFile={snippetGroup.component}
306 {issue.component === snippetGroup.component.key &&
307 issue.textRange === undefined &&
309 <IssueSourceViewerScrollContext.Consumer>
314 onClick={this.props.onIssueSelect}
315 ref={ctx?.registerPrimaryLocationRef}
318 </IssueSourceViewerScrollContext.Consumer>
321 {snippetLines.map((snippet, index) => (
323 key={snippets[index].index}
324 renderAdditionalChildInLine={this.renderIssuesList}
325 component={this.props.snippetGroup.component}
326 duplications={this.props.duplications}
327 duplicationsByLine={this.props.duplicationsByLine}
328 expandBlock={this.expandBlock}
329 handleSymbolClick={this.handleSymbolClick}
330 highlightedLocationMessage={this.props.highlightedLocationMessage}
331 highlightedSymbols={this.state.highlightedSymbols}
332 index={snippets[index].index}
333 issue={this.props.issue}
334 lastSnippetOfLastGroup={lastSnippetGroup && index === snippets.length - 1}
335 loadDuplications={this.loadDuplications}
336 locations={this.props.locations}
337 locationsByLine={includeIssueLocation ? locations : {}}
338 onLocationSelect={this.props.onLocationSelect}
339 renderDuplicationPopup={this.renderDuplicationPopup}