-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathkeybindings.ts
More file actions
328 lines (282 loc) · 9.14 KB
/
keybindings.ts
File metadata and controls
328 lines (282 loc) · 9.14 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import {
type KeybindingCommand,
type KeybindingShortcut,
type KeybindingWhenNode,
type ResolvedKeybindingsConfig,
} from "@okcode/contracts";
import { isMacPlatform } from "./lib/utils";
export interface ShortcutEventLike {
type?: string;
key: string;
metaKey: boolean;
ctrlKey: boolean;
shiftKey: boolean;
altKey: boolean;
}
export interface ShortcutMatchContext {
terminalFocus: boolean;
terminalOpen: boolean;
[key: string]: boolean;
}
interface ShortcutMatchOptions {
platform?: string;
context?: Partial<ShortcutMatchContext>;
}
const TERMINAL_WORD_BACKWARD = "\u001bb";
const TERMINAL_WORD_FORWARD = "\u001bf";
const TERMINAL_LINE_START = "\u0001";
const TERMINAL_LINE_END = "\u0005";
function normalizeEventKey(key: string): string {
const normalized = key.toLowerCase();
if (normalized === "esc") return "escape";
return normalized;
}
function matchesShortcut(
event: ShortcutEventLike,
shortcut: KeybindingShortcut,
platform = navigator.platform,
): boolean {
const key = normalizeEventKey(event.key);
if (key !== shortcut.key) return false;
const useMetaForMod = isMacPlatform(platform);
const expectedMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod);
const expectedCtrl = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod);
return (
event.metaKey === expectedMeta &&
event.ctrlKey === expectedCtrl &&
event.shiftKey === shortcut.shiftKey &&
event.altKey === shortcut.altKey
);
}
function resolvePlatform(options: ShortcutMatchOptions | undefined): string {
return options?.platform ?? navigator.platform;
}
function resolveContext(options: ShortcutMatchOptions | undefined): ShortcutMatchContext {
return {
terminalFocus: false,
terminalOpen: false,
...options?.context,
};
}
function evaluateWhenNode(node: KeybindingWhenNode, context: ShortcutMatchContext): boolean {
switch (node.type) {
case "identifier":
if (node.name === "true") return true;
if (node.name === "false") return false;
return Boolean(context[node.name]);
case "not":
return !evaluateWhenNode(node.node, context);
case "and":
return evaluateWhenNode(node.left, context) && evaluateWhenNode(node.right, context);
case "or":
return evaluateWhenNode(node.left, context) || evaluateWhenNode(node.right, context);
}
}
function matchesWhenClause(
whenAst: KeybindingWhenNode | undefined,
context: ShortcutMatchContext,
): boolean {
if (!whenAst) return true;
return evaluateWhenNode(whenAst, context);
}
function matchesCommandShortcut(
event: ShortcutEventLike,
keybindings: ResolvedKeybindingsConfig,
command: KeybindingCommand,
options?: ShortcutMatchOptions,
): boolean {
return resolveShortcutCommand(event, keybindings, options) === command;
}
export function resolveShortcutCommand(
event: ShortcutEventLike,
keybindings: ResolvedKeybindingsConfig,
options?: ShortcutMatchOptions,
): string | null {
const platform = resolvePlatform(options);
const context = resolveContext(options);
for (let index = keybindings.length - 1; index >= 0; index -= 1) {
const binding = keybindings[index];
if (!binding) continue;
if (!matchesWhenClause(binding.whenAst, context)) continue;
if (!matchesShortcut(event, binding.shortcut, platform)) continue;
return binding.command;
}
return null;
}
function formatShortcutKeyLabel(key: string): string {
if (key === " ") return "Space";
if (key.length === 1) return key.toUpperCase();
if (key === "escape") return "Esc";
if (key === "arrowup") return "Up";
if (key === "arrowdown") return "Down";
if (key === "arrowleft") return "Left";
if (key === "arrowright") return "Right";
return key.slice(0, 1).toUpperCase() + key.slice(1);
}
export function formatShortcutLabel(
shortcut: KeybindingShortcut,
platform = navigator.platform,
): string {
const keyLabel = formatShortcutKeyLabel(shortcut.key);
const useMetaForMod = isMacPlatform(platform);
const showMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod);
const showCtrl = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod);
const showAlt = shortcut.altKey;
const showShift = shortcut.shiftKey;
if (useMetaForMod) {
return `${showCtrl ? "\u2303" : ""}${showAlt ? "\u2325" : ""}${showShift ? "\u21e7" : ""}${showMeta ? "\u2318" : ""}${keyLabel}`;
}
const parts: string[] = [];
if (showCtrl) parts.push("Ctrl");
if (showAlt) parts.push("Alt");
if (showShift) parts.push("Shift");
if (showMeta) parts.push("Meta");
parts.push(keyLabel);
return parts.join("+");
}
export function shortcutLabelForCommand(
keybindings: ResolvedKeybindingsConfig,
command: KeybindingCommand,
platform = navigator.platform,
): string | null {
const labels = shortcutLabelsForCommand(keybindings, command, platform);
return labels[labels.length - 1] ?? null;
}
export function shortcutLabelsForCommand(
keybindings: ResolvedKeybindingsConfig,
command: KeybindingCommand,
platform = navigator.platform,
): string[] {
const labels: string[] = [];
const seenLabels = new Set<string>();
for (const binding of keybindings) {
if (!binding || binding.command !== command) continue;
const label = formatShortcutLabel(binding.shortcut, platform);
if (seenLabels.has(label)) continue;
seenLabels.add(label);
labels.push(label);
}
return labels;
}
export function isTerminalToggleShortcut(
event: ShortcutEventLike,
keybindings: ResolvedKeybindingsConfig,
options?: ShortcutMatchOptions,
): boolean {
return matchesCommandShortcut(event, keybindings, "terminal.toggle", options);
}
export function isTerminalSplitShortcut(
event: ShortcutEventLike,
keybindings: ResolvedKeybindingsConfig,
options?: ShortcutMatchOptions,
): boolean {
return matchesCommandShortcut(event, keybindings, "terminal.split", options);
}
export function isTerminalNewShortcut(
event: ShortcutEventLike,
keybindings: ResolvedKeybindingsConfig,
options?: ShortcutMatchOptions,
): boolean {
return matchesCommandShortcut(event, keybindings, "terminal.new", options);
}
export function isTerminalCloseShortcut(
event: ShortcutEventLike,
keybindings: ResolvedKeybindingsConfig,
options?: ShortcutMatchOptions,
): boolean {
return matchesCommandShortcut(event, keybindings, "terminal.close", options);
}
export function isDiffToggleShortcut(
event: ShortcutEventLike,
keybindings: ResolvedKeybindingsConfig,
options?: ShortcutMatchOptions,
): boolean {
return matchesCommandShortcut(event, keybindings, "diff.toggle", options);
}
export function isChatNewShortcut(
event: ShortcutEventLike,
keybindings: ResolvedKeybindingsConfig,
options?: ShortcutMatchOptions,
): boolean {
return matchesCommandShortcut(event, keybindings, "chat.new", options);
}
export function isChatNewLocalShortcut(
event: ShortcutEventLike,
keybindings: ResolvedKeybindingsConfig,
options?: ShortcutMatchOptions,
): boolean {
return matchesCommandShortcut(event, keybindings, "chat.newLocal", options);
}
export function isOpenFavoriteEditorShortcut(
event: ShortcutEventLike,
keybindings: ResolvedKeybindingsConfig,
options?: ShortcutMatchOptions,
): boolean {
return matchesCommandShortcut(event, keybindings, "editor.openFavorite", options);
}
export function isTerminalClearShortcut(
event: ShortcutEventLike,
platform = navigator.platform,
): boolean {
if (event.type !== undefined && event.type !== "keydown") {
return false;
}
const key = event.key.toLowerCase();
if (key === "l" && event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) {
return true;
}
return (
isMacPlatform(platform) &&
key === "k" &&
event.metaKey &&
!event.ctrlKey &&
!event.altKey &&
!event.shiftKey
);
}
export function isTerminalAddToChatShortcut(
event: ShortcutEventLike,
platform = navigator.platform,
): boolean {
if (event.type !== undefined && event.type !== "keydown") {
return false;
}
const key = event.key.toLowerCase();
// Cmd+L on Mac
if (isMacPlatform(platform)) {
return key === "l" && event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey;
}
// Ctrl+Shift+L on non-Mac (Ctrl+L is terminal clear)
return key === "l" && event.ctrlKey && event.shiftKey && !event.metaKey && !event.altKey;
}
export function terminalNavigationShortcutData(
event: ShortcutEventLike,
platform = navigator.platform,
): string | null {
if (event.type !== undefined && event.type !== "keydown") {
return null;
}
if (event.shiftKey) return null;
const key = normalizeEventKey(event.key);
if (key !== "arrowleft" && key !== "arrowright") {
return null;
}
const moveWord = key === "arrowleft" ? TERMINAL_WORD_BACKWARD : TERMINAL_WORD_FORWARD;
const moveLine = key === "arrowleft" ? TERMINAL_LINE_START : TERMINAL_LINE_END;
if (isMacPlatform(platform)) {
if (event.altKey && !event.metaKey && !event.ctrlKey) {
return moveWord;
}
if (event.metaKey && !event.altKey && !event.ctrlKey) {
return moveLine;
}
return null;
}
if (event.ctrlKey && !event.metaKey && !event.altKey) {
return moveWord;
}
if (event.altKey && !event.metaKey && !event.ctrlKey) {
return moveWord;
}
return null;
}