Skip to content

Commit 85a589f

Browse files
committed
Refactor CheckboxQuickPick to showCheckboxQuickPick
This change refactors CheckboxQuickPick to a function called showCheckboxQuickPick to conform to VS Code's API for showQuickPick. The primary change is the use of a Thenable<T> instead of a callback function to communicate completion of the user's selection.
1 parent 613dbc7 commit 85a589f

File tree

2 files changed

+93
-71
lines changed

2 files changed

+93
-71
lines changed

src/checkboxQuickPick.ts

Lines changed: 81 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,94 @@
33
*--------------------------------------------------------*/
44

55
import vscode = require("vscode");
6-
import QuickPickItem = vscode.QuickPickItem;
76

8-
export class CheckboxQuickPickItem {
9-
name: string;
7+
var confirmItemLabel: string = "$(checklist) Confirm";
8+
var checkedPrefix: string = "[ $(check) ]";
9+
var uncheckedPrefix: string = "[ ]";
10+
var defaultPlaceHolder: string = "Select 'Confirm' to confirm or press 'Esc' key to cancel";
11+
12+
export interface CheckboxQuickPickItem {
13+
label: string;
14+
description?: string;
1015
isSelected: boolean;
1116
}
1217

13-
export class CheckboxQuickPick {
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";
18-
19-
public static show(
20-
checkboxQuickPickItems: CheckboxQuickPickItem[],
21-
callback: (items: CheckboxQuickPickItem[]) => void): void {
22-
CheckboxQuickPick.showInner(checkboxQuickPickItems.slice(), callback);
23-
}
24-
25-
private static showInner(
26-
items: CheckboxQuickPickItem[],
27-
callback: (items: CheckboxQuickPickItem[]) => void): void {
18+
export interface CheckboxQuickPickOptions {
19+
confirmPlaceHolder: string;
20+
}
21+
22+
var defaultOptions:CheckboxQuickPickOptions = { confirmPlaceHolder: defaultPlaceHolder};
23+
24+
export function showCheckboxQuickPick(
25+
items: CheckboxQuickPickItem[],
26+
options: CheckboxQuickPickOptions = defaultOptions): Thenable<CheckboxQuickPickItem[]> {
27+
28+
return showInner(items, options).then(
29+
(selectedItems) => {
30+
// We're mutating the original item list so just return it for now.
31+
return items
32+
})
33+
}
34+
35+
function getQuickPickItems(items: CheckboxQuickPickItem[]): vscode.QuickPickItem[] {
36+
37+
let quickPickItems: vscode.QuickPickItem[] = [];
38+
quickPickItems.push({ label: confirmItemLabel, description: "" });
39+
40+
items.forEach(item =>
41+
quickPickItems.push({
42+
label: convertToCheckBox(item),
43+
description: item.description
44+
}));
45+
46+
return quickPickItems;
47+
}
48+
49+
function showInner(
50+
items: CheckboxQuickPickItem[],
51+
options: CheckboxQuickPickOptions): Thenable<vscode.QuickPickItem> {
52+
53+
var quickPickThenable: Thenable<vscode.QuickPickItem> =
2854
vscode.window.showQuickPick(
29-
CheckboxQuickPick.getQuickPickItems(items),
55+
getQuickPickItems(items),
3056
{
3157
ignoreFocusOut: true,
3258
matchOnDescription: true,
33-
placeHolder: CheckboxQuickPick.confirmPlaceHolder
34-
}).then((selection) => {
35-
if (!selection) {
36-
return;
37-
}
38-
39-
if (selection.label === CheckboxQuickPick.confirm) {
40-
callback(items);
41-
return;
42-
}
43-
44-
let index: number = CheckboxQuickPick.getRuleIndex(items, selection.description);
45-
CheckboxQuickPick.toggleSelection(items[index]);
46-
CheckboxQuickPick.showInner(items, callback);
59+
placeHolder: options.confirmPlaceHolder
4760
});
48-
}
49-
50-
private static getRuleIndex(items: CheckboxQuickPickItem[], itemLabel: string): number {
51-
return items.findIndex(item => item.name === itemLabel);
52-
}
53-
54-
private static getQuickPickItems(items: CheckboxQuickPickItem[]): QuickPickItem[] {
55-
let quickPickItems: QuickPickItem[] = [];
56-
quickPickItems.push({ label: CheckboxQuickPick.confirm, description: "Confirm" });
57-
items.forEach(item =>
58-
quickPickItems.push({
59-
label: CheckboxQuickPick.convertToCheckBox(item.isSelected), description: item.name
60-
}));
61-
return quickPickItems;
62-
}
63-
64-
private static toggleSelection(item: CheckboxQuickPickItem): void {
65-
item.isSelected = !item.isSelected;
66-
}
67-
68-
private static convertToCheckBox(state: boolean): string {
69-
if (state) {
70-
return CheckboxQuickPick.checkboxOn;
71-
}
72-
else {
73-
return CheckboxQuickPick.checkboxOff;
74-
}
75-
}
61+
62+
return quickPickThenable.then(
63+
(selection) => {
64+
if (!selection) {
65+
return Promise.reject<vscode.QuickPickItem>("showCheckBoxQuickPick cancelled")
66+
}
67+
68+
if (selection.label === confirmItemLabel) {
69+
return selection;
70+
}
71+
72+
let index: number = getItemIndex(items, selection.label);
73+
74+
if (index > 0) {
75+
toggleSelection(items[index]);
76+
}
77+
else {
78+
console.log(`Couldn't find CheckboxQuickPickItem for label '${selection.label}'`);
79+
}
80+
81+
return showInner(items, options);
82+
});
83+
}
84+
85+
function getItemIndex(items: CheckboxQuickPickItem[], itemLabel: string): number {
86+
var trimmedLabel = itemLabel.substr(itemLabel.indexOf("]") + 2);
87+
return items.findIndex(item => item.label === trimmedLabel);
88+
}
89+
90+
function toggleSelection(item: CheckboxQuickPickItem): void {
91+
item.isSelected = !item.isSelected;
92+
}
93+
94+
function convertToCheckBox(item: CheckboxQuickPickItem): string {
95+
return `${item.isSelected ? checkedPrefix : uncheckedPrefix} ${item.label}`;
7696
}

src/features/SelectPSSARules.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import vscode = require("vscode");
66
import { IFeature } from "../feature";
77
import { LanguageClient, RequestType } from "vscode-languageclient";
8-
import { CheckboxQuickPickItem, CheckboxQuickPick } from "../checkboxQuickPick";
8+
import { CheckboxQuickPickItem, showCheckboxQuickPick } from "../checkboxQuickPick";
99

1010
export namespace GetPSSARulesRequest {
1111
export const type: RequestType<any, any, void> = { get method(): string { return "powerShell/getPSSARules"; } };
@@ -43,16 +43,18 @@ export class SelectPSSARulesFeature implements IFeature {
4343
return;
4444
}
4545
let options: CheckboxQuickPickItem[] = returnedRules.map(function (rule: RuleInfo): CheckboxQuickPickItem {
46-
return { name: rule.name, isSelected: rule.isEnabled };
46+
return { label: rule.name, isSelected: rule.isEnabled };
4747
});
48-
CheckboxQuickPick.show(options, (updatedOptions) => {
49-
let filepath: string = vscode.window.activeTextEditor.document.uri.fsPath;
50-
let ruleInfos: RuleInfo[] = updatedOptions.map(
51-
function (option: CheckboxQuickPickItem): RuleInfo {
52-
return { name: option.name, isEnabled: option.isSelected };
53-
});
54-
let requestParams: SetPSSARulesRequestParams = {filepath, ruleInfos};
55-
this.languageClient.sendRequest(SetPSSARulesRequest.type, requestParams);
48+
49+
showCheckboxQuickPick(options)
50+
.then(updatedOptions => {
51+
let filepath: string = vscode.window.activeTextEditor.document.uri.fsPath;
52+
let ruleInfos: RuleInfo[] = updatedOptions.map(
53+
function (option: CheckboxQuickPickItem): RuleInfo {
54+
return { name: option.label, isEnabled: option.isSelected };
55+
});
56+
let requestParams: SetPSSARulesRequestParams = {filepath, ruleInfos};
57+
this.languageClient.sendRequest(SetPSSARulesRequest.type, requestParams);
5658
});
5759
});
5860
});

0 commit comments

Comments
 (0)