-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathregisterCommands.ts
More file actions
138 lines (120 loc) · 5.02 KB
/
registerCommands.ts
File metadata and controls
138 lines (120 loc) · 5.02 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import type { NeovimClient } from "neovim";
import type {
CommandResponse,
CommandServerApi,
CursorlessCommandId,
} from "@cursorless/lib-common";
import {
CURSORLESS_COMMAND_ID,
clientSupportsFallback,
ensureCommandShape,
} from "@cursorless/lib-common";
import type { CommandApi } from "@cursorless/lib-engine";
import type { NeovimIDE } from "@cursorless/lib-neovim-common";
import {
modeSwitchNormalTerminal,
modeSwitchTerminal,
} from "@cursorless/lib-neovim-common";
import { getNeovimRegistry } from "@cursorless/lib-neovim-registry";
export async function registerCommands(
client: NeovimClient,
neovimIDE: NeovimIDE,
commandApi: CommandApi,
commandServerApi: CommandServerApi,
): Promise<void> {
const commands: Record<CursorlessCommandId, (...args: any[]) => any> = {
// The core Cursorless command
[CURSORLESS_COMMAND_ID]: async (...args: unknown[]) => {
const originalMode = await client.mode;
if (originalMode.mode === "t") {
// Switch to "nt" so we can easily call lua functions without any problems
void modeSwitchNormalTerminal(client);
}
try {
await neovimIDE.updateTextEditor();
const result = await commandApi.runCommandSafe(...args);
const command = ensureCommandShape(args);
const focusedElementType =
await commandServerApi.getFocusedElementType();
if (
focusedElementType === "terminal" &&
clientSupportsFallback(command)
) {
const commandResponse = result as CommandResponse;
if (
"fallback" in commandResponse &&
commandResponse.fallback.action === "insert"
) {
// if user runs a terminal, and a "bring" command was requested, switch back to "t" mode
// so the fallback can do its magic
void modeSwitchTerminal(client);
}
}
return result;
} catch (e) {
if (neovimIDE.runMode !== "test") {
const err = e as Error;
console.error(err.stack);
neovimIDE.handleCommandError(err);
}
throw e;
}
},
["cursorless.repeatPreviousCommand"]: dummyCommandHandler,
// Cheatsheet commands
["cursorless.showCheatsheet"]: dummyCommandHandler,
["cursorless.internal.updateCheatsheetDefaults"]: dummyCommandHandler,
// Testcase recorder commands
["cursorless.recordTestCase"]: dummyCommandHandler,
["cursorless.recordOneTestCaseThenPause"]: dummyCommandHandler,
["cursorless.pauseRecording"]: dummyCommandHandler,
["cursorless.resumeRecording"]: dummyCommandHandler,
["cursorless.takeSnapshot"]: dummyCommandHandler,
// Scope test recorder commands
["cursorless.recordScopeTests.showUnimplementedFacets"]:
dummyCommandHandler,
["cursorless.recordScopeTests.saveActiveDocument"]: dummyCommandHandler,
// Other commands
["cursorless.showQuickPick"]: dummyCommandHandler,
["cursorless.showDocumentation"]: dummyCommandHandler,
["cursorless.showInstallationDependencies"]: dummyCommandHandler,
["cursorless.private.logQuickActions"]: dummyCommandHandler,
// Hats
["cursorless.toggleDecorations"]: dummyCommandHandler,
["cursorless.recomputeDecorationStyles"]: dummyCommandHandler,
// Scope visualizer
["cursorless.showScopeVisualizer"]: dummyCommandHandler,
["cursorless.hideScopeVisualizer"]: dummyCommandHandler,
["cursorless.scopeVisualizer.openUrl"]: dummyCommandHandler,
// Command history
["cursorless.analyzeCommandHistory"]: dummyCommandHandler,
// General keyboard commands
["cursorless.keyboard.escape"]: dummyCommandHandler,
// Targeted keyboard commands
["cursorless.keyboard.targeted.targetHat"]: dummyCommandHandler,
["cursorless.keyboard.targeted.targetScope"]: dummyCommandHandler,
["cursorless.keyboard.targeted.targetSelection"]: dummyCommandHandler,
["cursorless.keyboard.targeted.clearTarget"]: dummyCommandHandler,
["cursorless.keyboard.targeted.runActionOnTarget"]: dummyCommandHandler,
// Modal keyboard commands
["cursorless.keyboard.modal.modeOn"]: dummyCommandHandler,
["cursorless.keyboard.modal.modeOff"]: dummyCommandHandler,
["cursorless.keyboard.modal.modeToggle"]: dummyCommandHandler,
["cursorless.keyboard.undoTarget"]: dummyCommandHandler,
["cursorless.keyboard.redoTarget"]: dummyCommandHandler,
// Tutorial commands
["cursorless.tutorial.start"]: dummyCommandHandler,
["cursorless.tutorial.next"]: dummyCommandHandler,
["cursorless.tutorial.previous"]: dummyCommandHandler,
["cursorless.tutorial.restart"]: dummyCommandHandler,
["cursorless.tutorial.resume"]: dummyCommandHandler,
["cursorless.tutorial.list"]: dummyCommandHandler,
["cursorless.documentationOpened"]: dummyCommandHandler,
};
Object.entries(commands).forEach(([commandId, callback]) =>
getNeovimRegistry().registerCommand(commandId, callback),
);
}
export async function dummyCommandHandler(...args: any[]) {
console.debug(`dummyCommandHandler(): args=${args}`);
}