Skip to content

Commit 9f08c2d

Browse files
author
Kapil Borle
committed
Make CheckboxQuickPick.show a static method
1 parent 40a8fdd commit 9f08c2d

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

src/checkboxQuickPick.ts

+31-38
Original file line numberDiff line numberDiff line change
@@ -11,83 +11,76 @@ export class CheckboxQuickPickItem {
1111
}
1212

1313
export class CheckboxQuickPick {
14-
private options: CheckboxQuickPickItem[];
15-
private readonly confirm: string;
16-
private readonly checkboxOn: string;
17-
private readonly checkboxOff: string;
18-
private readonly confirmPlaceHolder: string;
14+
private static readonly confirm: string = "$(check)";
15+
private static readonly checkboxOn: string = "[ x ]";
16+
private static readonly checkboxOff: string = "[ ]";
17+
private static readonly confirmPlaceHolder: string = "Select 'Confirm' to confirm change; Press 'esc' key to cancel changes";
1918

20-
constructor(options: CheckboxQuickPickItem[]) {
21-
this.options = options;
22-
this.confirm = "$(check)";
23-
this.checkboxOn = "[ x ]";
24-
this.checkboxOff = "[ ]";
25-
this.confirmPlaceHolder = "Select 'Confirm' to confirm change; Press 'esc' key to cancel changes";
19+
public static show(
20+
checkboxQuickPickItems: CheckboxQuickPickItem[],
21+
callback: (items: CheckboxQuickPickItem[]) => void): void {
22+
CheckboxQuickPick.showInner(checkboxQuickPickItems.slice(), callback);
2623
}
2724

28-
public show(callback: (options: CheckboxQuickPickItem[]) => void): void {
29-
let tempOptions: CheckboxQuickPickItem[] = this.options.slice();
30-
this.showInner(tempOptions, callback);
31-
}
32-
33-
private showInner(
25+
private static showInner(
3426
tempOptions: CheckboxQuickPickItem[],
3527
callback: (options: CheckboxQuickPickItem[]) => void): void {
3628
vscode.window.showQuickPick(
37-
this.getQuickPickItems(tempOptions),
38-
{ ignoreFocusOut: true, placeHolder: this.confirmPlaceHolder }).then((selection) => {
29+
CheckboxQuickPick.getQuickPickItems(tempOptions),
30+
{ ignoreFocusOut: true, placeHolder: CheckboxQuickPick.confirmPlaceHolder }).then((selection) => {
3931
if (!selection) {
4032
return;
4133
}
4234

43-
if (selection.label == this.confirm) {
35+
if (selection.label === CheckboxQuickPick.confirm) {
4436
callback(tempOptions);
45-
this.options = tempOptions;
4637
return;
4738
}
4839

49-
let index: number = this.getRuleIndex(tempOptions, selection.description);
50-
this.toggleOption(tempOptions[index]);
51-
this.showInner(tempOptions, callback);
40+
let index: number = CheckboxQuickPick.getRuleIndex(tempOptions, selection.description);
41+
CheckboxQuickPick.toggleOption(tempOptions[index]);
42+
CheckboxQuickPick.showInner(tempOptions, callback);
5243
});
5344
}
5445

55-
private getRuleIndex(options: CheckboxQuickPickItem[], optionLabel: string): number {
46+
private static getRuleIndex(options: CheckboxQuickPickItem[], optionLabel: string): number {
5647
return options.findIndex(opt => opt.name == optionLabel);
5748
}
5849

59-
private getQuickPickItems(tempOptions: CheckboxQuickPickItem[]): QuickPickItem[] {
50+
private static getQuickPickItems(tempOptions: CheckboxQuickPickItem[]): QuickPickItem[] {
6051
let quickPickItems: QuickPickItem[] = [];
61-
quickPickItems.push({ label: this.confirm, description: "Confirm" });
52+
quickPickItems.push({ label: CheckboxQuickPick.confirm, description: "Confirm" });
6253
tempOptions.forEach(option =>
6354
quickPickItems.push({
64-
label: this.convertToCheckBox(option.isSelected), description: option.name
55+
label: CheckboxQuickPick.convertToCheckBox(option.isSelected), description: option.name
6556
}));
6657
return quickPickItems;
6758
}
6859

69-
private convertToState(checkBox: string): boolean {
70-
return checkBox == this.checkboxOn;
60+
private static convertToState(checkBox: string): boolean {
61+
return checkBox === CheckboxQuickPick.checkboxOn;
7162
}
7263

73-
private toggleState(state: boolean): boolean {
64+
private static toggleState(state: boolean): boolean {
7465
return !state;
7566
}
7667

77-
private toggleOption(option: CheckboxQuickPickItem): void {
78-
option.isSelected = this.toggleState(option.isSelected);
68+
private static toggleOption(option: CheckboxQuickPickItem): void {
69+
option.isSelected = CheckboxQuickPick.toggleState(option.isSelected);
7970
}
8071

81-
private toggleCheckBox(checkBox: string): string {
82-
return this.convertToCheckBox(this.toggleState(this.convertToState(checkBox)));
72+
private static toggleCheckBox(checkBox: string): string {
73+
return CheckboxQuickPick.convertToCheckBox(
74+
CheckboxQuickPick.toggleState(
75+
CheckboxQuickPick.convertToState(checkBox)));
8376
}
8477

85-
private convertToCheckBox(state: boolean): string {
78+
private static convertToCheckBox(state: boolean): string {
8679
if (state) {
87-
return this.checkboxOn;
80+
return CheckboxQuickPick.checkboxOn;
8881
}
8982
else {
90-
return this.checkboxOff;
83+
return CheckboxQuickPick.checkboxOff;
9184
}
9285
}
9386
}

src/features/SelectPSSARules.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class SelectPSSARulesFeature implements IFeature {
4141
let options: CheckboxQuickPickItem[] = returnedRules.map(function (rule: IRuleInfo): CheckboxQuickPickItem {
4242
return { name: rule.name, isSelected: rule.isEnabled };
4343
});
44-
(new CheckboxQuickPick(options)).show((updatedOptions) => {
44+
CheckboxQuickPick.show(options, (updatedOptions) => {
4545
this.languageClient.sendRequest(
4646
SetPSSARulesRequest.type,
4747
updatedOptions.map(function (option: CheckboxQuickPickItem): IRuleInfo {

0 commit comments

Comments
 (0)