-
Notifications
You must be signed in to change notification settings - Fork 500
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #358 from PowerShell/kapilmb/AddPSSARulesQuickPick
Create a choice list of PSSA rules
- Loading branch information
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters