Skip to content

Commit

Permalink
Add setting to change how severities are displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
Discookie committed Apr 10, 2024
1 parent eef200f commit 9c9c473
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Since CodeChecker-related paths vary greatly between systems, the following sett
| --- | --- |
| CodeChecker > Backend > Output folder <br> (default: `${workspaceFolder}/.codechecker`) | The output folder where the CodeChecker analysis files are stored. |
| 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. |
| 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" }` |
| CodeChecker > Editor > Show database dialog <br> (default: `on`) | Controls the dialog when opening a workspace without a compilation database. |
| CodeChecker > Editor > Enable CodeLens <br> (default: `on`) | Enable CodeLens for displaying the reproduction path in the editor. |
| CodeChecker > Executor > Enable notifications <br> (default: `on`) | Enable CodeChecker-related toast notifications. |
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@
"description": "Enable CodeLens for displaying the reproduction path",
"default": true
},
"codechecker.editor.customBugSeverities": {
"type": [
"object",
"null"
],
"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.",
"default": null
},
"codechecker.executor.enableNotifications": {
"type": "boolean",
"description": "Enable pop-up notifications. Past messages are accessible via the sidebar menu regardless of this setting.",
Expand Down
78 changes: 55 additions & 23 deletions src/editor/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ConfigurationChangeEvent,
Diagnostic,
DiagnosticCollection,
DiagnosticRelatedInformation,
Expand All @@ -16,6 +17,7 @@ import {
} from 'vscode';
import { ExtensionApi } from '../backend';
import { DiagnosticReport } from '../backend/types';
import { Editor } from './editor';

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

// TODO: implement api

// Get diagnostics severity for the given CodeChecker severity.
function getDiagnosticSeverity(severity: string): DiagnosticSeverity {
if (severity === 'STYLE') {
return DiagnosticSeverity.Information;
}
return DiagnosticSeverity.Error;
}

// Get diagnostic related information for the given report.
// eslint-disable-next-line no-unused-vars
function getRelatedInformation(report: DiagnosticReport): DiagnosticRelatedInformation[] {
Expand Down Expand Up @@ -71,26 +65,21 @@ function getRange(report: DiagnosticReport) {
return new Range(startLine, report.column - 1, endLine, endColumn);
}

function getDiagnostic(report: DiagnosticReport): Diagnostic {
const severity = report.severity || 'UNSPECIFIED';

return {
message: `[${severity}] ${report.message} [${report.checker_name}]`,
range: getRange(report),
// FIXME: for now it's not possible to attach custom commands to related informations. Later if it will be
// available through the VSCode API we can show related information here.
// relatedInformation: getRelatedInformation(report),
severity: getDiagnosticSeverity(severity),
source: 'CodeChecker',
};
}

export class DiagnosticRenderer {
private _diagnosticCollection: DiagnosticCollection;
private _lastUpdatedFiles: Uri[] = [];
private _openedFiles: Uri[] = [];
private customSeverities?: {[codecheckerSeverity: string]: string};
private _severityMap: {[userSeverity: string]: DiagnosticSeverity} = {
'error': DiagnosticSeverity.Error,
'warning': DiagnosticSeverity.Warning,
'information': DiagnosticSeverity.Information,
'hint': DiagnosticSeverity.Hint
};

constructor(ctx: ExtensionContext) {
this.customSeverities = workspace.getConfiguration('codechecker.editor').get('customBugSeverities');

ctx.subscriptions.push(this._diagnosticCollection = languages.createDiagnosticCollection('codechecker'));

ExtensionApi.diagnostics.diagnosticsUpdated(this.onDiagnosticUpdated, this, ctx.subscriptions);
Expand All @@ -106,6 +95,8 @@ export class DiagnosticRenderer {
}
}
});

workspace.onDidChangeConfiguration(this.onConfigChanged, this, ctx.subscriptions);
}

onDiagnosticUpdated() {
Expand All @@ -120,6 +111,12 @@ export class DiagnosticRenderer {
this.highlightActiveBugStep();
}

onConfigChanged(event: ConfigurationChangeEvent) {
if (event.affectsConfiguration('codechecker.editor')) {
this.customSeverities = workspace.getConfiguration('codechecker.editor').get('customBugSeverities');
}
}

clearBugStepDecorations(editor: TextEditor) {
editor.setDecorations(reportStepDecorationType, []);
}
Expand Down Expand Up @@ -152,12 +149,47 @@ export class DiagnosticRenderer {
editor.setDecorations(reportStepDecorationType, ranges);
}

// Get diagnostics severity for the given CodeChecker severity.
getDiagnosticSeverity(severity: string): DiagnosticSeverity {
if (this.customSeverities && this.customSeverities[severity]) {
const severityString = this.customSeverities[severity];

if (typeof severityString === 'string' && this._severityMap[severityString.toLowerCase()]) {
return this._severityMap[severityString.toLowerCase()];
} else {
Editor.loggerPanel.window.appendLine(
`>>> Invalid editor display type for CodeChecker severity ${severity}`
);
}
}

if (severity === 'STYLE') {
return DiagnosticSeverity.Information;
}

return DiagnosticSeverity.Error;
}

getDiagnostic(report: DiagnosticReport): Diagnostic {
const severity = report.severity || 'UNSPECIFIED';

return {
message: `[${severity}] ${report.message} [${report.checker_name}]`,
range: getRange(report),
// FIXME: for now it's not possible to attach custom commands to related informations. Later if it will be
// available through the VSCode API we can show related information here.
// relatedInformation: getRelatedInformation(report),
severity: this.getDiagnosticSeverity(severity),
source: 'CodeChecker',
};
}

// TODO: Implement CancellableToken
updateAllDiagnostics(): void {
const diagnosticMap: Map<string, Diagnostic[]> = new Map();
const updateDiagnosticMap = (report: DiagnosticReport) => {
const file = Uri.file(report.file.original_path);
diagnosticMap.get(file.toString())?.push(getDiagnostic(report));
diagnosticMap.get(file.toString())?.push(this.getDiagnostic(report));
};

// Update "regular" errors in files
Expand Down

0 comments on commit 9c9c473

Please sign in to comment.