Skip to content

Commit 657446d

Browse files
authored
Merge pull request #358 from PowerShell/kapilmb/AddPSSARulesQuickPick
Create a choice list of PSSA rules
2 parents f835bdf + aeacf0f commit 657446d

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@
123123
"command": "PowerShell.ShowSessionMenu",
124124
"title": "Show Session Menu",
125125
"category": "PowerShell"
126+
},
127+
{
128+
"command": "PowerShell.SelectPSSARules",
129+
"title": "Select PSScriptAnalyzer Rules",
130+
"category": "PowerShell"
126131
}
127132
],
128133
"snippets": [

src/checkboxQuickPick.ts

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------*/
4+
5+
import vscode = require("vscode");
6+
import QuickPickItem = vscode.QuickPickItem;
7+
8+
export class CheckboxQuickPickItem {
9+
name: string;
10+
isSelected: boolean;
11+
}
12+
13+
export class CheckboxQuickPick {
14+
private static readonly confirm: string = "$(check)";
15+
private static readonly checkboxOn: string = "[ x ]";
16+
private static readonly checkboxOff: string = "[ ]";
17+
private static readonly confirmPlaceHolder: string = "Select 'Confirm' to confirm change; Press 'esc' key to cancel changes";
18+
19+
public static show(
20+
checkboxQuickPickItems: CheckboxQuickPickItem[],
21+
callback: (items: CheckboxQuickPickItem[]) => void): void {
22+
CheckboxQuickPick.showInner(checkboxQuickPickItems.slice(), callback);
23+
}
24+
25+
private static showInner(
26+
items: CheckboxQuickPickItem[],
27+
callback: (items: CheckboxQuickPickItem[]) => void): void {
28+
vscode.window.showQuickPick(
29+
CheckboxQuickPick.getQuickPickItems(items),
30+
{
31+
ignoreFocusOut: true,
32+
matchOnDescription: true,
33+
placeHolder: CheckboxQuickPick.confirmPlaceHolder
34+
}).then((selection) => {
35+
if (!selection) {
36+
return;
37+
}
38+
39+
if (selection.label === CheckboxQuickPick.confirm) {
40+
callback(items);
41+
return;
42+
}
43+
44+
let index: number = CheckboxQuickPick.getRuleIndex(items, selection.description);
45+
CheckboxQuickPick.toggleSelection(items[index]);
46+
CheckboxQuickPick.showInner(items, callback);
47+
});
48+
}
49+
50+
private static getRuleIndex(items: CheckboxQuickPickItem[], itemLabel: string): number {
51+
return items.findIndex(item => item.name === itemLabel);
52+
}
53+
54+
private static getQuickPickItems(items: CheckboxQuickPickItem[]): QuickPickItem[] {
55+
let quickPickItems: QuickPickItem[] = [];
56+
quickPickItems.push({ label: CheckboxQuickPick.confirm, description: "Confirm" });
57+
items.forEach(item =>
58+
quickPickItems.push({
59+
label: CheckboxQuickPick.convertToCheckBox(item.isSelected), description: item.name
60+
}));
61+
return quickPickItems;
62+
}
63+
64+
private static toggleSelection(item: CheckboxQuickPickItem): void {
65+
item.isSelected = !item.isSelected;
66+
}
67+
68+
private static convertToCheckBox(state: boolean): string {
69+
if (state) {
70+
return CheckboxQuickPick.checkboxOn;
71+
}
72+
else {
73+
return CheckboxQuickPick.checkboxOff;
74+
}
75+
}
76+
}

src/features/SelectPSSARules.ts

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------*/
4+
5+
import vscode = require("vscode");
6+
import { IFeature } from "../feature";
7+
import { LanguageClient, RequestType } from "vscode-languageclient";
8+
import { CheckboxQuickPickItem, CheckboxQuickPick } from "../checkboxQuickPick";
9+
10+
export namespace GetPSSARulesRequest {
11+
export const type: RequestType<any, any, void> = { get method(): string { return "powerShell/getPSSARules"; } };
12+
}
13+
14+
export namespace SetPSSARulesRequest {
15+
export const type: RequestType<any, any, void> = { get method(): string { return "powerShell/setPSSARules"; } };
16+
}
17+
18+
class RuleInfo {
19+
name: string;
20+
isEnabled: boolean;
21+
}
22+
23+
class SetPSSARulesRequestParams {
24+
filepath: string;
25+
ruleInfos: RuleInfo[];
26+
}
27+
28+
export class SelectPSSARulesFeature implements IFeature {
29+
30+
private command: vscode.Disposable;
31+
private languageClient: LanguageClient;
32+
33+
constructor() {
34+
this.command = vscode.commands.registerCommand("PowerShell.SelectPSSARules", () => {
35+
if (this.languageClient === undefined) {
36+
return;
37+
}
38+
39+
this.languageClient.sendRequest(GetPSSARulesRequest.type, null).then((returnedRules) => {
40+
if (returnedRules == null) {
41+
vscode.window.showWarningMessage(
42+
"PowerShell extension uses PSScriptAnalyzer settings file - Cannot update rules.");
43+
return;
44+
}
45+
let options: CheckboxQuickPickItem[] = returnedRules.map(function (rule: RuleInfo): CheckboxQuickPickItem {
46+
return { name: rule.name, isSelected: rule.isEnabled };
47+
});
48+
CheckboxQuickPick.show(options, (updatedOptions) => {
49+
let filepath: string = vscode.window.activeTextEditor.document.uri.fsPath;
50+
let ruleInfos: RuleInfo[] = updatedOptions.map(
51+
function (option: CheckboxQuickPickItem): RuleInfo {
52+
return { name: option.name, isEnabled: option.isSelected };
53+
});
54+
let requestParams: SetPSSARulesRequestParams = {filepath, ruleInfos};
55+
this.languageClient.sendRequest(SetPSSARulesRequest.type, requestParams);
56+
});
57+
});
58+
});
59+
}
60+
61+
public setLanguageClient(languageclient: LanguageClient): void {
62+
this.languageClient = languageclient;
63+
}
64+
65+
public dispose(): void {
66+
this.command.dispose();
67+
}
68+
}

src/main.ts

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ExpandAliasFeature } from './features/ExpandAlias';
1515
import { ShowHelpFeature } from './features/ShowOnlineHelp';
1616
import { FindModuleFeature } from './features/PowerShellFindModule';
1717
import { ExtensionCommandsFeature } from './features/ExtensionCommands';
18+
import { SelectPSSARulesFeature } from './features/SelectPSSARules';
1819
import { CodeActionsFeature } from './features/CodeActions';
1920

2021
// NOTE: We will need to find a better way to deal with the required
@@ -91,6 +92,7 @@ export function activate(context: vscode.ExtensionContext): void {
9192
new ShowHelpFeature(),
9293
new FindModuleFeature(),
9394
new ExtensionCommandsFeature(),
95+
new SelectPSSARulesFeature(),
9496
new CodeActionsFeature()
9597
];
9698

0 commit comments

Comments
 (0)