/
inline-input.ts
105 lines (88 loc) · 2.48 KB
/
inline-input.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
99
100
101
102
103
104
105
import * as vscode from 'vscode';
class Input {
text: string;
validateInput: (text: string) => string;
resolve: (text: string) => void;
reject: (reason: any) => void;
constructor(options: {
validateInput: (text: string) => string;
resolve: (text: string) => void;
reject: (reason: any) => void;
}) {
this.text = '';
this.validateInput = options.validateInput;
this.resolve = options.resolve;
this.reject = options.reject;
}
}
export class InlineInput {
private subscriptions: vscode.Disposable[] = [];
private input: Input;
constructor() {
this.registerTextEditorCommand('extension.aceJump.input.stop', this.cancel);
}
show = (
editor: vscode.TextEditor,
validateInput: (text: string) => string
): Promise<string> => {
this.setContext(true);
const promise = new Promise<string>((resolve, reject) => {
this.input = new Input({
validateInput: validateInput,
resolve: resolve,
reject: reject
});
vscode.window.onDidChangeActiveTextEditor(() => {
this.cancel(editor);
});
});
this.registerCommand('type', this.onType);
return promise;
};
private dispose = () => {
this.subscriptions.forEach(d => d.dispose());
};
private registerTextEditorCommand = (
commandId: string,
run: (
editor: vscode.TextEditor,
edit: vscode.TextEditorEdit,
...args: any[]
) => void
): void => {
this.subscriptions.push(
vscode.commands.registerTextEditorCommand(commandId, run)
);
};
private registerCommand = (
commandId: string,
run: (...args: any[]) => void
): void => {
this.subscriptions.push(vscode.commands.registerCommand(commandId, run));
};
private onType = (event: { text: string }) => {
const editor = vscode.window.activeTextEditor;
if (this.input) {
this.input.text += event.text;
this.input.validateInput(this.input.text);
this.complete(editor);
} else vscode.commands.executeCommand('default:type', event);
};
private cancel = (editor: vscode.TextEditor) => {
if (this.input) {
this.input.reject('canceled');
}
this.dispose();
this.setContext(false);
};
private complete = (editor: vscode.TextEditor) => {
if (this.input) {
this.input.resolve(this.input.text);
}
this.dispose();
this.setContext(false);
};
private setContext(value: boolean) {
vscode.commands.executeCommand('setContext', 'aceJumpInput', value);
}
}