|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// tslint:disable:max-func-body-length no-any no-unnecessary-class |
| 7 | + |
| 8 | +import { Disposable, QuickInput, QuickInputButton, QuickInputButtons, QuickPickItem } from 'vscode'; |
| 9 | +import { IApplicationShell } from '../application/types'; |
| 10 | + |
| 11 | +// Borrowed from https://github.com/Microsoft/vscode-extension-samples/blob/master/quickinput-sample/src/multiStepInput.ts |
| 12 | +// Why re-invent the wheel :) |
| 13 | + |
| 14 | +export class InputFlowAction { |
| 15 | + public static back = new InputFlowAction(); |
| 16 | + public static cancel = new InputFlowAction(); |
| 17 | + public static resume = new InputFlowAction(); |
| 18 | + private constructor() { } |
| 19 | +} |
| 20 | + |
| 21 | +export type InputStep<T extends any> = (input: MultiStepInput<T>, state?: T) => Thenable<InputStep<T> | void>; |
| 22 | + |
| 23 | +export interface IQuickPickParameters<T extends QuickPickItem> { |
| 24 | + title: string; |
| 25 | + step?: number; |
| 26 | + totalSteps?: number; |
| 27 | + canGoBack?: boolean; |
| 28 | + items: T[]; |
| 29 | + activeItem?: T; |
| 30 | + placeholder: string; |
| 31 | + buttons?: QuickInputButton[]; |
| 32 | + shouldResume?(): Thenable<boolean>; |
| 33 | +} |
| 34 | + |
| 35 | +export interface InputBoxParameters { |
| 36 | + title: string; |
| 37 | + step?: number; |
| 38 | + totalSteps?: number; |
| 39 | + value: string; |
| 40 | + prompt: string; |
| 41 | + buttons?: QuickInputButton[]; |
| 42 | + validate(value: string): Promise<string | undefined>; |
| 43 | + shouldResume?(): Thenable<boolean>; |
| 44 | +} |
| 45 | + |
| 46 | +export class MultiStepInput<S> { |
| 47 | + private current?: QuickInput; |
| 48 | + private steps: InputStep<S>[] = []; |
| 49 | + constructor(private readonly shell: IApplicationShell) { } |
| 50 | + public static async run<S>(shell: IApplicationShell, start: InputStep<S>, state: S) { |
| 51 | + const input = new MultiStepInput<S>(shell); |
| 52 | + return input.stepThrough(start, state); |
| 53 | + } |
| 54 | + |
| 55 | + public async showQuickPick<T extends QuickPickItem, P extends IQuickPickParameters<T>>({ title, step, totalSteps, items, activeItem, placeholder, buttons, shouldResume }: P) { |
| 56 | + const disposables: Disposable[] = []; |
| 57 | + try { |
| 58 | + return await new Promise<T | (P extends { buttons: (infer I)[] } ? I : never)>((resolve, reject) => { |
| 59 | + const input = this.shell.createQuickPick<T>(); |
| 60 | + input.title = title; |
| 61 | + input.step = step; |
| 62 | + input.totalSteps = totalSteps; |
| 63 | + input.placeholder = placeholder; |
| 64 | + input.ignoreFocusOut = true; |
| 65 | + input.items = items; |
| 66 | + if (activeItem) { |
| 67 | + input.activeItems = [activeItem]; |
| 68 | + } |
| 69 | + input.buttons = [ |
| 70 | + ...(this.steps.length > 1 ? [QuickInputButtons.Back] : []), |
| 71 | + ...(buttons || []) |
| 72 | + ]; |
| 73 | + disposables.push( |
| 74 | + input.onDidTriggerButton(item => { |
| 75 | + if (item === QuickInputButtons.Back) { |
| 76 | + reject(InputFlowAction.back); |
| 77 | + } else { |
| 78 | + resolve(<any>item); |
| 79 | + } |
| 80 | + }), |
| 81 | + input.onDidChangeSelection(selectedItems => resolve(selectedItems[0])), |
| 82 | + input.onDidHide(() => { |
| 83 | + (async () => { |
| 84 | + reject(shouldResume && await shouldResume() ? InputFlowAction.resume : InputFlowAction.cancel); |
| 85 | + })() |
| 86 | + .catch(reject); |
| 87 | + }) |
| 88 | + ); |
| 89 | + if (this.current) { |
| 90 | + this.current.dispose(); |
| 91 | + } |
| 92 | + this.current = input; |
| 93 | + this.current.show(); |
| 94 | + }); |
| 95 | + } finally { |
| 96 | + disposables.forEach(d => d.dispose()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public async showInputBox<P extends InputBoxParameters>({ title, step, totalSteps, value, prompt, validate, buttons, shouldResume }: P) { |
| 101 | + const disposables: Disposable[] = []; |
| 102 | + try { |
| 103 | + return await new Promise<string | (P extends { buttons: (infer I)[] } ? I : never)>((resolve, reject) => { |
| 104 | + const input = this.shell.createInputBox(); |
| 105 | + input.title = title; |
| 106 | + input.step = step; |
| 107 | + input.totalSteps = totalSteps; |
| 108 | + input.value = value || ''; |
| 109 | + input.prompt = prompt; |
| 110 | + input.ignoreFocusOut = true; |
| 111 | + input.buttons = [ |
| 112 | + ...(this.steps.length > 1 ? [QuickInputButtons.Back] : []), |
| 113 | + ...(buttons || []) |
| 114 | + ]; |
| 115 | + let validating = validate(''); |
| 116 | + disposables.push( |
| 117 | + input.onDidTriggerButton(item => { |
| 118 | + if (item === QuickInputButtons.Back) { |
| 119 | + reject(InputFlowAction.back); |
| 120 | + } else { |
| 121 | + resolve(<any>item); |
| 122 | + } |
| 123 | + }), |
| 124 | + input.onDidAccept(async () => { |
| 125 | + const inputValue = input.value; |
| 126 | + input.enabled = false; |
| 127 | + input.busy = true; |
| 128 | + if (!(await validate(inputValue))) { |
| 129 | + resolve(inputValue); |
| 130 | + } |
| 131 | + input.enabled = true; |
| 132 | + input.busy = false; |
| 133 | + }), |
| 134 | + input.onDidChangeValue(async text => { |
| 135 | + const current = validate(text); |
| 136 | + validating = current; |
| 137 | + const validationMessage = await current; |
| 138 | + if (current === validating) { |
| 139 | + input.validationMessage = validationMessage; |
| 140 | + } |
| 141 | + }), |
| 142 | + input.onDidHide(() => { |
| 143 | + (async () => { |
| 144 | + reject(shouldResume && await shouldResume() ? InputFlowAction.resume : InputFlowAction.cancel); |
| 145 | + })() |
| 146 | + .catch(reject); |
| 147 | + }) |
| 148 | + ); |
| 149 | + if (this.current) { |
| 150 | + this.current.dispose(); |
| 151 | + } |
| 152 | + this.current = input; |
| 153 | + this.current.show(); |
| 154 | + }); |
| 155 | + } finally { |
| 156 | + disposables.forEach(d => d.dispose()); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private async stepThrough<T>(start: InputStep<S>, state: S) { |
| 161 | + let step: InputStep<S> | void = start; |
| 162 | + while (step) { |
| 163 | + this.steps.push(step); |
| 164 | + if (this.current) { |
| 165 | + this.current.enabled = false; |
| 166 | + this.current.busy = true; |
| 167 | + } |
| 168 | + try { |
| 169 | + step = await step(this, state); |
| 170 | + } catch (err) { |
| 171 | + if (err === InputFlowAction.back) { |
| 172 | + this.steps.pop(); |
| 173 | + step = this.steps.pop(); |
| 174 | + } else if (err === InputFlowAction.resume) { |
| 175 | + step = this.steps.pop(); |
| 176 | + } else if (err === InputFlowAction.cancel) { |
| 177 | + step = undefined; |
| 178 | + } else { |
| 179 | + throw err; |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + if (this.current) { |
| 184 | + this.current.dispose(); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments