Skip to content

Commit

Permalink
Merge pull request #358 from PowerShell/kapilmb/AddPSSARulesQuickPick
Browse files Browse the repository at this point in the history
Create a choice list of PSSA rules
  • Loading branch information
daviwil authored Dec 9, 2016
2 parents f835bdf + aeacf0f commit 657446d
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
"command": "PowerShell.ShowSessionMenu",
"title": "Show Session Menu",
"category": "PowerShell"
},
{
"command": "PowerShell.SelectPSSARules",
"title": "Select PSScriptAnalyzer Rules",
"category": "PowerShell"
}
],
"snippets": [
Expand Down
76 changes: 76 additions & 0 deletions src/checkboxQuickPick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import vscode = require("vscode");
import QuickPickItem = vscode.QuickPickItem;

export class CheckboxQuickPickItem {
name: string;
isSelected: boolean;
}

export class CheckboxQuickPick {
private static readonly confirm: string = "$(check)";
private static readonly checkboxOn: string = "[ x ]";
private static readonly checkboxOff: string = "[ ]";
private static readonly confirmPlaceHolder: string = "Select 'Confirm' to confirm change; Press 'esc' key to cancel changes";

public static show(
checkboxQuickPickItems: CheckboxQuickPickItem[],
callback: (items: CheckboxQuickPickItem[]) => void): void {
CheckboxQuickPick.showInner(checkboxQuickPickItems.slice(), callback);
}

private static showInner(
items: CheckboxQuickPickItem[],
callback: (items: CheckboxQuickPickItem[]) => void): void {
vscode.window.showQuickPick(
CheckboxQuickPick.getQuickPickItems(items),
{
ignoreFocusOut: true,
matchOnDescription: true,
placeHolder: CheckboxQuickPick.confirmPlaceHolder
}).then((selection) => {
if (!selection) {
return;
}

if (selection.label === CheckboxQuickPick.confirm) {
callback(items);
return;
}

let index: number = CheckboxQuickPick.getRuleIndex(items, selection.description);
CheckboxQuickPick.toggleSelection(items[index]);
CheckboxQuickPick.showInner(items, callback);
});
}

private static getRuleIndex(items: CheckboxQuickPickItem[], itemLabel: string): number {
return items.findIndex(item => item.name === itemLabel);
}

private static getQuickPickItems(items: CheckboxQuickPickItem[]): QuickPickItem[] {
let quickPickItems: QuickPickItem[] = [];
quickPickItems.push({ label: CheckboxQuickPick.confirm, description: "Confirm" });
items.forEach(item =>
quickPickItems.push({
label: CheckboxQuickPick.convertToCheckBox(item.isSelected), description: item.name
}));
return quickPickItems;
}

private static toggleSelection(item: CheckboxQuickPickItem): void {
item.isSelected = !item.isSelected;
}

private static convertToCheckBox(state: boolean): string {
if (state) {
return CheckboxQuickPick.checkboxOn;
}
else {
return CheckboxQuickPick.checkboxOff;
}
}
}
68 changes: 68 additions & 0 deletions src/features/SelectPSSARules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import vscode = require("vscode");
import { IFeature } from "../feature";
import { LanguageClient, RequestType } from "vscode-languageclient";
import { CheckboxQuickPickItem, CheckboxQuickPick } from "../checkboxQuickPick";

export namespace GetPSSARulesRequest {
export const type: RequestType<any, any, void> = { get method(): string { return "powerShell/getPSSARules"; } };
}

export namespace SetPSSARulesRequest {
export const type: RequestType<any, any, void> = { get method(): string { return "powerShell/setPSSARules"; } };
}

class RuleInfo {
name: string;
isEnabled: boolean;
}

class SetPSSARulesRequestParams {
filepath: string;
ruleInfos: RuleInfo[];
}

export class SelectPSSARulesFeature implements IFeature {

private command: vscode.Disposable;
private languageClient: LanguageClient;

constructor() {
this.command = vscode.commands.registerCommand("PowerShell.SelectPSSARules", () => {
if (this.languageClient === undefined) {
return;
}

this.languageClient.sendRequest(GetPSSARulesRequest.type, null).then((returnedRules) => {
if (returnedRules == null) {
vscode.window.showWarningMessage(
"PowerShell extension uses PSScriptAnalyzer settings file - Cannot update rules.");
return;
}
let options: CheckboxQuickPickItem[] = returnedRules.map(function (rule: RuleInfo): CheckboxQuickPickItem {
return { name: rule.name, isSelected: rule.isEnabled };
});
CheckboxQuickPick.show(options, (updatedOptions) => {
let filepath: string = vscode.window.activeTextEditor.document.uri.fsPath;
let ruleInfos: RuleInfo[] = updatedOptions.map(
function (option: CheckboxQuickPickItem): RuleInfo {
return { name: option.name, isEnabled: option.isSelected };
});
let requestParams: SetPSSARulesRequestParams = {filepath, ruleInfos};
this.languageClient.sendRequest(SetPSSARulesRequest.type, requestParams);
});
});
});
}

public setLanguageClient(languageclient: LanguageClient): void {
this.languageClient = languageclient;
}

public dispose(): void {
this.command.dispose();
}
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ExpandAliasFeature } from './features/ExpandAlias';
import { ShowHelpFeature } from './features/ShowOnlineHelp';
import { FindModuleFeature } from './features/PowerShellFindModule';
import { ExtensionCommandsFeature } from './features/ExtensionCommands';
import { SelectPSSARulesFeature } from './features/SelectPSSARules';
import { CodeActionsFeature } from './features/CodeActions';

// NOTE: We will need to find a better way to deal with the required
Expand Down Expand Up @@ -91,6 +92,7 @@ export function activate(context: vscode.ExtensionContext): void {
new ShowHelpFeature(),
new FindModuleFeature(),
new ExtensionCommandsFeature(),
new SelectPSSARulesFeature(),
new CodeActionsFeature()
];

Expand Down

0 comments on commit 657446d

Please sign in to comment.