Skip to content

Commit ab498d4

Browse files
author
Kapil Borle
committed
Create class to select mulitple options
1 parent fb47aa0 commit ab498d4

File tree

2 files changed

+108
-89
lines changed

2 files changed

+108
-89
lines changed

src/CheckboxQuickPick.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*---------------------------------------------------------
2+
* Copyright (C) Microsoft Corporation. All rights reserved.
3+
*--------------------------------------------------------*/
4+
5+
import vscode = require('vscode');
6+
import QuickPickItem = vscode.QuickPickItem;
7+
const figures = require('figures');
8+
9+
export interface Option {
10+
name: string;
11+
isSelected: boolean;
12+
}
13+
14+
export class CheckboxQuickPick {
15+
private options: Option[];
16+
private readonly confirm: string;
17+
private readonly checkboxOn: string;
18+
private readonly checkboxOff: string;
19+
private readonly confirmPlaceHolder: string;
20+
21+
constructor(options: Option[]) {
22+
this.options = options;
23+
this.confirm = figures.tick;
24+
this.checkboxOn = "[ x ]";
25+
this.checkboxOff = "[ ]";
26+
this.confirmPlaceHolder = "Select " + this.confirm + " to confirm";
27+
}
28+
29+
public show(callback: (options: Option[]) => void) {
30+
let tempOptions = this.options.slice();
31+
this.showInner(tempOptions, callback);
32+
}
33+
34+
private showInner(tempOptions: Option[], callback: (options: Option[]) => void) {
35+
vscode.window.showQuickPick(
36+
this.getQuickPickItems(tempOptions),
37+
{ ignoreFocusOut: true, placeHolder: this.confirmPlaceHolder }).then((selection) => {
38+
if (!selection) {
39+
return;
40+
}
41+
42+
if (selection.label == this.confirm) {
43+
callback(tempOptions);
44+
this.options = tempOptions;
45+
return;
46+
}
47+
48+
let index = this.getRuleIndex(tempOptions, selection.description);
49+
// this.toggleOption(tempOptions[index]);
50+
tempOptions[index].isSelected = this.toggleState(tempOptions[index].isSelected);
51+
this.showInner(tempOptions, callback);
52+
});
53+
}
54+
55+
private getRuleIndex(options: Option[], optionLabel: string) {
56+
return options.findIndex(opt => opt.name == optionLabel);
57+
}
58+
59+
private getQuickPickItems(tempOptions: Option[]): QuickPickItem[] {
60+
let quickPickItems: QuickPickItem[] = [];
61+
tempOptions.forEach(option =>
62+
quickPickItems.push({
63+
label: this.convertToCheckBox(option.isSelected), description: option.name
64+
}));
65+
quickPickItems.push({ label: this.confirm, description: "Confirm" });
66+
return quickPickItems;
67+
}
68+
69+
private convertToState(checkBox: string): boolean {
70+
return checkBox == this.checkboxOn;
71+
}
72+
73+
private toggleOption(option: Option) {
74+
option.isSelected = this.toggleState(option.isSelected);
75+
}
76+
77+
private toggleState(state: boolean): boolean {
78+
return !state;
79+
}
80+
81+
private toggleCheckBox(checkBox: string): string {
82+
return this.convertToCheckBox(this.toggleState(this.convertToState(checkBox)));
83+
}
84+
85+
private convertToCheckBox(state: boolean): string {
86+
if (state) {
87+
return this.checkboxOn;
88+
}
89+
else {
90+
return this.checkboxOff;
91+
}
92+
}
93+
}

src/features/SelectPSSARules.ts

Lines changed: 15 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ import vscode = require('vscode');
66
import { IFeature } from '../feature';
77
import QuickPickItem = vscode.QuickPickItem;
88
import { LanguageClient, RequestType, NotificationType } from 'vscode-languageclient';
9+
import { Option, CheckboxQuickPick } from '../checkboxQuickPick';
910
const figures = require('figures');
1011

1112
export namespace GetPSSARulesRequest {
1213
export const type: RequestType<any, any, void> = { get method() { return 'powerShell/getPSSARules'; } };
1314
}
1415

