forked from Ericsson/CodecheckerVSCodePlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreports.ts
More file actions
382 lines (317 loc) · 13 KB
/
reports.ts
File metadata and controls
382 lines (317 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import { basename, relative } from 'path';
import {
Event,
EventEmitter,
ExtensionContext,
ThemeColor,
ThemeIcon,
TreeDataProvider,
TreeItem,
TreeItemCollapsibleState,
TreeItemLabel,
TreeView,
TreeViewSelectionChangeEvent,
Uri,
commands,
window,
workspace
} from 'vscode';
import { ExtensionApi } from '../../backend';
import { DiagnosticReport } from '../../backend/types';
export class ReportTreeItem extends TreeItem {
parent: ReportTreeItem | undefined;
constructor(
public readonly _id: string,
public readonly label: string | TreeItemLabel,
public readonly iconPath: ThemeIcon,
public readonly children?: ReportTreeItem[] | undefined
) {
super(label, children?.length ? TreeItemCollapsibleState.Collapsed : TreeItemCollapsibleState.None);
this._id = _id;
this.label = label;
this.iconPath = this.iconPath;
this.children = children;
// Set parent for children automatically.
this.children?.forEach(c => c.parent = this);
}
// This function can be used to set ID attribute of a tree item and all the children of it based on the parent id.
setId() {
this.id = `${this.parent?.id ?? 'root'}_${this._id}`;
this.children?.forEach(c => c.setId());
}
// It will expand the tree item (including all the parent items) if the tree item is in collapsed state.
expand() {
if (this.collapsibleState === TreeItemCollapsibleState.Collapsed) {
this.collapsibleState = TreeItemCollapsibleState.Expanded;
}
if (this.parent !== undefined) {
this.parent.expand();
}
}
traverse(cb: (item: ReportTreeItem) => void) {
cb(this);
this.children?.forEach(c => c.traverse(cb));
}
}
class TreeDiagnosticReport {
constructor(
public readonly entryId: number,
public readonly value: DiagnosticReport
) {
this.entryId = entryId;
this.value = value;
}
}
/* eslint-disable @typescript-eslint/naming-convention */
const severityOrder: { [key: string]: number } = {
'CRITICAL': 0,
'HIGH': 1,
'MEDIUM': 2,
'LOW': 3,
'STYLE': 4,
'UNSPECIFIED': 5,
};
/* eslint-enable @typescript-eslint/naming-convention */
export class ReportsView implements TreeDataProvider<ReportTreeItem> {
protected currentFile?: Uri;
protected currentEntryList?: DiagnosticReport[];
protected tree?: TreeView<ReportTreeItem>;
// Contains [fullpath => item] entries
private treeItems: Map<string, ReportTreeItem> = new Map();
private selectedTreeItems: ReportTreeItem[] = [];
constructor(ctx: ExtensionContext) {
ctx.subscriptions.push(this._onDidChangeTreeData = new EventEmitter());
window.onDidChangeActiveTextEditor(editor => {
// this event is called twice by vscode. Ignore deactivation of the old editor.
if (!editor) {
return;
}
this.refreshBugList();
}, this, ctx.subscriptions);
ExtensionApi.diagnostics.diagnosticsUpdated(() => {
// FIXME: fired twice when a file is opened freshly.
const selected = ExtensionApi.diagnostics.selectedEntry?.position.file;
for (const filePath of this.treeItems.keys()) {
if (filePath !== selected) {
this.treeItems.delete(filePath);
}
}
this.refreshBugList();
}, this, ctx.subscriptions);
ctx.subscriptions.push(this.tree = window.createTreeView(
'codechecker.views.reports',
{ treeDataProvider: this }
));
this.init();
}
protected init() {
this.currentFile = window.activeTextEditor?.document.uri;
this.tree?.onDidChangeSelection((item: TreeViewSelectionChangeEvent<ReportTreeItem>) => {
this.selectedTreeItems = item.selection;
});
}
private _onDidChangeTreeData: EventEmitter<void>;
public get onDidChangeTreeData(): Event<void> {
return this._onDidChangeTreeData.event;
}
refreshBugList() {
// Hide the report list when there's no metadata
const metadataExists = ExtensionApi.metadata.metadata !== undefined;
commands.executeCommand('setContext', 'codechecker.sidebar.showReports', metadataExists);
const activeUri = window.activeTextEditor?.document.uri;
if (!activeUri) {
return;
}
// Handle Code's Output tab
if (activeUri?.scheme === 'output') {
// Only clear currentFile if it was closed
if (window.visibleTextEditors.every((editor) => editor.document.uri.fsPath !== this.currentFile?.fsPath)) {
this.currentFile = undefined;
}
} else {
this.currentFile = activeUri;
}
// Clear tree on file close
if (this.currentFile === undefined) {
this.currentEntryList = [];
this._onDidChangeTreeData.fire();
return;
}
this.currentEntryList = ExtensionApi.diagnostics.getFileDiagnostics(this.currentFile);
this._onDidChangeTreeData.fire();
}
revealSelectedItems() {
const selectedIds = new Set(this.selectedTreeItems.map(item => item.id));
this.treeItems.forEach(root => root.traverse(item => {
if (selectedIds.has(item.id)) {
void this.tree?.reveal(item, { select: true, focus: true });
}
}));
}
readonly getTreeItem = (t: ReportTreeItem) => t;
readonly getParent = (t: ReportTreeItem) => t.parent;
getReportStepItems({ entryId, value: entry }: TreeDiagnosticReport): ReportTreeItem[] {
const selectedPosition = ExtensionApi.diagnostics.selectedEntry?.position;
const isActiveReport = selectedPosition?.idx === entryId;
const currentFile = this.currentFile;
const jumpToReportItem = new ReportTreeItem('jumpToReport', 'Jump to report', new ThemeIcon('debug-step-over'));
jumpToReportItem.command = {
title: 'jumpToReport',
command: 'codechecker.editor.jumpToReport',
arguments: [currentFile, entryId, true]
};
const checkerData = ExtensionApi.metadata.checkerData
?.get(`${entry.analyzer_name ?? ''}/${entry.checker_name ?? ''}`);
const docUrl = checkerData?.labels
.find((val) => val.startsWith('doc_url:'))
?.substring(8);
const goToDocsItems = [];
if (docUrl !== undefined) {
const goToDocsItem = new ReportTreeItem('openDocs', 'Go to checker documentation', new ThemeIcon('book'));
goToDocsItem.command = {
title: 'openDocs',
command: 'codechecker.editor.openDocs',
arguments: [docUrl]
};
goToDocsItems.push(goToDocsItem);
}
let toggleStepsItem = null;
if (isActiveReport) {
toggleStepsItem = new ReportTreeItem(
'hideReproductionSteps', 'Hide reproduction steps', new ThemeIcon('eye-closed'));
} else {
toggleStepsItem = new ReportTreeItem(
'showReproductionSteps', 'Show reproduction steps', new ThemeIcon('eye'));
}
toggleStepsItem.command = {
title: 'toggleSteps',
command: 'codechecker.editor.toggleSteps',
arguments: [currentFile, entryId]
};
let indentation = 0;
return [
jumpToReportItem,
...goToDocsItems,
toggleStepsItem,
...entry.bug_path_events.map((event, idx) => {
const filePath = event.file.original_path;
const fileName = basename(filePath);
const item = new ReportTreeItem(
`${idx + 1}`,
`${' '.repeat(indentation)} ${idx + 1}. ${fileName}:${event.line} - ${event.message}`,
new ThemeIcon('debug-breakpoint'));
item.tooltip = [
`Message: ${event.message}`,
`File path: ${filePath}`,
`Line: ${event.line}`,
`Column: ${event.column}`,
].join('\n');
item.command = {
title: 'jumpToStep',
command: 'codechecker.editor.jumpToStep',
arguments: [currentFile, entryId, idx, true]
};
if (event.message.includes('Calling')) {
indentation += 1;
} else if (event.message.includes('Returning from')) {
indentation -= 1;
}
return item;
})
];
}
getReportItems(entries: TreeDiagnosticReport[]): ReportTreeItem[] {
return entries.map(entry => {
/* eslint-disable @typescript-eslint/naming-convention */
const {
file, line, column, analyzer_name, checker_name, message, report_hash, severity, review_status,
bug_path_events
} = entry.value;
/* eslint-enable @typescript-eslint/naming-convention */
const item: ReportTreeItem = new ReportTreeItem(
`${entry.entryId}`,
`L${line} - ${checker_name} [${bug_path_events.length}]`,
new ThemeIcon('bug'),
this.getReportStepItems(entry));
item.tooltip = [
`Analyze name: ${analyzer_name}`,
`Checker name: ${checker_name}`,
`Severity: ${severity}`,
`File path: ${file.original_path}`,
`Line: ${line}`,
`Column: ${column}`,
`Message: ${message}`,
`Report hash: ${report_hash}`,
`Review status: ${review_status}`,
].join('\n');
return item;
});
}
getRootItemThemeIcon(severity: string): ThemeIcon {
const color = `severity.${severity.toLocaleLowerCase()}`;
return new ThemeIcon('warning', new ThemeColor(color));
}
sortSeverityItems([severityA]: [string, DiagnosticReport[]], [severityB]: [string, DiagnosticReport[]]) {
return severityOrder[severityA] - severityOrder[severityB];
}
// Get root level items.
getRootItems(): ReportTreeItem[] | undefined {
if (!this.currentEntryList?.length) {
return [new ReportTreeItem('noReportsFound', 'No reports found', new ThemeIcon('pass'))];
}
const severityItems: { [key: string]: TreeDiagnosticReport[] } = {};
for (const [idx, entry] of this.currentEntryList.entries()) {
const severity = entry.severity || 'UNSPECIFIED';
if (!(severity in severityItems)) {
severityItems[severity] = [];
}
severityItems[severity].push(new TreeDiagnosticReport(idx, entry));
}
const rootItems: ReportTreeItem[] = [];
rootItems.push(...Object.entries(severityItems)
.sort(([severityA]: [string, TreeDiagnosticReport[]], [severityB]: [string, TreeDiagnosticReport[]]) =>
severityOrder[severityA] - severityOrder[severityB])
.map(([severity, entries]) => {
const item: ReportTreeItem = new ReportTreeItem(
severity, severity, this.getRootItemThemeIcon(severity), this.getReportItems(entries));
return item;
}));
return rootItems;
}
getChildren(t?: ReportTreeItem): ReportTreeItem[] | undefined {
const currentFile = this.currentFile;
if (!currentFile) {
return [new ReportTreeItem('noFileSelected', 'No file selected', new ThemeIcon('warning'))];
}
if (t) {
return t.children;
}
// Refresh current file's reports
let filePath = currentFile.fsPath;
const workspaceFolder = workspace.workspaceFolders?.[0].uri.fsPath;
if (workspaceFolder) {
filePath = relative(workspaceFolder, filePath);
}
const item: ReportTreeItem = new ReportTreeItem(
currentFile.fsPath, filePath, new ThemeIcon('file-code'),
this.getRootItems());
item.setId();
item.collapsibleState = TreeItemCollapsibleState.Expanded;
this.treeItems.set(currentFile.fsPath, item);
if (!this.selectedTreeItems.length) {
item.children?.[0]?.children?.[0].expand();
}
this.revealSelectedItems();
const items = [];
if (ExtensionApi.diagnostics.selectedEntry) {
const item = new ReportTreeItem('hideSteps', 'Hide active reproduction steps', new ThemeIcon('eye-closed'));
item.command = {
title: 'toggleSteps',
command: 'codechecker.editor.toggleSteps',
arguments: [this.currentFile, -1, false]
};
items.push(item);
}
return [...items, ...this.treeItems.values()];
}
}