Skip to content

Commit 17e928c

Browse files
committed
extracted codeActionProvider class to sepparate util file
1 parent 524e346 commit 17e928c

3 files changed

Lines changed: 63 additions & 61 deletions

File tree

src/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import * as crypto from 'crypto';
66
import { documentationLinkMap, getPremiumCertLink } from './util/documentation';
77
import { runCommand } from './util/scripts';
88
import { looksLikePath, resolvePath, findWorkspaceRoot } from './util/path';
9-
import { CodeActionProvider, diagnosticsUnion } from './util/diagnostics';
9+
import { diagnosticsUnion } from './util/diagnostics';
10+
import { CodeActionProvider } from './util/codeActions';
1011

1112
// To keep track of document changes we save hashed versions of their content to this record
1213
let documentHashMemory : Record<string, string> = {};

src/util/codeActions.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as vscode from 'vscode';
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+
}

src/util/diagnostics.ts

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,5 @@
11
import * as vscode from 'vscode';
22

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-
633
export function diagnosticsUnion(diagnosticsA : vscode.Diagnostic[], diagnosticB : vscode.Diagnostic[]) : vscode.Diagnostic[] {
644
const diagnosticsUnion = new Array<vscode.Diagnostic>;
655
// Add all elements from diagnosticsA to result array

0 commit comments

Comments
 (0)