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 { getSources } from '../../../api/components';
22 import Issue from '../../../components/issue/Issue';
23 import SecondaryIssue from '../../../components/issue/SecondaryIssue';
24 import getCoverageStatus from '../../../components/SourceViewer/helpers/getCoverageStatus';
25 import { locationsByLine } from '../../../components/SourceViewer/helpers/indexing';
26 import SourceViewerHeaderSlim from '../../../components/SourceViewer/SourceViewerHeaderSlim';
27 import { getBranchLikeQuery } from '../../../helpers/branch-like';
28 import { BranchLike } from '../../../types/branch-like';
29 import { isFile } from '../../../types/component';
42 } from '../../../types/types';
43 import SnippetViewer from './SnippetViewer';
53 branchLike: BranchLike | undefined;
54 duplications?: Duplication[];
55 duplicationsByLine?: { [line: number]: number[] };
56 highlightedLocationMessage: { index: number; text: string | undefined } | undefined;
57 isLastOccurenceOfPrimaryComponent: boolean;
59 issuePopup?: { issue: string; name: string };
60 issuesByLine: IssuesByLine;
61 lastSnippetGroup: boolean;
62 loadDuplications: (component: string, line: SourceLine) => void;
63 locations: FlowLocation[];
64 onIssueChange: (issue: TypeIssue) => void;
65 onIssueSelect: (issueKey: string) => void;
66 onIssuePopupToggle: (issue: string, popupName: string, open?: boolean) => void;
67 onLocationSelect: (index: number) => void;
68 renderDuplicationPopup: (
69 component: SourceViewerFile,
73 scroll?: (element: HTMLElement, offset: number) => void;
74 snippetGroup: SnippetGroup;
78 additionalLines: { [line: number]: SourceLine };
79 highlightedSymbols: string[];
84 export default class ComponentSourceSnippetGroupViewer extends React.PureComponent<Props, State> {
86 rootNodeRef = React.createRef<HTMLDivElement>();
89 highlightedSymbols: [],
96 this.createSnippetsFromProps();
99 componentWillUnmount() {
100 this.mounted = false;
103 createSnippetsFromProps() {
104 const { issue, snippetGroup } = this.props;
106 const snippets = createSnippets({
107 component: snippetGroup.component.key,
109 locations: snippetGroup.locations
112 this.setState({ snippets });
115 getNodes(index: number): { wrapper: HTMLElement; table: HTMLElement } | undefined {
116 const root = this.rootNodeRef.current;
120 const element = root.querySelector(`#snippet-wrapper-${index}`);
124 const wrapper = element.querySelector<HTMLElement>('.snippet');
128 const table = wrapper.firstChild as HTMLElement;
133 return { wrapper, table };
137 * Clean after animation
139 cleanDom(index: number) {
140 const nodes = this.getNodes(index);
146 const { wrapper, table } = nodes;
148 table.style.marginTop = '';
149 wrapper.style.maxHeight = '';
152 setMaxHeight(index: number, value?: number, up = false) {
153 const nodes = this.getNodes(index);
159 const { wrapper, table } = nodes;
161 const maxHeight = value !== undefined ? value : table.getBoundingClientRect().height;
164 const startHeight = wrapper.getBoundingClientRect().height;
165 table.style.transition = 'none';
166 table.style.marginTop = `${startHeight - maxHeight}px`;
170 table.style.transition = '';
171 table.style.marginTop = '0px';
172 wrapper.style.maxHeight = `${maxHeight + 20}px`;
175 wrapper.style.maxHeight = `${maxHeight + 20}px`;
179 expandBlock = (snippetIndex: number, direction: ExpandDirection): Promise<void> => {
180 const { branchLike, snippetGroup } = this.props;
181 const { key } = snippetGroup.component;
182 const { snippets } = this.state;
183 const snippet = snippets.find(s => s.index === snippetIndex);
185 return Promise.reject();
187 // Extend by EXPAND_BY_LINES and add buffer for merging snippets
188 const extension = EXPAND_BY_LINES + MERGE_DISTANCE - 1;
192 from: Math.max(1, snippet.start - extension),
193 to: snippet.start - 1
196 from: snippet.end + 1,
197 to: snippet.end + extension
202 ...getBranchLikeQuery(branchLike)
205 lines.reduce((lineMap: Dict<SourceLine>, line) => {
206 line.coverageStatus = getCoverageStatus(line);
207 lineMap[line.line] = line;
211 .then(newLinesMapped => this.animateBlockExpansion(snippetIndex, direction, newLinesMapped));
214 animateBlockExpansion(
215 snippetIndex: number,
216 direction: ExpandDirection,
217 newLinesMapped: Dict<SourceLine>
220 const { snippets } = this.state;
222 const newSnippets = expandSnippet({
228 const deletedSnippets = newSnippets.filter(s => s.toDelete);
230 // set max-height to current height for CSS transitions
231 deletedSnippets.forEach(s => this.setMaxHeight(s.index));
232 this.setMaxHeight(snippetIndex);
234 return new Promise(resolve => {
236 ({ additionalLines, snippets }) => {
237 const combinedLines = { ...additionalLines, ...newLinesMapped };
239 additionalLines: combinedLines,
244 // Set max-height 0 to trigger CSS transitions
245 deletedSnippets.forEach(s => {
246 this.setMaxHeight(s.index, 0);
248 this.setMaxHeight(snippetIndex, undefined, direction === 'up');
250 // Wait for transition to finish before updating dom
252 this.setState({ snippets: newSnippets.filter(s => !s.toDelete) }, resolve);
253 this.cleanDom(snippetIndex);
259 return Promise.resolve();
262 expandComponent = () => {
263 const { branchLike, snippetGroup } = this.props;
264 const { key } = snippetGroup.component;
266 this.setState({ loading: true });
268 getSources({ key, ...getBranchLikeQuery(branchLike) }).then(
271 this.setState(({ additionalLines }) => {
272 const combinedLines = { ...additionalLines, ...lines };
274 additionalLines: combinedLines,
276 snippets: [{ start: 0, end: lines[lines.length - 1].line, index: -1 }]
283 this.setState({ loading: false });
289 handleSymbolClick = (clickedSymbols: string[]) => {
290 this.setState(({ highlightedSymbols }) => {
291 const newHighlightedSymbols = clickedSymbols.filter(
292 symb => !highlightedSymbols.includes(symb)
294 return { highlightedSymbols: newHighlightedSymbols };
298 loadDuplications = (line: SourceLine) => {
299 this.props.loadDuplications(this.props.snippetGroup.component.key, line);
302 renderDuplicationPopup = (index: number, line: number) => {
303 return this.props.renderDuplicationPopup(this.props.snippetGroup.component, index, line);
306 renderIssuesList = (line: SourceLine) => {
308 isLastOccurenceOfPrimaryComponent,
315 issue.component === snippetGroup.component.key && issue.textRange !== undefined
316 ? locationsByLine([issue])
319 const isFlow = issue.secondaryLocations.length === 0;
320 const includeIssueLocation = isFlow ? isLastOccurenceOfPrimaryComponent : true;
321 const issuesForLine = issuesByLine[line.line] || [];
322 const issueLocations = includeIssueLocation ? locations[line.line] : [];
325 issuesForLine.length > 0 && (
326 <div className="issue-list">
327 {issuesForLine.map(issueToDisplay => {
328 if (issueToDisplay.key === issue.key && issueLocations && issueLocations.length) {
331 branchLike={branchLike}
332 displayWhyIsThisAnIssue={false}
333 issue={issueToDisplay}
334 key={issueToDisplay.key}
335 onChange={this.props.onIssueChange}
336 onPopupToggle={this.props.onIssuePopupToggle}
338 this.props.issuePopup && this.props.issuePopup.issue === issueToDisplay.key
339 ? this.props.issuePopup.name
342 selected={issue.key === issueToDisplay.key}
348 key={issueToDisplay.key}
349 issue={issueToDisplay}
350 onClick={this.props.onIssueSelect}
361 lastSnippetOfLastGroup,
366 lastSnippetOfLastGroup: boolean;
367 locationsByLine: { [line: number]: LinearIssueLocation[] };
368 snippet: SourceLine[];
372 renderAdditionalChildInLine={this.renderIssuesList}
373 component={this.props.snippetGroup.component}
374 duplications={this.props.duplications}
375 duplicationsByLine={this.props.duplicationsByLine}
376 expandBlock={this.expandBlock}
377 handleSymbolClick={this.handleSymbolClick}
378 highlightedLocationMessage={this.props.highlightedLocationMessage}
379 highlightedSymbols={this.state.highlightedSymbols}
381 issue={this.props.issue}
382 lastSnippetOfLastGroup={lastSnippetOfLastGroup}
383 loadDuplications={this.loadDuplications}
384 locations={this.props.locations}
385 locationsByLine={locationsByLine}
386 onLocationSelect={this.props.onLocationSelect}
387 renderDuplicationPopup={this.renderDuplicationPopup}
388 scroll={this.props.scroll}
397 isLastOccurenceOfPrimaryComponent,
403 const { additionalLines, loading, snippets } = this.state;
405 issue.component === snippetGroup.component.key && issue.textRange !== undefined
406 ? locationsByLine([issue])
410 snippets.length === 1 &&
411 snippetGroup.component.measures &&
412 snippets[0].end - snippets[0].start ===
413 parseInt(snippetGroup.component.measures.lines || '', 10);
415 const snippetLines = linesForSnippets(snippets, {
416 ...snippetGroup.sources,
420 const isFlow = issue.secondaryLocations.length === 0;
421 const includeIssueLocation = isFlow ? isLastOccurenceOfPrimaryComponent : true;
424 <div className="component-source-container" ref={this.rootNodeRef}>
425 <SourceViewerHeaderSlim
426 branchLike={branchLike}
427 expandable={!fullyShown && isFile(snippetGroup.component.q)}
429 onExpand={this.expandComponent}
430 sourceViewerFile={snippetGroup.component}
432 {issue.component === snippetGroup.component.key && issue.textRange === undefined && (
433 <div className="padded-top padded-left padded-right">
436 onChange={this.props.onIssueChange}
437 onPopupToggle={this.props.onIssuePopupToggle}
438 openPopup={issuePopup && issuePopup.issue === issue.key ? issuePopup.name : undefined}
443 {snippetLines.map((snippet, index) => (
444 <div id={`snippet-wrapper-${snippets[index].index}`} key={snippets[index].index}>
445 {this.renderSnippet({
447 index: snippets[index].index,
448 locationsByLine: includeIssueLocation ? locations : {},
449 lastSnippetOfLastGroup: lastSnippetGroup && index === snippets.length - 1