Skip to content

Create a choice list of PSSA rules #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Dec 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more thing here: I think your request for getting the PSSA rules needs to be done in setLanguageClient. Since this method is called when the session is started, restarted, or switched to a new runtime, we'll need to re-query this list to make sure the list is up to date.

It makes sense to maintain the list of selected rules across sessions, though. This might require getting the list of rules and comparing it against the current list of rules that have been set so that you can set those rules on the new session. If this is too complicated to get done right now, we at least need to get the set of rules from the server when the LanguageClient changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current implementation doesn't required the client the maintain any rule state at all. The client retrieves the rule list whenever the user opens the quickpick menu and sends the updated list to the server. It does not maintain any state about the rule list.

So, if the LangauageClient changes and if the user opens the quickpick menu, it will get a new list from the server.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! OK, that makes sense. I should have paid closer attention :) cool, will merge this now!

}

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 @@ -76,6 +77,7 @@ export function activate(context: vscode.ExtensionContext): void {
new ShowHelpFeature(),
new FindModuleFeature(),
new ExtensionCommandsFeature(),
new SelectPSSARulesFeature(),
new CodeActionsFeature()
];

Expand Down