-
Notifications
You must be signed in to change notification settings - Fork 514
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
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6324789
List PSScriptAnalyzer rules
bbc41f0
Get a list of active PSSA rules
fb47aa0
Update list of active PSSA rules
ab498d4
Create class to select mulitple options
967b897
Add figures module to dependencies
cc28ba8
Fix merge conflict with master
9c0c14a
Rename CheckboxQuickPick.ts to checkboxQuickPick.ts
b00bc50
Add SelectPSSARules command to the extension
b3ba01f
Update interface names and clean-up some
5389d49
Replace figures.check with built-in icon
561fd68
Notify if extension uses PSSA settings file
40a8fdd
Move confirmation to the top of the checkbox quick pick menu
9f08c2d
Make CheckboxQuickPick.show a static method
e98b1e8
Remove unused methods from CheckboxQuickPick class
249d05f
Send filepath param for SetPSSARulesRequest
aeacf0f
Allow searching of PSSA rules from palette
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!