Skip to content

Commit 9c9c473

Browse files
committed
Add setting to change how severities are displayed
1 parent eef200f commit 9c9c473

File tree

3 files changed

+64
-23
lines changed

3 files changed

+64
-23
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Since CodeChecker-related paths vary greatly between systems, the following sett
8585
| --- | --- |
8686
| CodeChecker > Backend > Output folder <br> (default: `${workspaceFolder}/.codechecker`) | The output folder where the CodeChecker analysis files are stored. |
8787
| CodeChecker > Backend > Compilation database path <br> (default: *(empty)*) | Path to a custom compilation database, in case of a custom build system. The database setup dialog sets the path for the current workspace only. Leave blank to use the database in CodeChecker's output folder, or to use CodeChecker's autodetection for multi-root workspaces. |
88+
| CodeChecker > Editor > Custom bug severities <br> (default: `null`) | Control how a bug is displayed in the editor, depending on what its severity is. Bugs can be displayed as 'Error', 'Warning', 'Information' or 'Hint'. By default, everything except 'STYLE' severity is displayed as an error. Configured as an array, such as `{ "UNSPECIFIED": "Warning", "LOW": "Warning" }` |
8889
| CodeChecker > Editor > Show database dialog <br> (default: `on`) | Controls the dialog when opening a workspace without a compilation database. |
8990
| CodeChecker > Editor > Enable CodeLens <br> (default: `on`) | Enable CodeLens for displaying the reproduction path in the editor. |
9091
| CodeChecker > Executor > Enable notifications <br> (default: `on`) | Enable CodeChecker-related toast notifications. |

package.json

+8
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@
158158
"description": "Enable CodeLens for displaying the reproduction path",
159159
"default": true
160160
},
161+
"codechecker.editor.customBugSeverities": {
162+
"type": [
163+
"object",
164+
"null"
165+
],
166+
"description": "Control how a bug is displayed in the editor, depending on what its severity is. Bugs can be displayed as 'Error', 'Warning', 'Information' or 'Hint'. By default, everything except 'STYLE' severity is displayed as an error.",
167+
"default": null
168+
},
161169
"codechecker.executor.enableNotifications": {
162170
"type": "boolean",
163171
"description": "Enable pop-up notifications. Past messages are accessible via the sidebar menu regardless of this setting.",

src/editor/diagnostics.ts

+55-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ConfigurationChangeEvent,
23
Diagnostic,
34
DiagnosticCollection,
45
DiagnosticRelatedInformation,
@@ -16,6 +17,7 @@ import {
1617
} from 'vscode';
1718
import { ExtensionApi } from '../backend';
1819
import { DiagnosticReport } from '../backend/types';
20+
import { Editor } from './editor';
1921

2022
// Decoration type for highlighting report step positions.
2123
const reportStepDecorationType = window.createTextEditorDecorationType({
@@ -28,14 +30,6 @@ const reportStepDecorationType = window.createTextEditorDecorationType({
2830

2931
// TODO: implement api
3032

31-
// Get diagnostics severity for the given CodeChecker severity.
32-
function getDiagnosticSeverity(severity: string): DiagnosticSeverity {
33-
if (severity === 'STYLE') {
34-
return DiagnosticSeverity.Information;
35-
}
36-
return DiagnosticSeverity.Error;
37-
}
38-
3933
// Get diagnostic related information for the given report.
4034
// eslint-disable-next-line no-unused-vars
4135
function getRelatedInformation(report: DiagnosticReport): DiagnosticRelatedInformation[] {
@@ -71,26 +65,21 @@ function getRange(report: DiagnosticReport) {
7165
return new Range(startLine, report.column - 1, endLine, endColumn);
7266
}
7367

74-
function getDiagnostic(report: DiagnosticReport): Diagnostic {
75-
const severity = report.severity || 'UNSPECIFIED';
76-
77-
return {
78-
message: `[${severity}] ${report.message} [${report.checker_name}]`,
79-
range: getRange(report),
80-
// FIXME: for now it's not possible to attach custom commands to related informations. Later if it will be
81-
// available through the VSCode API we can show related information here.
82-
// relatedInformation: getRelatedInformation(report),
83-
severity: getDiagnosticSeverity(severity),
84-
source: 'CodeChecker',
85-
};
86-
}
87-
8868
export class DiagnosticRenderer {
8969
private _diagnosticCollection: DiagnosticCollection;
9070
private _lastUpdatedFiles: Uri[] = [];
9171
private _openedFiles: Uri[] = [];
72+
private customSeverities?: {[codecheckerSeverity: string]: string};
73+
private _severityMap: {[userSeverity: string]: DiagnosticSeverity} = {
74+
'error': DiagnosticSeverity.Error,
75+
'warning': DiagnosticSeverity.Warning,
76+
'information': DiagnosticSeverity.Information,
77+
'hint': DiagnosticSeverity.Hint
78+
};
9279

9380
constructor(ctx: ExtensionContext) {
81+
this.customSeverities = workspace.getConfiguration('codechecker.editor').get('customBugSeverities');
82+
9483
ctx.subscriptions.push(this._diagnosticCollection = languages.createDiagnosticCollection('codechecker'));
9584

9685
ExtensionApi.diagnostics.diagnosticsUpdated(this.onDiagnosticUpdated, this, ctx.subscriptions);
@@ -106,6 +95,8 @@ export class DiagnosticRenderer {
10695
}
10796
}
10897
});
98+
99+
workspace.onDidChangeConfiguration(this.onConfigChanged, this, ctx.subscriptions);
109100
}
110101

111102
onDiagnosticUpdated() {
@@ -120,6 +111,12 @@ export class DiagnosticRenderer {
120111
this.highlightActiveBugStep();
121112
}
122113

114+
onConfigChanged(event: ConfigurationChangeEvent) {
115+
if (event.affectsConfiguration('codechecker.editor')) {
116+
this.customSeverities = workspace.getConfiguration('codechecker.editor').get('customBugSeverities');
117+
}
118+
}
119+
123120
clearBugStepDecorations(editor: TextEditor) {
124121
editor.setDecorations(reportStepDecorationType, []);
125122
}
@@ -152,12 +149,47 @@ export class DiagnosticRenderer {
152149
editor.setDecorations(reportStepDecorationType, ranges);
153150
}
154151

152+
// Get diagnostics severity for the given CodeChecker severity.
153+
getDiagnosticSeverity(severity: string): DiagnosticSeverity {
154+
if (this.customSeverities && this.customSeverities[severity]) {
155+
const severityString = this.customSeverities[severity];
156+
157+
if (typeof severityString === 'string' && this._severityMap[severityString.toLowerCase()]) {
158+
return this._severityMap[severityString.toLowerCase()];
159+
} else {
160+
Editor.loggerPanel.window.appendLine(
161+
`>>> Invalid editor display type for CodeChecker severity ${severity}`
162+
);
163+
}
164+
}
165+
166+
if (severity === 'STYLE') {
167+
return DiagnosticSeverity.Information;
168+
}
169+
170+
return DiagnosticSeverity.Error;
171+
}
172+
173+
getDiagnostic(report: DiagnosticReport): Diagnostic {
174+
const severity = report.severity || 'UNSPECIFIED';
175+
176+
return {
177+
message: `[${severity}] ${report.message} [${report.checker_name}]`,
178+
range: getRange(report),
179+
// FIXME: for now it's not possible to attach custom commands to related informations. Later if it will be
180+
// available through the VSCode API we can show related information here.
181+
// relatedInformation: getRelatedInformation(report),
182+
severity: this.getDiagnosticSeverity(severity),
183+
source: 'CodeChecker',
184+
};
185+
}
186+
155187
// TODO: Implement CancellableToken
156188
updateAllDiagnostics(): void {
157189
const diagnosticMap: Map<string, Diagnostic[]> = new Map();
158190
const updateDiagnosticMap = (report: DiagnosticReport) => {
159191
const file = Uri.file(report.file.original_path);
160-
diagnosticMap.get(file.toString())?.push(getDiagnostic(report));
192+
diagnosticMap.get(file.toString())?.push(this.getDiagnostic(report));
161193
};
162194

163195
// Update "regular" errors in files

0 commit comments

Comments
 (0)