Open History to see the last commit
1import { assert, describe, it } from "vite-plus/test";
2import {
3 compileResolvedKeybindingsConfig,
4 DEFAULT_RESOLVED_KEYBINDINGS,
5 mergeWithDefaultKeybindings,
6} from "@t3tools/shared/keybindings";
7
8import {
9 type KeybindingCommand,
10 type KeybindingShortcut,
11 type KeybindingWhenNode,
12 type ResolvedKeybindingsConfig,
13} from "@t3tools/contracts";
14import {
15 effectiveShortcutsForCommand,
16 formatShortcutLabel,
17 isRichTextBoldShortcut,
18 isSidebarToggleShortcut,
19 isTerminalNewShortcut,
20 isTerminalSplitShortcut,
21 resolveChatShortcutCommand,
22 resolveShortcutCommand,
23 shouldShowThreadJumpHintsForModifiers,
24 shortcutLabelForCommand,
25 terminalNavigationShortcutData,
26 type ShortcutEventLike,
27} from "./keybindings";
28
29function event(overrides: Partial<ShortcutEventLike> = {}): ShortcutEventLike {
30 return {
31 key: "j",
32 metaKey: false,
33 ctrlKey: false,
34 shiftKey: false,
35 altKey: false,
36 ...overrides,
37 };
38}
39
40describe("terminal word navigation", () => {
41 it.each(["Win32", "Linux x86_64"])(
42 "leaves Ctrl+Arrow to the terminal encoder on %s",
43 (platform) => {
44 for (const key of ["ArrowLeft", "ArrowRight"]) {
45 assert.equal(terminalNavigationShortcutData(event({ key, ctrlKey: true }), platform), null);
46 }
47 },
48 );
49
50 it.each(["Win32", "Linux x86_64", "MacIntel"])(
51 "preserves Alt+Arrow word navigation on %s",
52 (platform) => {
53 assert.equal(
54 terminalNavigationShortcutData(event({ key: "ArrowLeft", altKey: true }), platform),
55 "\u001bb",
56 );
57 assert.equal(
58 terminalNavigationShortcutData(event({ key: "ArrowRight", altKey: true }), platform),
59 "\u001bf",
60 );
61 },
62 );
63});
64
65function modShortcut(
66 key: string,
67 overrides: Partial<Omit<KeybindingShortcut, "key">> = {},
68): KeybindingShortcut {
69 return {
70 key,
71 metaKey: false,
72 ctrlKey: false,
73 shiftKey: false,
74 altKey: false,
75 modKey: true,
76 ...overrides,
77 };
78}
79
80function whenIdentifier(name: string): KeybindingWhenNode {
81 return { type: "identifier", name };
82}
83
84function whenNot(node: KeybindingWhenNode): KeybindingWhenNode {
85 return { type: "not", node };
86}
87
88function whenAnd(left: KeybindingWhenNode, right: KeybindingWhenNode): KeybindingWhenNode {
89 return { type: "and", left, right };
90}
91
92interface TestBinding {
93 shortcut: KeybindingShortcut;
94 command: KeybindingCommand;
95 whenAst?: KeybindingWhenNode;
96}
97
98function compile(bindings: TestBinding[]): ResolvedKeybindingsConfig {
99 return bindings.map((binding) => ({
100 command: binding.command,
101 shortcut: binding.shortcut,
102 ...(binding.whenAst ? { whenAst: binding.whenAst } : {}),
103 }));
104}
105
106const DEFAULT_BINDINGS = compile([
107 { shortcut: modShortcut("b"), command: "sidebar.toggle" },
108 { shortcut: modShortcut("j"), command: "terminal.toggle" },
109 { shortcut: modShortcut("b", { altKey: true }), command: "rightPanel.toggle" },
110 {
111 shortcut: modShortcut("d"),
112 command: "terminal.split",
113 whenAst: whenIdentifier("terminalFocus"),
114 },
115 {
116 shortcut: modShortcut("d", { shiftKey: true }),
117 command: "terminal.splitVertical",
118 whenAst: whenIdentifier("terminalFocus"),
119 },
120 {
121 shortcut: modShortcut("n"),
122 command: "terminal.new",
123 whenAst: whenIdentifier("terminalFocus"),
124 },
125 {
126 shortcut: modShortcut("w"),
127 command: "terminal.close",
128 whenAst: whenIdentifier("terminalFocus"),
129 },
130 {
131 shortcut: modShortcut("w"),
132 command: "rightPanel.close",
133 whenAst: whenNot(whenIdentifier("terminalFocus")),
134 },
135 {
136 shortcut: modShortcut("d"),
137 command: "diff.toggle",
138 whenAst: whenNot(whenIdentifier("terminalFocus")),
139 },
140 {
141 shortcut: modShortcut("k"),
142 command: "commandPalette.toggle",
143 whenAst: whenNot(whenIdentifier("terminalFocus")),
144 },
145 {
146 shortcut: modShortcut("p"),
147 command: "filePicker.toggle",
148 whenAst: whenNot(whenIdentifier("terminalFocus")),
149 },
150 {
151 shortcut: modShortcut("f", { shiftKey: true }),
152 command: "projectSearch.toggle",
153 whenAst: whenNot(whenIdentifier("terminalFocus")),
154 },
155 {
156 shortcut: modShortcut("t", { altKey: true, shiftKey: true }),
157 command: "themeEditor.toggle",
158 },
159 {
160 shortcut: modShortcut("f"),
161 command: "chat.find",
162 whenAst: whenAnd(
163 whenNot(whenIdentifier("terminalFocus")),
164 whenNot(whenIdentifier("previewFocus")),
165 ),
166 },
167 {
168 shortcut: modShortcut("m", { shiftKey: true }),
169 command: "modelPicker.toggle",
170 whenAst: whenNot(whenIdentifier("terminalFocus")),
171 },
172 { shortcut: modShortcut("o", { shiftKey: true }), command: "chat.new" },
173 { shortcut: modShortcut("n", { shiftKey: true }), command: "chat.newLocal" },
174 { shortcut: modShortcut("o"), command: "editor.openFavorite" },
175 { shortcut: modShortcut("[", { shiftKey: true }), command: "thread.previous" },
176 { shortcut: modShortcut("]", { shiftKey: true }), command: "thread.next" },
177 {
178 shortcut: modShortcut("c", { shiftKey: true }),
179 command: "thread.copyReference",
180 whenAst: whenNot(whenIdentifier("terminalFocus")),
181 },
182 {
183 shortcut: modShortcut("s", { shiftKey: true }),
184 command: "thread.settle",
185 whenAst: whenNot(whenIdentifier("terminalFocus")),
186 },
187 { shortcut: modShortcut("1"), command: "thread.jump.1" },
188 { shortcut: modShortcut("2"), command: "thread.jump.2" },
189 { shortcut: modShortcut("3"), command: "thread.jump.3" },
190 {
191 shortcut: modShortcut("1"),
192 command: "modelPicker.jump.1",
193 whenAst: whenIdentifier("modelPickerOpen"),
194 },
195 {
196 shortcut: modShortcut("2"),
197 command: "modelPicker.jump.2",
198 whenAst: whenIdentifier("modelPickerOpen"),
199 },
200 {
201 shortcut: modShortcut("3"),
202 command: "modelPicker.jump.3",
203 whenAst: whenIdentifier("modelPickerOpen"),
204 },
205]);
206
207describe("effectiveShortcutsForCommand", () => {
208 it("passes only effective preview shortcuts to the desktop bridge", () => {
209 const reopen = modShortcut("t", { shiftKey: true });
210 const second = modShortcut("y", { shiftKey: true });
211 const keybindings = compile([
212 { shortcut: reopen, command: "view.reopenClosed" },
213 {
214 shortcut: second,
215 command: "view.reopenClosed",
216 whenAst: whenIdentifier("previewFocus"),
217 },
218 {
219 shortcut: reopen,
220 command: "preview.toggle",
221 whenAst: whenIdentifier("previewFocus"),
222 },
223 ]);
224 assert.deepEqual(
225 effectiveShortcutsForCommand(keybindings, "view.reopenClosed", {
226 platform: "MacIntel",
227 context: { previewFocus: true, previewOpen: true },
228 }),
229 [second],
230 );
231 assert.deepEqual(
232 effectiveShortcutsForCommand(keybindings, "view.reopenClosed", {
233 platform: "MacIntel",
234 context: { previewFocus: false },
235 }),
236 [reopen],
237 );
238 });
239});
240
241describe("settle thread shortcut", () => {
242 it("does not intercept the terminal", () => {
243 assert.isNull(
244 resolveShortcutCommand(event({ key: "s", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
245 platform: "Win32",
246 context: { terminalFocus: true },
247 }),
248 );
249 });
250});
251
252describe("thread undo shortcut", () => {
253 it("leaves native undo alone inside text fields and terminals", () => {
254 assert.isNull(
255 resolveShortcutCommand(event({ key: "z", ctrlKey: true }), DEFAULT_RESOLVED_KEYBINDINGS, {
256 platform: "Win32",
257 context: { editableFocus: true },
258 }),
259 );
260 assert.isNull(
261 resolveShortcutCommand(event({ key: "z", ctrlKey: true }), DEFAULT_RESOLVED_KEYBINDINGS, {
262 platform: "Win32",
263 context: { terminalFocus: true },
264 }),
265 );
266 });
267});
268
269describe("split/new/close terminal shortcuts", () => {
270 it("supports when expressions", () => {
271 const keybindings = compile([
272 {
273 shortcut: modShortcut("\\"),
274 command: "terminal.split",
275 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
276 },
277 {
278 shortcut: modShortcut("n", { shiftKey: true }),
279 command: "terminal.new",
280 whenAst: whenAnd(whenIdentifier("terminalOpen"), whenNot(whenIdentifier("terminalFocus"))),
281 },
282 { shortcut: modShortcut("j"), command: "terminal.toggle" },
283 ]);
284 assert.isTrue(
285 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
286 platform: "Win32",
287 context: { terminalOpen: true, terminalFocus: false },
288 }),
289 );
290 assert.isFalse(
291 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
292 platform: "Win32",
293 context: { terminalOpen: false, terminalFocus: false },
294 }),
295 );
296 assert.isTrue(
297 isTerminalNewShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), keybindings, {
298 platform: "Win32",
299 context: { terminalOpen: true, terminalFocus: false },
300 }),
301 );
302 });
303});
304
305describe("shortcutLabelForCommand", () => {
306 it("returns null for commands shadowed by a later conflicting shortcut", () => {
307 const bindings = compile([
308 { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.1" },
309 { shortcut: modShortcut("1", { shiftKey: true }), command: "thread.jump.7" },
310 ]);
311
312 assert.isNull(shortcutLabelForCommand(bindings, "thread.jump.1", "MacIntel"));
313 assert.strictEqual(shortcutLabelForCommand(bindings, "thread.jump.7", "MacIntel"), "⇧⌘1");
314 });
315
316 it("respects when-context while resolving labels", () => {
317 const bindings = compile([
318 { shortcut: modShortcut("d"), command: "diff.toggle" },
319 {
320 shortcut: modShortcut("d"),
321 command: "terminal.split",
322 whenAst: whenIdentifier("terminalFocus"),
323 },
324 ]);
325
326 assert.strictEqual(
327 shortcutLabelForCommand(bindings, "diff.toggle", {
328 platform: "Linux",
329 context: { terminalFocus: false },
330 }),
331 "Ctrl+D",
332 );
333 assert.isNull(
334 shortcutLabelForCommand(bindings, "diff.toggle", {
335 platform: "Linux",
336 context: { terminalFocus: true },
337 }),
338 );
339 assert.strictEqual(
340 shortcutLabelForCommand(bindings, "terminal.split", {
341 platform: "Linux",
342 context: { terminalFocus: true },
343 }),
344 "Ctrl+D",
345 );
346 });
347});
348
349describe("thread navigation helpers", () => {
350 it("never shows jump hints while the terminal is focused, even with an unrestricted binding", () => {
351 assert.isFalse(
352 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
353 platform: "MacIntel",
354 context: { terminalFocus: true },
355 }),
356 );
357 assert.isTrue(
358 shouldShowThreadJumpHintsForModifiers(event({ metaKey: true }), DEFAULT_BINDINGS, {
359 platform: "MacIntel",
360 context: { terminalFocus: false },
361 }),
362 );
363 });
364
365 it("keeps default thread jumps off the web so the browser can switch tabs", () => {
366 const input = event({ key: "1", metaKey: true });
367 assert.isNull(
368 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
369 platform: "MacIntel",
370 context: { isDesktop: false },
371 }),
372 );
373 assert.strictEqual(
374 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
375 platform: "MacIntel",
376 context: { isDesktop: true },
377 }),
378 "thread.jump.1",
379 );
380 assert.isFalse(
381 shouldShowThreadJumpHintsForModifiers(
382 event({ metaKey: true }),
383 DEFAULT_RESOLVED_KEYBINDINGS,
384 {
385 platform: "MacIntel",
386 context: { isDesktop: false },
387 },
388 ),
389 );
390 assert.isTrue(
391 shouldShowThreadJumpHintsForModifiers(
392 event({ metaKey: true }),
393 DEFAULT_RESOLVED_KEYBINDINGS,
394 {
395 platform: "MacIntel",
396 context: { isDesktop: true },
397 },
398 ),
399 );
400 });
401});
402
403describe("model picker navigation helpers", () => {
404 it("keeps default model jumps off the web even while the picker is open", () => {
405 const input = event({ key: "3", metaKey: true });
406 assert.isNull(
407 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
408 platform: "MacIntel",
409 context: { isDesktop: false, modelPickerOpen: true },
410 }),
411 );
412 assert.strictEqual(
413 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
414 platform: "MacIntel",
415 context: { isDesktop: true, modelPickerOpen: true },
416 }),
417 "modelPicker.jump.3",
418 );
419 assert.strictEqual(
420 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
421 platform: "MacIntel",
422 context: { isDesktop: true, modelPickerOpen: false },
423 }),
424 "thread.jump.3",
425 );
426 });
427});
428
429describe("chat/editor shortcuts", () => {
430 it("toggles the thread panel by default without taking terminal keystrokes", () => {
431 for (const platform of ["MacIntel", "Win32", "Linux"]) {
432 const input = event({
433 key: "b",
434 metaKey: platform === "MacIntel",
435 ctrlKey: platform !== "MacIntel",
436 shiftKey: true,
437 });
438 assert.strictEqual(
439 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
440 platform,
441 context: { terminalFocus: false, editableFocus: true },
442 }),
443 "threadPanel.toggle",
444 );
445 assert.isNull(
446 resolveShortcutCommand(input, DEFAULT_RESOLVED_KEYBINDINGS, {
447 platform,
448 context: { terminalFocus: true },
449 }),
450 );
451 }
452 });
453
454 it("preserves a user-configured thread panel shortcut instead of its default", () => {
455 const bindings = mergeWithDefaultKeybindings(
456 compile([
457 {
458 shortcut: modShortcut("b", { altKey: true, shiftKey: true }),
459 command: "threadPanel.toggle",
460 },
461 ]),
462 );
463
464 assert.strictEqual(
465 resolveShortcutCommand(
466 event({ key: "b", metaKey: true, altKey: true, shiftKey: true }),
467 bindings,
468 { platform: "MacIntel" },
469 ),
470 "threadPanel.toggle",
471 );
472 assert.isNull(
473 resolveShortcutCommand(event({ key: "b", metaKey: true, shiftKey: true }), bindings, {
474 platform: "MacIntel",
475 }),
476 );
477 });
478});
479
480describe("cross-command precedence", () => {
481 it("uses when + order so a later focused rule overrides a global rule", () => {
482 const keybindings = compile([
483 { shortcut: modShortcut("n"), command: "chat.new" },
484 {
485 shortcut: modShortcut("n"),
486 command: "terminal.new",
487 whenAst: whenIdentifier("terminalFocus"),
488 },
489 ]);
490
491 assert.isTrue(
492 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
493 platform: "MacIntel",
494 context: { terminalFocus: true },
495 }),
496 );
497 assert.strictEqual(
498 resolveShortcutCommand(event({ key: "n", metaKey: true }), keybindings, {
499 platform: "MacIntel",
500 context: { terminalFocus: true },
501 }),
502 "terminal.new",
503 );
504 assert.isFalse(
505 isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
506 platform: "MacIntel",
507 context: { terminalFocus: false },
508 }),
509 );
510 assert.strictEqual(
511 resolveShortcutCommand(event({ key: "n", metaKey: true }), keybindings, {
512 platform: "MacIntel",
513 context: { terminalFocus: false },
514 }),
515 "chat.new",
516 );
517 });
518
519 it("still lets a later global rule win when both rules match", () => {
520 const keybindings = compile([
521 {
522 shortcut: modShortcut("n"),
523 command: "terminal.new",
524 whenAst: whenIdentifier("terminalFocus"),
525 },
526 { shortcut: modShortcut("n"), command: "chat.new" },
527 ]);
528
529 assert.isFalse(
530 isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
531 platform: "Linux",
532 context: { terminalFocus: true },
533 }),
534 );
535 assert.strictEqual(
536 resolveShortcutCommand(event({ key: "n", ctrlKey: true }), keybindings, {
537 platform: "Linux",
538 context: { terminalFocus: true },
539 }),
540 "chat.new",
541 );
542 });
543});
544
545describe("resolveChatShortcutCommand", () => {
546 it("runs the active environment's script shortcut instead of the primary environment's", () => {
547 const primary = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
548 const active = compile([{ shortcut: modShortcut("t"), command: "script.setup.run" }]);
549
550 assert.strictEqual(
551 resolveChatShortcutCommand(event({ key: "t", ctrlKey: true }), primary, active, {
552 platform: "Linux",
553 }),
554 "script.setup.run",
555 );
556 assert.isNull(
557 resolveChatShortcutCommand(event({ key: "r", ctrlKey: true }), primary, active, {
558 platform: "Linux",
559 }),
560 );
561 });
562
563 it("does not run primary scripts before the active environment's bindings arrive", () => {
564 const primary = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
565
566 assert.isNull(
567 resolveChatShortcutCommand(event({ key: "r", ctrlKey: true }), primary, [], {
568 platform: "Linux",
569 }),
570 );
571 });
572
573 it("keeps primary app commands ahead of conflicting active script shortcuts", () => {
574 const primary = compile([{ shortcut: modShortcut("k"), command: "commandPalette.toggle" }]);
575 const active = compile([{ shortcut: modShortcut("k"), command: "script.setup.run" }]);
576
577 assert.strictEqual(
578 resolveChatShortcutCommand(event({ key: "k", ctrlKey: true }), primary, active, {
579 platform: "Linux",
580 }),
581 "commandPalette.toggle",
582 );
583 });
584
585 it("keeps app shortcuts on the primary environment when the active environment remaps them", () => {
586 const primary = compile([{ shortcut: modShortcut("j"), command: "terminal.toggle" }]);
587 const active = compile([{ shortcut: modShortcut("t"), command: "terminal.toggle" }]);
588
589 assert.strictEqual(
590 resolveChatShortcutCommand(event({ key: "j", ctrlKey: true }), primary, active, {
591 platform: "Linux",
592 }),
593 "terminal.toggle",
594 );
595 assert.isNull(
596 resolveChatShortcutCommand(event({ key: "t", ctrlKey: true }), primary, active, {
597 platform: "Linux",
598 }),
599 );
600 });
601
602 it.each([false, true])(
603 "preserves active binding precedence with terminalFocus=%s",
604 (terminalFocus) => {
605 const primary = compile([{ shortcut: modShortcut("r"), command: "script.setup.run" }]);
606 const active = compile([
607 { shortcut: modShortcut("r"), command: "script.deploy.run" },
608 {
609 shortcut: modShortcut("r"),
610 command: "terminal.new",
611 whenAst: whenIdentifier("terminalFocus"),
612 },
613 ]);
614 const options = { platform: "Linux", context: { terminalFocus } };
615 const shortcut = event({ key: "r", ctrlKey: true });
616
617 assert.strictEqual(
618 resolveChatShortcutCommand(shortcut, primary, active, options),
619 terminalFocus ? null : "script.deploy.run",
620 );
621 assert.strictEqual(
622 resolveChatShortcutCommand(shortcut, active, active, options),
623 resolveShortcutCommand(shortcut, active, options),
624 );
625 },
626 );
627});
628
629describe("resolveShortcutCommand", () => {
630 it.each(["constructor", "!constructor"])(
631 "treats inherited object properties in when-condition %s as unset",
632 (when) => {
633 const keybindings = compileResolvedKeybindingsConfig([
634 { key: "mod+j", command: "terminal.toggle", when },
635 ]);
636 assert.strictEqual(
637 resolveShortcutCommand(event({ code: "KeyJ", ctrlKey: true }), keybindings, {
638 platform: "Linux",
639 }),
640 when.startsWith("!") ? "terminal.toggle" : null,
641 );
642 },
643 );
644
645 it("routes mod+w to the terminal while focused and to the right panel otherwise", () => {
646 const closeEvent = event({ key: "w", metaKey: true });
647 assert.strictEqual(
648 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
649 platform: "MacIntel",
650 context: { terminalFocus: true },
651 }),
652 "terminal.close",
653 );
654 assert.strictEqual(
655 resolveShortcutCommand(closeEvent, DEFAULT_BINDINGS, {
656 platform: "MacIntel",
657 context: { terminalFocus: false },
658 }),
659 "rightPanel.close",
660 );
661 });
662
663 it("maximizes the right panel with mod+alt+e by default", () => {
664 // macOS reports Option+E as the dead key "´", so the physical key decides.
665 const mac = event({ key: "´", code: "KeyE", metaKey: true, altKey: true });
666 const linux = event({ key: "e", code: "KeyE", ctrlKey: true, altKey: true });
667 assert.strictEqual(
668 resolveShortcutCommand(mac, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "MacIntel" }),
669 "rightPanel.toggleMaximized",
670 );
671 assert.strictEqual(
672 resolveShortcutCommand(linux, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "Linux" }),
673 "rightPanel.toggleMaximized",
674 );
675 });
676
677 it("uses terminal cycling shortcuts without changing bracket history shortcuts", () => {
678 const back = event({ key: "[", code: "BracketLeft", metaKey: true });
679 const forward = event({ key: "]", code: "BracketRight", ctrlKey: true });
680 assert.strictEqual(
681 resolveShortcutCommand(back, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "MacIntel" }),
682 "navigation.back",
683 );
684 assert.strictEqual(
685 resolveShortcutCommand(forward, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "Linux" }),
686 "navigation.forward",
687 );
688 for (const platform of ["MacIntel", "Win32", "Linux"]) {
689 const mac = platform === "MacIntel";
690 for (const [key, shiftedKey, code, direction] of [
691 ["[", "{", "BracketLeft", "previous"],
692 ["]", "}", "BracketRight", "next"],
693 ] as const) {
694 const shortcut = event({
695 key: mac ? shiftedKey : key,
696 code,
697 metaKey: mac,
698 ctrlKey: !mac,
699 altKey: !mac,
700 shiftKey: mac,
701 });
702 assert.strictEqual(
703 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
704 platform,
705 context: { terminalFocus: true },
706 }),
707 `terminal.${direction}`,
708 );
709 if (mac) {
710 assert.strictEqual(
711 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
712 platform,
713 context: { terminalFocus: false },
714 }),
715 `thread.${direction}`,
716 );
717 }
718 }
719 if (mac) {
720 for (const key of ["[", "]"]) {
721 const command = resolveShortcutCommand(
722 event({ key, metaKey: true }),
723 DEFAULT_RESOLVED_KEYBINDINGS,
724 {
725 platform,
726 context: { terminalFocus: true },
727 },
728 );
729 assert.notStrictEqual(command, "terminal.previous");
730 assert.notStrictEqual(command, "terminal.next");
731 }
732 }
733 assert.isNull(
734 resolveShortcutCommand(event({ key: "[", ctrlKey: true }), DEFAULT_RESOLVED_KEYBINDINGS, {
735 platform,
736 context: { terminalFocus: true },
737 }),
738 );
739 }
740 });
741
742 it("matches punctuation shortcuts by physical key across keyboard layouts", () => {
743 const keybindings = compile([
744 { shortcut: modShortcut("'", { shiftKey: true }), command: "diff.toggle" },
745 ]);
746
747 assert.strictEqual(
748 resolveShortcutCommand(
749 event({ key: "@", code: "Quote", metaKey: true, shiftKey: true }),
750 keybindings,
751 { platform: "MacIntel" },
752 ),
753 "diff.toggle",
754 );
755 assert.isNull(
756 resolveShortcutCommand(
757 event({ key: '"', code: "Digit2", metaKey: true, shiftKey: true }),
758 keybindings,
759 { platform: "MacIntel" },
760 ),
761 );
762 });
763
764 it('matches Ctrl+layout "+" as itself unless Shift, Alt or the number row is involved', () => {
765 assert.isNull(
766 resolveShortcutCommand(
767 event({ key: "+", code: "BracketRight", ctrlKey: true }),
768 DEFAULT_RESOLVED_KEYBINDINGS,
769 { platform: "Linux", context: { terminalFocus: false } },
770 ),
771 );
772 assert.strictEqual(
773 resolveShortcutCommand(
774 event({ key: "+", code: "Minus", ctrlKey: true }),
775 DEFAULT_RESOLVED_KEYBINDINGS,
776 { platform: "Linux", context: { previewFocus: true } },
777 ),
778 "preview.zoomIn",
779 );
780 assert.strictEqual(
781 resolveShortcutCommand(
782 event({ key: "+", code: "Digit1", ctrlKey: true }),
783 DEFAULT_RESOLVED_KEYBINDINGS,
784 { platform: "Linux", context: { isDesktop: true } },
785 ),
786 "thread.jump.1",
787 );
788 assert.strictEqual(
789 resolveShortcutCommand(
790 event({ key: "+", code: "BracketRight", altKey: true }),
791 compile([
792 {
793 shortcut: modShortcut("]", { modKey: false, altKey: true }),
794 command: "terminal.toggle",
795 },
796 ]),
797 { platform: "Linux" },
798 ),
799 "terminal.toggle",
800 );
801 assert.strictEqual(
802 resolveShortcutCommand(
803 event({ key: "+", code: "Equal", ctrlKey: true, shiftKey: true }),
804 compile([{ shortcut: modShortcut("=", { shiftKey: true }), command: "terminal.toggle" }]),
805 { platform: "Linux" },
806 ),
807 "terminal.toggle",
808 );
809 });
810
811 it("does not let a punctuation position shadow a Latin layout key", () => {
812 const keybindings = compile([
813 { shortcut: modShortcut("m"), command: "diff.toggle" },
814 { shortcut: modShortcut(";"), command: "sidebar.toggle" },
815 ]);
816
817 assert.strictEqual(
818 resolveShortcutCommand(event({ key: "m", code: "Semicolon", metaKey: true }), keybindings, {
819 platform: "MacIntel",
820 }),
821 "diff.toggle",
822 );
823 });
824
825 it("matches Option-modified letters using the physical key code on macOS", () => {
826 assert.strictEqual(
827 resolveShortcutCommand(
828 event({ key: "∫", code: "KeyB", metaKey: true, altKey: true }),
829 DEFAULT_BINDINGS,
830 { platform: "MacIntel" },
831 ),
832 "rightPanel.toggle",
833 );
834 });
835
836 it("matches Option symbols against the active Latin layout rather than the US position", () => {
837 const keybindings = compile([
838 { shortcut: modShortcut("a", { altKey: true, shiftKey: true }), command: "appearance.cycle" },
839 { shortcut: modShortcut("q", { altKey: true, shiftKey: true }), command: "sidebar.toggle" },
840 ]);
841 assert.strictEqual(
842 resolveShortcutCommand(
843 event({ key: "Æ", code: "KeyQ", metaKey: true, altKey: true, shiftKey: true }),
844 keybindings,
845 { platform: "MacIntel", keyboardLayoutMap: new Map([["KeyQ", "a"]]) },
846 ),
847 "appearance.cycle",
848 );
849 });
850
851 it("preserves physical punctuation alongside an Option-modified mapped letter", () => {
852 for (const key of [";", "m"]) {
853 const keybindings = compile([
854 { shortcut: modShortcut(key, { altKey: true }), command: "diff.toggle" },
855 ]);
856 assert.strictEqual(
857 resolveShortcutCommand(
858 event({ key: "µ", code: "Semicolon", metaKey: true, altKey: true }),
859 keybindings,
860 { platform: "MacIntel", keyboardLayoutMap: new Map([["Semicolon", "m"]]) },
861 ),
862 "diff.toggle",
863 );
864 }
865 });
866
867 it("keeps Latin event keys and unsupported layout fallbacks", () => {
868 const keybindings = compile([
869 { shortcut: modShortcut("a", { altKey: true }), command: "appearance.cycle" },
870 { shortcut: modShortcut("q", { altKey: true }), command: "sidebar.toggle" },
871 ]);
872 const options = { platform: "MacIntel", keyboardLayoutMap: new Map([["KeyQ", "a"]]) };
873 assert.strictEqual(
874 resolveShortcutCommand(
875 event({ key: "q", code: "KeyQ", metaKey: true, altKey: true }),
876 keybindings,
877 options,
878 ),
879 "sidebar.toggle",
880 );
881 for (const keyboardLayoutMap of [new Map<string, string>(), new Map([["KeyQ", "й"]])]) {
882 assert.strictEqual(
883 resolveShortcutCommand(
884 event({ key: "Æ", code: "KeyQ", metaKey: true, altKey: true }),
885 keybindings,
886 { ...options, keyboardLayoutMap },
887 ),
888 "sidebar.toggle",
889 );
890 }
891 assert.strictEqual(
892 resolveShortcutCommand(
893 event({
894 key: "Æ",
895 code: "KeyQ",
896 ctrlKey: true,
897 altKey: true,
898 getModifierState: () => true,
899 }),
900 keybindings,
901 { ...options, platform: "Win32" },
902 ),
903 null,
904 );
905 });
906
907 it("matches non-Latin layout letters using the physical key code", () => {
908 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
909
910 assert.strictEqual(
911 resolveShortcutCommand(event({ key: "в", code: "KeyD", metaKey: true }), keybindings, {
912 platform: "MacIntel",
913 }),
914 "diff.toggle",
915 );
916 });
917
918 it("ignores the physical key code when the layout types a different Latin letter", () => {
919 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
920
921 assert.isNull(
922 resolveShortcutCommand(event({ key: "a", code: "KeyD", metaKey: true }), keybindings, {
923 platform: "MacIntel",
924 }),
925 );
926 assert.strictEqual(
927 resolveShortcutCommand(event({ key: "d", code: "KeyL", metaKey: true }), keybindings, {
928 platform: "MacIntel",
929 }),
930 "diff.toggle",
931 );
932 });
933});
934
935describe("isRichTextBoldShortcut", () => {
936 const selected = { isCollapsed: false };
937
938 it("matches the B key on non-Latin layouts, like the sidebar toggle does", () => {
939 const cyrillicB = event({ key: "и", code: "KeyB", ctrlKey: true });
940 assert.isTrue(isRichTextBoldShortcut(cyrillicB, selected));
941 assert.strictEqual(
942 resolveShortcutCommand(cyrillicB, DEFAULT_BINDINGS, { platform: "Win32" }),
943 "sidebar.toggle",
944 );
945 });
946
947 it("follows the letter a Latin layout types, not the physical key", () => {
948 assert.isFalse(
949 isRichTextBoldShortcut(event({ key: "x", code: "KeyB", ctrlKey: true }), selected),
950 );
951 assert.isTrue(
952 isRichTextBoldShortcut(event({ key: "b", code: "KeyN", ctrlKey: true }), selected),
953 );
954 });
955
956 it("leaves the chord to the sidebar toggle when no text is selected", () => {
957 const modB = event({ key: "b", metaKey: true });
958 assert.isFalse(isRichTextBoldShortcut(modB, { isCollapsed: true }));
959 assert.isFalse(isRichTextBoldShortcut(modB, null));
960 });
961});
962
963describe("isSidebarToggleShortcut", () => {
964 it("leaves Ctrl+B to a focused terminal, where it is the tmux prefix", () => {
965 const ctrlB = event({ key: "b", ctrlKey: true });
966 assert.isFalse(
967 isSidebarToggleShortcut(ctrlB, DEFAULT_BINDINGS, {
968 platform: "Linux",
969 context: { terminalFocus: true },
970 }),
971 );
972 assert.isTrue(
973 isSidebarToggleShortcut(ctrlB, DEFAULT_BINDINGS, {
974 platform: "Linux",
975 context: { terminalFocus: false },
976 }),
977 );
978 });
979
980 it("keeps Cmd+B toggling the sidebar from a focused terminal on macOS", () => {
981 assert.isTrue(
982 isSidebarToggleShortcut(event({ key: "b", metaKey: true }), DEFAULT_BINDINGS, {
983 platform: "MacIntel",
984 context: { terminalFocus: true },
985 }),
986 );
987 });
988});
989
990describe("composer and pull request shortcuts", () => {
991 it("uses the dictation chord outside the terminal and preserves terminal split", () => {
992 const chord = event({ key: "d", ctrlKey: true, shiftKey: true });
993 assert.strictEqual(
994 resolveShortcutCommand(chord, DEFAULT_RESOLVED_KEYBINDINGS, {
995 platform: "Linux",
996 context: { terminalFocus: false },
997 }),
998 "composer.dictation",
999 );
1000 assert.strictEqual(
1001 resolveShortcutCommand(chord, DEFAULT_RESOLVED_KEYBINDINGS, {
1002 platform: "Linux",
1003 context: { terminalFocus: true },
1004 }),
1005 "terminal.splitVertical",
1006 );
1007 });
1008 it("fills missing number shortcuts without replacing the saved URL binding", () => {
1009 const olderServerBindings = DEFAULT_RESOLVED_KEYBINDINGS.filter(
1010 (binding) =>
1011 binding.command !== "pullRequest.copyNumber" && binding.command !== "thread.copyReference",
1012 );
1013 const bindings = mergeWithDefaultKeybindings([
1014 ...olderServerBindings,
1015 ...compileResolvedKeybindingsConfig([
1016 { key: "mod+shift+8", command: "thread.copyReference", when: "!terminalFocus" },
1017 ]),
1018 ]);
1019 for (const [key, command] of [
1020 ["k", "pullRequest.copyNumber"],
1021 ["8", "thread.copyReference"],
1022 ["c", null],
1023 ["y", null],
1024 ] as const) {
1025 assert.strictEqual(
1026 resolveShortcutCommand(event({ key, metaKey: true, shiftKey: true }), bindings, {
1027 platform: "MacIntel",
1028 }),
1029 command,
1030 );
1031 }
1032 });
1033
1034 it.each(["MacIntel", "Win32", "Linux"])(
1035 "reserves the alternate send chord for the focused composer on %s",
1036 (platform) => {
1037 const modifier = {
1038 metaKey: platform === "MacIntel",
1039 ctrlKey: platform !== "MacIntel",
1040 };
1041 const shiftedEnter = event({ key: "Enter", shiftKey: true, ...modifier });
1042 for (const composerRequiresModifier of [false, true]) {
1043 const context = {
1044 composerFocus: true,
1045 turnRunning: true,
1046 composerRequiresModifier,
1047 };
1048 assert.strictEqual(
1049 resolveShortcutCommand(shiftedEnter, DEFAULT_RESOLVED_KEYBINDINGS, {
1050 platform,
1051 context,
1052 }),
1053 composerRequiresModifier ? "composer.sendAlternate" : "thread.steerQueuedMessage",
1054 );
1055 assert.strictEqual(
1056 shortcutLabelForCommand(DEFAULT_RESOLVED_KEYBINDINGS, "composer.sendAlternate", {
1057 platform,
1058 context,
1059 }),
1060 formatShortcutLabel(
1061 modShortcut("enter", { shiftKey: composerRequiresModifier }),
1062 platform,
1063 ),
1064 );
1065 assert.strictEqual(
1066 resolveShortcutCommand(shiftedEnter, DEFAULT_RESOLVED_KEYBINDINGS, {
1067 platform,
1068 context: { ...context, composerFocus: false },
1069 }),
1070 "thread.steerQueuedMessage",
1071 );
1072 }
1073 },
1074 );
1075
1076 const altEffortBindings = compileResolvedKeybindingsConfig([
1077 { key: "mod+alt+e", command: "composer.effort", when: "!terminalFocus" },
1078 ]);
1079
1080 it("leaves AltGr text entry alone with a custom Alt binding", () => {
1081 for (const platform of ["Win32", "Linux"]) {
1082 const input = event({
1083 key: "€",
1084 code: "KeyE",
1085 ctrlKey: true,
1086 altKey: true,
1087 getModifierState: (key) => key === "AltGraph",
1088 });
1089 assert.isNull(resolveShortcutCommand(input, altEffortBindings, { platform }));
1090 assert.strictEqual(
1091 resolveShortcutCommand({ ...input, getModifierState: () => false }, altEffortBindings, {
1092 platform,
1093 }),
1094 "composer.effort",
1095 );
1096 }
1097 });
1098
1099 it("keeps Firefox modifier reporting usable on Windows and macOS", () => {
1100 const getModifierState = (key: string) => key === "AltGraph";
1101 assert.strictEqual(
1102 resolveShortcutCommand(
1103 event({ key: "e", ctrlKey: true, altKey: true, getModifierState }),
1104 altEffortBindings,
1105 { platform: "Win32" },
1106 ),
1107 "composer.effort",
1108 );
1109 assert.strictEqual(
1110 resolveShortcutCommand(
1111 event({ key: "´", code: "KeyE", metaKey: true, altKey: true, getModifierState }),
1112 altEffortBindings,
1113 { platform: "MacIntel" },
1114 ),
1115 "composer.effort",
1116 );
1117 });
1118});
1119
1120describe("Usage shortcuts", () => {
1121 it.each(["Linux", "MacIntel"])(
1122 "preserves desktop numbered thread shortcuts on Usage on %s",
1123 (platform) => {
1124 const shortcut = event({
1125 key: "2",
1126 ctrlKey: platform === "Linux",
1127 metaKey: platform === "MacIntel",
1128 });
1129 assert.strictEqual(
1130 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1131 platform,
1132 context: { usagePageOpen: true, isDesktop: true },
1133 }),
1134 "thread.jump.2",
1135 );
1136 assert.isNotNull(
1137 shortcutLabelForCommand(DEFAULT_RESOLVED_KEYBINDINGS, "thread.jump.2", {
1138 platform,
1139 context: { usagePageOpen: true, isDesktop: true },
1140 }),
1141 );
1142 },
1143 );
1144
1145 it("matches shifted number keys for periods and only on Usage", () => {
1146 const shortcut = event({ key: "!", code: "Digit1", ctrlKey: true, shiftKey: true });
1147 assert.strictEqual(
1148 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1149 platform: "Linux",
1150 context: { usagePageOpen: true },
1151 }),
1152 "usage.period.day",
1153 );
1154 assert.isNull(
1155 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1156 platform: "Linux",
1157 }),
1158 );
1159 });
1160});