Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

keybindings.ts

410 lines11.6 KB

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