1516
export namespace SetPSSARulesRequest {
16-
export const type: RequestType<any, any, void> = { get method() {return 'powerShell/setPSSARules';}}
17+
export const type: RequestType<any, any, void> = { get method() { return 'powerShell/setPSSARules'; } }
1718
}
1819

1920
interface RuleInfo {
20-
Name: string;
21-
IsEnabled: boolean;
21+
name: string;
22+
isEnabled: boolean;
2223
}
2324

2425
export class SelectPSSARulesFeature implements IFeature {
@@ -33,95 +34,20 @@ export class SelectPSSARulesFeature implements IFeature {
3334
return;
3435
}
3536

36-
const editor = vscode.window.activeTextEditor;
37-
38-
var selection = editor.selection;
39-
var doc = editor.document;
40-
var cwr = doc.getWordRangeAtPosition(selection.active)
41-
var text = doc.getText(cwr);
42-
43-
let rules: RuleInfo[] = [];
37+
let options: Option[] = [];
4438
this.languageClient.sendRequest(GetPSSARulesRequest.type, null).then((returnedRules) => {
45-
for (var index = 0; index < returnedRules.length; index++) {
46-
var element = returnedRules[index];
47-
rules.push({Name : element.name, IsEnabled : element.isEnabled})
48-
}
49-
this.GetSelections(rules);
39+
let options: Option[] = returnedRules.map(function (rule): Option {
40+
return { name: rule.name, isSelected: rule.isEnabled };
5041
});
51-
});
52-
}
53-
54-
private GetSelections(rules: RuleInfo[])
55-
{
56-
vscode.window.showQuickPick(this.GetQuickPickItems(rules))
57-
.then((selection) =>{
58-
if (!selection)
59-
{
60-
return;
61-
}
62-
63-
if (selection.label == figures.tick)
64-
{
65-
this.languageClient.sendRequest(SetPSSARulesRequest.type, rules); //TODO handle error
66-
return;
67-
}
68-
69-
let index = this.GetRuleIndex(rules, selection.description);
70-
rules[index].IsEnabled = this.ToggleState(rules[index].IsEnabled);
71-
this.GetSelections(rules);
42+
(new CheckboxQuickPick(options)).show((updatedOptions) => {
43+
this.languageClient.sendRequest(
44+
SetPSSARulesRequest.type,
45+
updatedOptions.map(function (option): RuleInfo {
46+
return { name: option.name, isEnabled: option.isSelected };
47+
}));
7248
});
73-
}
74-
75-
private GetRuleIndex(rules: RuleInfo[], ruleName: string) : number
76-
{
77-
return rules.findIndex(rule => rule.Name == ruleName);
78-
}
79-
80-
private GetCheckBoxOn() : string
81-
{
82-
return "[ x ]"; // this looks better than figure.checkboxOn
83-
}
84-
85-
private GetCheckBoxOff() : string
86-
{
87-
return "[ ]"; // this looks better than figure.checkboxOff
88-
}
89-
90-
private ConvertToState(checkBox: string) : boolean
91-
{
92-
return checkBox == this.GetCheckBoxOn();
93-
}
94-
95-
private ToggleState(state: boolean) : boolean
96-
{
97-
return !state;
98-
}
99-
100-
private ToggleCheckBox(checkBox: string): string
101-
{
102-
return this.ConvertToCheckBox(this.ToggleState(this.ConvertToState(checkBox)));
103-
}
104-
105-
private ConvertToCheckBox(state: boolean): string
106-
{
107-
if (state)
108-
{
109-
return this.GetCheckBoxOn();
110-
}
111-
else
112-
{
113-
return this.GetCheckBoxOff();
114-
}
115-
}
116-
117-
private GetQuickPickItems(rules: RuleInfo[]): QuickPickItem[] {
118-
let qItems: QuickPickItem[] = [];
119-
for (var index = 0; index < rules.length; index++) {
120-
var element = rules[index];
121-
qItems.push({label: this.ConvertToCheckBox(element.IsEnabled), description: element.Name })
122-
}
123-
qItems.push({label: figures.tick, description: "confirm"});
124-
return qItems;
49+
});
50+
});
12551
}
12652

12753
public setLanguageClient(languageclient: LanguageClient) {

0 commit comments

Comments
 (0)