forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiStepInput.ts
More file actions
239 lines (226 loc) · 8.48 KB
/
multiStepInput.ts
File metadata and controls
239 lines (226 loc) · 8.48 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
// tslint:disable:max-func-body-length no-any no-unnecessary-class
import { inject, injectable } from 'inversify';
import { Disposable, QuickInput, QuickInputButton, QuickInputButtons, QuickPickItem } from 'vscode';
import { IApplicationShell } from '../application/types';
// Borrowed from https://github.com/Microsoft/vscode-extension-samples/blob/master/quickinput-sample/src/multiStepInput.ts
// Why re-invent the wheel :)
export class InputFlowAction {
public static back = new InputFlowAction();
public static cancel = new InputFlowAction();
public static resume = new InputFlowAction();
private constructor() {}
}
export type InputStep<T extends any> = (input: MultiStepInput<T>, state: T) => Promise<InputStep<T> | void>;
export interface IQuickPickParameters<T extends QuickPickItem> {
title: string;
step?: number;
totalSteps?: number;
canGoBack?: boolean;
items: T[];
activeItem?: T;
placeholder: string;
buttons?: QuickInputButton[];
shouldResume?(): Promise<boolean>;
}
// tslint:disable-next-line: interface-name
export interface InputBoxParameters {
title: string;
step?: number;
totalSteps?: number;
value: string;
prompt: string;
buttons?: QuickInputButton[];
validate(value: string): Promise<string | undefined>;
shouldResume?(): Promise<boolean>;
}
type MultiStepInputQuickPicResponseType<T, P> = T | (P extends { buttons: (infer I)[] } ? I : never);
type MultiStepInputInputBoxResponseType<P> = string | (P extends { buttons: (infer I)[] } ? I : never);
export interface IMultiStepInput<S> {
run(start: InputStep<S>, state: S): Promise<void>;
showQuickPick<T extends QuickPickItem, P extends IQuickPickParameters<T>>({
title,
step,
totalSteps,
items,
activeItem,
placeholder,
buttons,
shouldResume
}: P): Promise<MultiStepInputQuickPicResponseType<T, P>>;
showInputBox<P extends InputBoxParameters>({
title,
step,
totalSteps,
value,
prompt,
validate,
buttons,
shouldResume
}: P): Promise<MultiStepInputInputBoxResponseType<P>>;
}
export class MultiStepInput<S> implements IMultiStepInput<S> {
private current?: QuickInput;
private steps: InputStep<S>[] = [];
constructor(private readonly shell: IApplicationShell) {}
public run(start: InputStep<S>, state: S) {
return this.stepThrough(start, state);
}
public async showQuickPick<T extends QuickPickItem, P extends IQuickPickParameters<T>>({
title,
step,
totalSteps,
items,
activeItem,
placeholder,
buttons,
shouldResume
}: P): Promise<MultiStepInputQuickPicResponseType<T, P>> {
const disposables: Disposable[] = [];
try {
return await new Promise<MultiStepInputQuickPicResponseType<T, P>>((resolve, reject) => {
const input = this.shell.createQuickPick<T>();
input.title = title;
input.step = step;
input.totalSteps = totalSteps;
input.placeholder = placeholder;
input.ignoreFocusOut = true;
input.items = items;
if (activeItem) {
input.activeItems = [activeItem];
}
input.buttons = [...(this.steps.length > 1 ? [QuickInputButtons.Back] : []), ...(buttons || [])];
disposables.push(
input.onDidTriggerButton(item => {
if (item === QuickInputButtons.Back) {
reject(InputFlowAction.back);
} else {
resolve(<any>item);
}
}),
input.onDidChangeSelection(selectedItems => resolve(selectedItems[0])),
input.onDidHide(() => {
(async () => {
reject(
shouldResume && (await shouldResume()) ? InputFlowAction.resume : InputFlowAction.cancel
);
})().catch(reject);
})
);
if (this.current) {
this.current.dispose();
}
this.current = input;
this.current.show();
});
} finally {
disposables.forEach(d => d.dispose());
}
}
public async showInputBox<P extends InputBoxParameters>({
title,
step,
totalSteps,
value,
prompt,
validate,
buttons,
shouldResume
}: P): Promise<MultiStepInputInputBoxResponseType<P>> {
const disposables: Disposable[] = [];
try {
return await new Promise<MultiStepInputInputBoxResponseType<P>>((resolve, reject) => {
const input = this.shell.createInputBox();
input.title = title;
input.step = step;
input.totalSteps = totalSteps;
input.value = value || '';
input.prompt = prompt;
input.ignoreFocusOut = true;
input.buttons = [...(this.steps.length > 1 ? [QuickInputButtons.Back] : []), ...(buttons || [])];
let validating = validate('');
disposables.push(
input.onDidTriggerButton(item => {
if (item === QuickInputButtons.Back) {
reject(InputFlowAction.back);
} else {
resolve(<any>item);
}
}),
input.onDidAccept(async () => {
const inputValue = input.value;
input.enabled = false;
input.busy = true;
if (!(await validate(inputValue))) {
resolve(inputValue);
}
input.enabled = true;
input.busy = false;
}),
input.onDidChangeValue(async text => {
const current = validate(text);
validating = current;
const validationMessage = await current;
if (current === validating) {
input.validationMessage = validationMessage;
}
}),
input.onDidHide(() => {
(async () => {
reject(
shouldResume && (await shouldResume()) ? InputFlowAction.resume : InputFlowAction.cancel
);
})().catch(reject);
})
);
if (this.current) {
this.current.dispose();
}
this.current = input;
this.current.show();
});
} finally {
disposables.forEach(d => d.dispose());
}
}
private async stepThrough(start: InputStep<S>, state: S) {
let step: InputStep<S> | void = start;
while (step) {
this.steps.push(step);
if (this.current) {
this.current.enabled = false;
this.current.busy = true;
}
try {
step = await step(this, state);
} catch (err) {
if (err === InputFlowAction.back) {
this.steps.pop();
step = this.steps.pop();
} else if (err === InputFlowAction.resume) {
step = this.steps.pop();
} else if (err === InputFlowAction.cancel) {
step = undefined;
} else {
throw err;
}
}
}
if (this.current) {
this.current.dispose();
}
}
}
export const IMultiStepInputFactory = Symbol('IMultiStepInputFactory');
export interface IMultiStepInputFactory {
create<S>(): IMultiStepInput<S>;
}
@injectable()
export class MultiStepInputFactory {
constructor(@inject(IApplicationShell) private readonly shell: IApplicationShell) {}
public create<S>(): IMultiStepInput<S> {
return new MultiStepInput<S>(this.shell);
}
}