Open History to see the last commit
1import {
2 type KeybindingCommand,
3 type KeybindingShortcut,
4 type KeybindingWhenNode,
5 type ResolvedKeybindingsConfig,
6 THREAD_JUMP_KEYBINDING_COMMANDS,
7 type ThreadJumpKeybindingCommand,
8} from "@workbench/contracts";
9import { isMacPlatform } from "./lib/utils";
10
11export interface ShortcutEventLike {
12 type?: string;
13 code?: string;
14 key: string;
15 metaKey: boolean;
16 ctrlKey: boolean;
17 shiftKey: boolean;
18 altKey: boolean;
19}
20
21export interface ShortcutMatchContext {
22 terminalFocus: boolean;
23 terminalOpen: boolean;
24 [key: string]: boolean;
25}
26
27interface ShortcutMatchOptions {
28 platform?: string;
29 context?: Partial<ShortcutMatchContext>;
30}
31
32interface ResolvedShortcutLabelOptions extends ShortcutMatchOptions {
33 platform?: string;
34}
35
36const TERMINAL_WORD_BACKWARD = "\u001bb";
37const TERMINAL_WORD_FORWARD = "\u001bf";
38const TERMINAL_LINE_START = "\u0001";
39const TERMINAL_LINE_END = "\u0005";
40const TERMINAL_DELETE_TO_LINE_START = "\u0015";
41const EVENT_CODE_KEY_ALIASES: Readonly<Record<string, readonly string[]>> = {
42 BracketLeft: ["["],
43 BracketRight: ["]"],
44 Digit0: ["0"],
45 Digit1: ["1"],
46 Digit2: ["2"],
47 Digit3: ["3"],
48 Digit4: ["4"],
49 Digit5: ["5"],
50 Digit6: ["6"],
51 Digit7: ["7"],
52 Digit8: ["8"],
53 Digit9: ["9"],
54};
55
56function normalizeEventKey(key: string): string {
57 const normalized = key.toLowerCase();
58 if (normalized === "esc") return "escape";
59 return normalized;
60}
61
62function resolveEventKeys(event: ShortcutEventLike): Set<string> {
63 const keys = new Set([normalizeEventKey(event.key)]);
64 const aliases = event.code ? EVENT_CODE_KEY_ALIASES[event.code] : undefined;
65 if (!aliases) return keys;
66
67 for (const alias of aliases) {
68 keys.add(alias);
69 }
70 return keys;
71}
72
73function matchesShortcutModifiers(
74 event: ShortcutEventLike,
75 shortcut: KeybindingShortcut,
76 platform = navigator.platform,
77): boolean {
78 const useMetaForMod = isMacPlatform(platform);
79 const expectedMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod);
80 const expectedCtrl = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod);
81 return (
82 event.metaKey === expectedMeta &&
83 event.ctrlKey === expectedCtrl &&
84 event.shiftKey === shortcut.shiftKey &&
85 event.altKey === shortcut.altKey
86 );
87}
88
89function matchesShortcut(
90 event: ShortcutEventLike,
91 shortcut: KeybindingShortcut,
92 platform = navigator.platform,
93): boolean {
94 if (!matchesShortcutModifiers(event, shortcut, platform)) return false;
95 return resolveEventKeys(event).has(shortcut.key);
96}
97
98function resolvePlatform(options: ShortcutMatchOptions | undefined): string {
99 return options?.platform ?? navigator.platform;
100}
101
102function resolveContext(options: ShortcutMatchOptions | undefined): ShortcutMatchContext {
103 return {
104 terminalFocus: false,
105 terminalOpen: false,
106 ...options?.context,
107 };
108}
109
110function evaluateWhenNode(node: KeybindingWhenNode, context: ShortcutMatchContext): boolean {
111 switch (node.type) {
112 case "identifier":
113 if (node.name === "true") return true;
114 if (node.name === "false") return false;
115 return Boolean(context[node.name]);
116 case "not":
117 return !evaluateWhenNode(node.node, context);
118 case "and":
119 return evaluateWhenNode(node.left, context) && evaluateWhenNode(node.right, context);
120 case "or":
121 return evaluateWhenNode(node.left, context) || evaluateWhenNode(node.right, context);
122 }
123}
124
125function matchesWhenClause(
126 whenAst: KeybindingWhenNode | undefined,
127 context: ShortcutMatchContext,
128): boolean {
129 if (!whenAst) return true;
130 return evaluateWhenNode(whenAst, context);
131}
132
133function shortcutConflictKey(shortcut: KeybindingShortcut, platform = navigator.platform): string {
134 const useMetaForMod = isMacPlatform(platform);
135 const metaKey = shortcut.metaKey || (shortcut.modKey && useMetaForMod);
136 const ctrlKey = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod);
137
138 return [
139 shortcut.key,
140 metaKey ? "meta" : "",
141 ctrlKey ? "ctrl" : "",
142 shortcut.shiftKey ? "shift" : "",
143 shortcut.altKey ? "alt" : "",
144 ].join("|");
145}
146
147function findEffectiveShortcutForCommand(
148 keybindings: ResolvedKeybindingsConfig,
149 command: KeybindingCommand,
150 options?: ShortcutMatchOptions,
151): KeybindingShortcut | null {
152 const platform = resolvePlatform(options);
153 const context = resolveContext(options);
154 const claimedShortcuts = new Set<string>();
155
156 for (let index = keybindings.length - 1; index >= 0; index -= 1) {
157 const binding = keybindings[index];
158 if (!binding) continue;
159 if (!matchesWhenClause(binding.whenAst, context)) continue;
160
161 const conflictKey = shortcutConflictKey(binding.shortcut, platform);
162 if (claimedShortcuts.has(conflictKey)) {
163 continue;
164 }
165
166 claimedShortcuts.add(conflictKey);
167 if (binding.command === command) {
168 return binding.shortcut;
169 }
170 }
171
172 return null;
173}
174
175function matchesCommandShortcut(
176 event: ShortcutEventLike,
177 keybindings: ResolvedKeybindingsConfig,
178 command: KeybindingCommand,
179 options?: ShortcutMatchOptions,
180): boolean {
181 return resolveShortcutCommand(event, keybindings, options) === command;
182}
183
184export function resolveShortcutCommand(
185 event: ShortcutEventLike,
186 keybindings: ResolvedKeybindingsConfig,
187 options?: ShortcutMatchOptions,
188): KeybindingCommand | null {
189 const platform = resolvePlatform(options);
190 const context = resolveContext(options);
191
192 for (let index = keybindings.length - 1; index >= 0; index -= 1) {
193 const binding = keybindings[index];
194 if (!binding) continue;
195 if (!matchesWhenClause(binding.whenAst, context)) continue;
196 if (!matchesShortcut(event, binding.shortcut, platform)) continue;
197 return binding.command;
198 }
199 return null;
200}
201
202function formatShortcutKeyLabel(key: string): string {
203 if (key === " ") return "Space";
204 if (key.length === 1) return key.toUpperCase();
205 if (key === "escape") return "Esc";
206 if (key === "arrowup") return "Up";
207 if (key === "arrowdown") return "Down";
208 if (key === "arrowleft") return "Left";
209 if (key === "arrowright") return "Right";
210 return key.slice(0, 1).toUpperCase() + key.slice(1);
211}
212
213export function formatShortcutLabel(
214 shortcut: KeybindingShortcut,
215 platform = navigator.platform,
216): string {
217 const keyLabel = formatShortcutKeyLabel(shortcut.key);
218 const useMetaForMod = isMacPlatform(platform);
219 const showMeta = shortcut.metaKey || (shortcut.modKey && useMetaForMod);
220 const showCtrl = shortcut.ctrlKey || (shortcut.modKey && !useMetaForMod);
221 const showAlt = shortcut.altKey;
222 const showShift = shortcut.shiftKey;
223
224 if (useMetaForMod) {
225 return `${showCtrl ? "\u2303" : ""}${showAlt ? "\u2325" : ""}${showShift ? "\u21e7" : ""}${showMeta ? "\u2318" : ""}${keyLabel}`;
226 }
227
228 const parts: string[] = [];
229 if (showCtrl) parts.push("Ctrl");
230 if (showAlt) parts.push("Alt");
231 if (showShift) parts.push("Shift");
232 if (showMeta) parts.push("Meta");
233 parts.push(keyLabel);
234 return parts.join("+");
235}
236
237export function shortcutLabelForCommand(
238 keybindings: ResolvedKeybindingsConfig,
239 command: KeybindingCommand,
240 options?: string | ResolvedShortcutLabelOptions,
241): string | null {
242 const resolvedOptions =
243 typeof options === "string"
244 ? ({ platform: options } satisfies ResolvedShortcutLabelOptions)
245 : options;
246 const platform = resolvePlatform(resolvedOptions);
247 const shortcut = findEffectiveShortcutForCommand(keybindings, command, resolvedOptions);
248 return shortcut ? formatShortcutLabel(shortcut, platform) : null;
249}
250
251export function threadJumpCommandForIndex(index: number): ThreadJumpKeybindingCommand | null {
252 return THREAD_JUMP_KEYBINDING_COMMANDS[index] ?? null;
253}
254
255export function threadJumpIndexFromCommand(command: string): number | null {
256 const index = THREAD_JUMP_KEYBINDING_COMMANDS.indexOf(command as ThreadJumpKeybindingCommand);
257 return index === -1 ? null : index;
258}
259
260export function threadTraversalDirectionFromCommand(
261 command: string | null,
262): "previous" | "next" | null {
263 if (command === "thread.previous") return "previous";
264 if (command === "thread.next") return "next";
265 return null;
266}
267
268export function shouldShowThreadJumpHints(
269 event: ShortcutEventLike,
270 keybindings: ResolvedKeybindingsConfig,
271 options?: ShortcutMatchOptions,
272): boolean {
273 const platform = resolvePlatform(options);
274
275 for (const command of THREAD_JUMP_KEYBINDING_COMMANDS) {
276 const shortcut = findEffectiveShortcutForCommand(keybindings, command, options);
277 if (!shortcut) continue;
278 if (matchesShortcutModifiers(event, shortcut, platform)) {
279 return true;
280 }
281 }
282
283 return false;
284}
285
286export function isTerminalToggleShortcut(
287 event: ShortcutEventLike,
288 keybindings: ResolvedKeybindingsConfig,
289 options?: ShortcutMatchOptions,
290): boolean {
291 return matchesCommandShortcut(event, keybindings, "terminal.toggle", options);
292}
293
294export function isTerminalSplitShortcut(
295 event: ShortcutEventLike,
296 keybindings: ResolvedKeybindingsConfig,
297 options?: ShortcutMatchOptions,
298): boolean {
299 return matchesCommandShortcut(event, keybindings, "terminal.split", options);
300}
301
302export function isTerminalNewShortcut(
303 event: ShortcutEventLike,
304 keybindings: ResolvedKeybindingsConfig,
305 options?: ShortcutMatchOptions,
306): boolean {
307 return matchesCommandShortcut(event, keybindings, "terminal.new", options);
308}
309
310export function isTerminalCloseShortcut(
311 event: ShortcutEventLike,
312 keybindings: ResolvedKeybindingsConfig,
313 options?: ShortcutMatchOptions,
314): boolean {
315 return matchesCommandShortcut(event, keybindings, "terminal.close", options);
316}
317
318export function isDiffToggleShortcut(
319 event: ShortcutEventLike,
320 keybindings: ResolvedKeybindingsConfig,
321 options?: ShortcutMatchOptions,
322): boolean {
323 return matchesCommandShortcut(event, keybindings, "diff.toggle", options);
324}
325
326export function isChatNewShortcut(
327 event: ShortcutEventLike,
328 keybindings: ResolvedKeybindingsConfig,
329 options?: ShortcutMatchOptions,
330): boolean {
331 return matchesCommandShortcut(event, keybindings, "chat.new", options);
332}
333
334export function isChatNewLocalShortcut(
335 event: ShortcutEventLike,
336 keybindings: ResolvedKeybindingsConfig,
337 options?: ShortcutMatchOptions,
338): boolean {
339 return matchesCommandShortcut(event, keybindings, "chat.newLocal", options);
340}
341
342export function isOpenFavoriteEditorShortcut(
343 event: ShortcutEventLike,
344 keybindings: ResolvedKeybindingsConfig,
345 options?: ShortcutMatchOptions,
346): boolean {
347 return matchesCommandShortcut(event, keybindings, "editor.openFavorite", options);
348}
349
350export function isTerminalClearShortcut(
351 event: ShortcutEventLike,
352 platform = navigator.platform,
353): boolean {
354 if (event.type !== undefined && event.type !== "keydown") {
355 return false;
356 }
357
358 const key = event.key.toLowerCase();
359
360 if (key === "l" && event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey) {
361 return true;
362 }
363
364 return (
365 isMacPlatform(platform) &&
366 key === "k" &&
367 event.metaKey &&
368 !event.ctrlKey &&
369 !event.altKey &&
370 !event.shiftKey
371 );
372}
373
374export function terminalDeleteShortcutData(
375 event: ShortcutEventLike,
376 platform = navigator.platform,
377): string | null {
378 if (event.type !== undefined && event.type !== "keydown") {
379 return null;
380 }
381
382 if (!isMacPlatform(platform)) {
383 return null;
384 }
385
386 const key = normalizeEventKey(event.key);
387 if (key !== "backspace") {
388 return null;
389 }
390
391 return event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey
392 ? TERMINAL_DELETE_TO_LINE_START
393 : null;
394}
395
396export function terminalNavigationShortcutData(
397 event: ShortcutEventLike,
398 platform = navigator.platform,
399): string | null {
400 if (event.type !== undefined && event.type !== "keydown") {
401 return null;
402 }
403
404 if (event.shiftKey) return null;
405
406 const key = normalizeEventKey(event.key);
407 if (key !== "arrowleft" && key !== "arrowright") {
408 return null;
409 }
410
411 const moveWord = key === "arrowleft" ? TERMINAL_WORD_BACKWARD : TERMINAL_WORD_FORWARD;
412 const moveLine = key === "arrowleft" ? TERMINAL_LINE_START : TERMINAL_LINE_END;
413
414 if (isMacPlatform(platform)) {
415 if (event.altKey && !event.metaKey && !event.ctrlKey) {
416 return moveWord;
417 }
418 if (event.metaKey && !event.altKey && !event.ctrlKey) {
419 return moveLine;
420 }
421 return null;
422 }
423
424 if (event.ctrlKey && !event.metaKey && !event.altKey) {
425 return moveWord;
426 }
427
428 if (event.altKey && !event.metaKey && !event.ctrlKey) {
429 return moveWord;
430 }
431
432 return null;
433}