-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathcheckboxQuickPick.ts
98 lines (77 loc) · 3.13 KB
/
checkboxQuickPick.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import vscode = require("vscode");
const confirmItemLabel: string = "$(checklist) Confirm";
const checkedPrefix: string = "[ $(check) ]";
const uncheckedPrefix: string = "[ ]";
const defaultPlaceHolder: string = "Select 'Confirm' to confirm or press 'Esc' key to cancel";
export interface ICheckboxQuickPickItem {
label: string;
description?: string;
isSelected: boolean;
}
export interface ICheckboxQuickPickOptions {
confirmPlaceHolder: string;
}
const defaultOptions: ICheckboxQuickPickOptions = { confirmPlaceHolder: defaultPlaceHolder};
export function showCheckboxQuickPick(
items: ICheckboxQuickPickItem[],
options: ICheckboxQuickPickOptions = defaultOptions): Thenable<ICheckboxQuickPickItem[]> {
return showInner(items, options).then(
(selectedItem) => {
// We're mutating the original item list so just return it for now.
// If 'selectedItem' is undefined it means the user cancelled the
// inner showQuickPick UI so pass the undefined along.
return selectedItem !== undefined ? items : undefined;
});
}
function getQuickPickItems(items: ICheckboxQuickPickItem[]): vscode.QuickPickItem[] {
const quickPickItems: vscode.QuickPickItem[] = [];
quickPickItems.push({ label: confirmItemLabel, description: "" });
for (const item of items) {
quickPickItems.push({
label: convertToCheckBox(item),
description: item.description,
});
}
return quickPickItems;
}
function showInner(
items: ICheckboxQuickPickItem[],
options: ICheckboxQuickPickOptions): Thenable<vscode.QuickPickItem> {
const quickPickThenable: Thenable<vscode.QuickPickItem> =
vscode.window.showQuickPick(
getQuickPickItems(items),
{
ignoreFocusOut: true,
matchOnDescription: true,
placeHolder: options.confirmPlaceHolder,
});
return quickPickThenable.then(
(selection) => {
if (!selection) {
return Promise.resolve<vscode.QuickPickItem>(undefined);
}
if (selection.label === confirmItemLabel) {
return selection;
}
const index: number = getItemIndex(items, selection.label);
if (index >= 0) {
toggleSelection(items[index]);
} else {
// tslint:disable-next-line:no-console
console.log(`Couldn't find CheckboxQuickPickItem for label '${selection.label}'`);
}
return showInner(items, options);
});
}
function getItemIndex(items: ICheckboxQuickPickItem[], itemLabel: string): number {
const trimmedLabel = itemLabel.substr(itemLabel.indexOf("]") + 2);
return items.findIndex((item) => item.label === trimmedLabel);
}
function toggleSelection(item: ICheckboxQuickPickItem): void {
item.isSelected = !item.isSelected;
}
function convertToCheckBox(item: ICheckboxQuickPickItem): string {
return `${item.isSelected ? checkedPrefix : uncheckedPrefix} ${item.label}`;
}