@@ -2,19 +2,41 @@ import vscode = require("vscode");
2
2
3
3
import { InputBoxOptions } from "vscode" ;
4
4
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
+ */
7
16
export class InputBoxManager {
17
+ /** Set to true if an input box is currently shown */
8
18
private static shown = false ;
19
+
20
+ /** Array of input boxes to show */
9
21
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) */
10
24
private static currentKey : string ;
11
25
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
+ */
12
33
public static showInputBox ( options : InputBoxOptions , callback : ( answer : string ) => void , key ?: string ) : void {
13
34
const spec : InputBoxSpec = {
14
35
options : options ,
15
36
callback : callback ,
16
37
} ;
17
38
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.
18
40
if (
19
41
InputBoxManager . currentKey === key ||
20
42
InputBoxManager . specs . find ( ( spec ) => {
@@ -25,21 +47,31 @@ export class InputBoxManager {
25
47
}
26
48
spec . key = key ;
27
49
}
50
+
51
+ // Add the input box to the list to show
28
52
this . specs . push ( spec ) ;
29
53
if ( ! this . shown ) {
30
54
this . shown = true ;
55
+ // If we aren't already showing an input box, show this one.
31
56
this . next ( ) ;
32
57
}
33
58
}
34
59
60
+ /** Shows the next input box in the queue, if any */
35
61
private static next ( ) {
36
62
if ( this . specs . length === 0 ) {
63
+ // All done!
37
64
this . shown = false ;
38
65
this . currentKey = undefined ;
39
66
return ;
40
67
}
68
+
69
+ // Get the next input box
41
70
const spec = this . specs . shift ( ) ;
42
71
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.
43
75
vscode . window . showInputBox ( spec . options ) . then (
44
76
( result ) => {
45
77
spec . callback ( result ) ;
@@ -51,9 +83,3 @@ export class InputBoxManager {
51
83
) ;
52
84
}
53
85
}
54
-
55
- interface InputBoxSpec {
56
- key ?: string ;
57
- options : InputBoxOptions ;
58
- callback : ( answer : string ) => void ;
59
- }
0 commit comments