Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting to change how severities are displayed #141

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@
"description": "Enable CodeLens for displaying the reproduction path",
"default": true
},
"codechecker.editor.customBugSeverities": {
Copy link
Collaborator

@MiklosMagyari MiklosMagyari Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be more user friendly to use an enum setting:

"codechecker.editor.customBugSeverities": {
      "type": "string",
      "default": "Error",
      "enum": [
        "Error",
        "Warning",
        "Information",
        "Hint"
      ],
      "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."
    }

so this setting would be a selection:

image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, and turns out vscode supports enum selection within objects, which is nice to use.
Can't rename the column headers, but it doesn't really matter too much.

image

"type": "object",
"description": "Control how a bug is displayed in the editor, depending on what its severity is.",
"additionalProperties": {
"type": "string",
"enum": [
"Error",
"Warning",
"Information",
"Hint"
]
},
"default": {
"HIGH": "Error",
"MEDIUM": "Error",
"LOW": "Error",
"UNSPECIFIED": "Error",
"STYLE": "Warning"
}
},
"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
Loading