forked from Silverquark/dance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modes.ts
64 lines (53 loc) · 1.79 KB
/
modes.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
import { Context } from "./context";
/**
* Switches to the mode with the given name.
*/
export function toMode(modeName: string): Thenable<void>;
/**
* Temporarily switches to the mode with the given name.
*/
export function toMode(modeName: string, count: number): Thenable<void>;
export async function toMode(modeName: string, count?: number) {
const context = Context.current,
extension = context.extension,
mode = extension.modes.get(modeName);
if (mode === undefined || mode.isPendingDeletion) {
throw new Error(`mode ${JSON.stringify(modeName)} does not exist`);
}
if (!count) {
return context.switchToMode(mode);
}
const editorState = context.getState(),
initialMode = editorState.mode,
disposable = extension
.createAutoDisposable()
.disposeOnEvent(editorState.onVisibilityDidChange)
.addDisposable({
dispose() {
context.switchToMode(initialMode);
},
});
await context.switchToMode(mode);
// We must start listening for events after a short delay, otherwise we will
// be notified of the mode change above, immediately returning to the
// previous mode.
setTimeout(() => {
const { Entry } = extension.recorder;
disposable
.addDisposable(extension.recorder.onDidAddEntry((entry) => {
if (entry instanceof Entry.ExecuteCommand
&& entry.descriptor().identifier.endsWith("updateCount")) {
// Ignore number inputs.
return;
}
if (entry instanceof Entry.ChangeTextEditor
|| entry instanceof Entry.ChangeTextEditorMode) {
// Immediately dispose.
return disposable.dispose();
}
if (--count! === 0) {
disposable.dispose();
}
}));
}, 0);
}