Skip to content

Commit 2f5fcec

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 2f5fcec

File tree

2 files changed

+96
-71
lines changed

2 files changed

+96
-71
lines changed

src/checkboxQuickPick.ts

Lines changed: 84 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,97 @@
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+
(selectedItem) => {
30+
// We're mutating the original item list so just return it for now.
31+
// If 'selectedItem' is undefined it means the user cancelled the
32+
// inner showQuickPick UI so pass the undefined along.
33+
return selectedItem != undefined ? items : undefined;
34+
})
35+
}
36+
37+
function getQuickPickItems(items: CheckboxQuickPickItem[]): vscode.QuickPickItem[] {
38+
39+
let quickPickItems: vscode.QuickPickItem[] = [];
40+
quickPickItems.push({ label: confirmItemLabel, description: "" });
41+
42+
items.forEach(item =>
43+
quickPickItems.push({
44+
label: convertToCheckBox(item),
45+
description: item.description
46+
}));
47+
48+
return quickPickItems;
49+
}
50+
51+
function showInner(
52+
items: CheckboxQuickPickItem[],
53+
options: CheckboxQuickPickOptions): Thenable<vscode.QuickPickItem> {
54+
55+
var quickPickThenable: Thenable<vscode.QuickPickItem> =
2856
vscode.window.showQuickPick(
29-
CheckboxQuickPick.getQuickPickItems(items),
57+
getQuickPickItems(items),
3058
{
3159
ignoreFocusOut: true,
3260
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);
61+
placeHolder: options.confirmPlaceHolder
4762
});
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-
}
63+
64+
return quickPickThenable.then(
65+
(selection) => {
66+
if (!selection) {
67+
//return Promise.reject<vscode.QuickPickItem>("showCheckBoxQuickPick cancelled")
68+
return Promise.resolve<vscode.QuickPickItem>(undefined);
69+
}
70+
71+
if (selection.label === confirmItemLabel) {
72+
return selection;
73+
}
74+
75+
let index: number = getItemIndex(items, selection.label);
76+
77+
if (index >= 0) {
78+
toggleSelection(items[index]);
79+
}
80+
else {
81+
console.log(`Couldn't find CheckboxQuickPickItem for label '${selection.label}'`);
82+
}
83+
84+
return showInner(items, options);
85+
});
86+
}
87+
88+
function getItemIndex(items: CheckboxQuickPickItem[], itemLabel: string): number {
89+
var trimmedLabel = itemLabel.substr(itemLabel.indexOf("]") + 2);
90+
return items.findIndex(item => item.label === trimmedLabel);
91+
}
92+
93+
function toggleSelection(item: CheckboxQuickPickItem): void {
94+
item.isSelected = !item.isSelected;
95+
}
96+
97+
function convertToCheckBox(item: CheckboxQuickPickItem): string {
98+
return `${item.isSelected ? checkedPrefix : uncheckedPrefix} ${item.label}`;
7699
}

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)