Skip to content

Commit 596392f

Browse files
committed
Rearrange and comment InputBoxManager
1 parent bf6f163 commit 596392f

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/utils/inputBoxManager.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,41 @@ import vscode = require("vscode");
22

33
import { InputBoxOptions } from "vscode";
44

5-
// Used in situations where multiple input boxes may be displayed to ensure that we don't stomp on
6-
// the box that's already shown.
5+
// Interface used internally to track queued input boxes
6+
interface InputBoxSpec {
7+
key?: string;
8+
options: InputBoxOptions;
9+
callback: (answer: string) => void;
10+
}
11+
12+
/**
13+
* Used in situations where multiple input boxes may be displayed in rapid succession
14+
* to allow the user to respond to them one at a time.
15+
*/
716
export class InputBoxManager {
17+
/** Set to true if an input box is currently shown */
818
private static shown = false;
19+
20+
/** Array of input boxes to show */
921
private static specs = new Array<InputBoxSpec>();
22+
23+
/** Tracks the current key of the input box being shown (to make sure we don't re-queue it) */
1024
private static currentKey: string;
1125

26+
/**
27+
* Queues an input box to be shown via `vscode.window.showInputBox()`. If no input box is shown, will show immediately; otherwise,
28+
* will show once all previously-queued input boxes have resolved.
29+
* @param options Set of options for the input box
30+
* @param callback Function to run when the input box resolves. (If rejected, will move on to the next input box.)
31+
* @param key A unique identifier for the input box to be shown. If already present in the queue, will not be duplicated.
32+
*/
1233
public static showInputBox(options: InputBoxOptions, callback: (answer: string) => void, key?: string): void {
1334
const spec: InputBoxSpec = {
1435
options: options,
1536
callback: callback,
1637
};
1738
if (key) {
39+
// If a key was provided, make sure that we aren't already showing (and haven't already queued) an input box with that key.
1840
if (
1941
InputBoxManager.currentKey === key ||
2042
InputBoxManager.specs.find((spec) => {
@@ -25,21 +47,31 @@ export class InputBoxManager {
2547
}
2648
spec.key = key;
2749
}
50+
51+
// Add the input box to the list to show
2852
this.specs.push(spec);
2953
if (!this.shown) {
3054
this.shown = true;
55+
// If we aren't already showing an input box, show this one.
3156
this.next();
3257
}
3358
}
3459

60+
/** Shows the next input box in the queue, if any */
3561
private static next() {
3662
if (this.specs.length === 0) {
63+
// All done!
3764
this.shown = false;
3865
this.currentKey = undefined;
3966
return;
4067
}
68+
69+
// Get the next input box
4170
const spec = this.specs.shift();
4271
this.currentKey = spec.key;
72+
73+
// Show the input box, then execute the user-provided callback.
74+
// Always move on to the next input box afterward.
4375
vscode.window.showInputBox(spec.options).then(
4476
(result) => {
4577
spec.callback(result);
@@ -51,9 +83,3 @@ export class InputBoxManager {
5183
);
5284
}
5385
}
54-
55-
interface InputBoxSpec {
56-
key?: string;
57-
options: InputBoxOptions;
58-
callback: (answer: string) => void;
59-
}

0 commit comments

Comments
 (0)