-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathSelectPSSARules.ts
70 lines (58 loc) · 2.81 KB
/
SelectPSSARules.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import vscode = require("vscode");
import { LanguageClient, RequestType } from "vscode-languageclient";
import { ICheckboxQuickPickItem, showCheckboxQuickPick } from "../controls/checkboxQuickPick";
import { IFeature } from "../feature";
import { Logger } from "../logging";
export const GetPSSARulesRequestType = new RequestType<any, any, void, void>("powerShell/getPSSARules");
export const SetPSSARulesRequestType = new RequestType<any, any, void, void>("powerShell/setPSSARules");
class RuleInfo {
public name: string;
public isEnabled: boolean;
}
export class SelectPSSARulesFeature implements IFeature {
private command: vscode.Disposable;
private languageClient: LanguageClient;
constructor(private log: Logger) {
this.command = vscode.commands.registerCommand("PowerShell.SelectPSSARules", () => {
if (this.languageClient === undefined) {
this.log.writeAndShowError(`<${SelectPSSARulesFeature.name}>: ` +
"Unable to instantiate; language client undefined.");
return;
}
this.languageClient.sendRequest(GetPSSARulesRequestType, null).then((returnedRules) => {
if (returnedRules == null) {
vscode.window.showWarningMessage(
"PowerShell extension uses PSScriptAnalyzer settings file - Cannot update rules.");
return;
}
const options: ICheckboxQuickPickItem[] =
returnedRules.map((rule: RuleInfo): ICheckboxQuickPickItem => {
return { label: rule.name, isSelected: rule.isEnabled };
});
showCheckboxQuickPick(options)
.then((updatedOptions: ICheckboxQuickPickItem[]) => {
if (updatedOptions === undefined) {
return;
}
this.languageClient.sendRequest(
SetPSSARulesRequestType,
{
filepath: vscode.window.activeTextEditor.document.uri.toString(),
ruleInfos: updatedOptions.map((option: ICheckboxQuickPickItem): RuleInfo => {
return { name: option.label, isEnabled: option.isSelected };
}),
});
});
});
});
}
public dispose(): void {
this.command.dispose();
}
public setLanguageClient(languageclient: LanguageClient): void {
this.languageClient = languageclient;
}
}