diff --git a/README.md b/README.md index d938841..5a28a11 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ Since CodeChecker-related paths vary greatly between systems, the following sett | --- | --- | | CodeChecker > Backend > Output folder
(default: `${workspaceFolder}/.codechecker`) | The output folder where the CodeChecker analysis files are stored. | | CodeChecker > Backend > Compilation database path
(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
(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
(default: `on`) | Controls the dialog when opening a workspace without a compilation database. | | CodeChecker > Editor > Enable CodeLens
(default: `on`) | Enable CodeLens for displaying the reproduction path in the editor. | | CodeChecker > Executor > Enable notifications
(default: `on`) | Enable CodeChecker-related toast notifications. | diff --git a/package.json b/package.json index f21bcbd..20abca7 100644 --- a/package.json +++ b/package.json @@ -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.", diff --git a/src/editor/diagnostics.ts b/src/editor/diagnostics.ts index 571e52a..0bf59c0 100644 --- a/src/editor/diagnostics.ts +++ b/src/editor/diagnostics.ts @@ -1,4 +1,5 @@ import { + ConfigurationChangeEvent, Diagnostic, DiagnosticCollection, DiagnosticRelatedInformation, @@ -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({ @@ -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[] { @@ -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); @@ -106,6 +95,8 @@ export class DiagnosticRenderer { } } }); + + workspace.onDidChangeConfiguration(this.onConfigChanged, this, ctx.subscriptions); } onDiagnosticUpdated() { @@ -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, []); } @@ -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 = 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