|
1 | 1 | import * as vscode from 'vscode'; |
2 | 2 |
|
| 3 | +export class CodeActionProvider implements vscode.CodeActionProvider { |
| 4 | + provideCodeActions( |
| 5 | + document: vscode.TextDocument, |
| 6 | + range: vscode.Range, |
| 7 | + context: vscode.CodeActionContext, |
| 8 | + token: vscode.CancellationToken |
| 9 | + ): vscode.CodeAction[] { |
| 10 | + |
| 11 | + const actions: vscode.CodeAction[] = []; |
| 12 | + |
| 13 | + for (const diagnostic of context.diagnostics) { |
| 14 | + var diagnosticCode = diagnostic.code; |
| 15 | + if (typeof(diagnosticCode) === "object" && typeof(diagnosticCode) !== null) { |
| 16 | + diagnosticCode = diagnosticCode.value; |
| 17 | + } |
| 18 | + |
| 19 | + // Set up one action for suppressing the specific warning on the line targeted by the diagnostic |
| 20 | + const suppressAction = new vscode.CodeAction( |
| 21 | + `Suppress warning for ${diagnosticCode} here`, |
| 22 | + vscode.CodeActionKind.QuickFix |
| 23 | + ); |
| 24 | + |
| 25 | + // Copy indentation from line affected by diagnostic |
| 26 | + const lineText = document.lineAt(diagnostic.range.start.line).text; |
| 27 | + const indent = lineText.match(/^\s*/)?.[0] ?? ""; |
| 28 | + |
| 29 | + // Insert suppression comment above affected line |
| 30 | + const suppressLineEdit = new vscode.WorkspaceEdit(); |
| 31 | + suppressLineEdit.insert( |
| 32 | + document.uri, |
| 33 | + new vscode.Position( |
| 34 | + diagnostic.range.start.line, |
| 35 | + 0, |
| 36 | + ), |
| 37 | + `${indent}// cppcheck-suppress ${diagnosticCode}\n` |
| 38 | + ); |
| 39 | + suppressAction.edit = suppressLineEdit; |
| 40 | + suppressAction.diagnostics = [diagnostic]; |
| 41 | + actions.push(suppressAction); |
| 42 | + |
| 43 | + // Set up one action for suppressing warning of a given type universally |
| 44 | + const suppressTypeAction = new vscode.CodeAction( |
| 45 | + `Suppress warning type ${diagnosticCode} universally`, |
| 46 | + vscode.CodeActionKind.QuickFix |
| 47 | + ); |
| 48 | + |
| 49 | + suppressTypeAction.command = { |
| 50 | + command: "cppcheck-official.suppressWarningAll", |
| 51 | + title: "Suppress warning here", |
| 52 | + arguments: [diagnostic] |
| 53 | + }; |
| 54 | + |
| 55 | + suppressTypeAction.diagnostics = [diagnostic]; |
| 56 | + actions.push(suppressTypeAction); |
| 57 | + } |
| 58 | + |
| 59 | + return actions; |
| 60 | + } |
| 61 | +} |
| 62 | + |
3 | 63 | export function diagnosticsUnion(diagnosticsA : vscode.Diagnostic[], diagnosticB : vscode.Diagnostic[]) : vscode.Diagnostic[] { |
4 | 64 | const diagnosticsUnion = new Array<vscode.Diagnostic>; |
5 | 65 | // Add all elements from diagnosticsA to result array |
|
0 commit comments