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("uses terminal cycling shortcuts without changing bracket history shortcuts", () => {
664 const back = event({ key: "[", code: "BracketLeft", metaKey: true });
665 const forward = event({ key: "]", code: "BracketRight", ctrlKey: true });
666 assert.strictEqual(
667 resolveShortcutCommand(back, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "MacIntel" }),
668 "navigation.back",
669 );
670 assert.strictEqual(
671 resolveShortcutCommand(forward, DEFAULT_RESOLVED_KEYBINDINGS, { platform: "Linux" }),
672 "navigation.forward",
673 );
674 for (const platform of ["MacIntel", "Win32", "Linux"]) {
675 const mac = platform === "MacIntel";
676 for (const [key, shiftedKey, code, direction] of [
677 ["[", "{", "BracketLeft", "previous"],
678 ["]", "}", "BracketRight", "next"],
679 ] as const) {
680 const shortcut = event({
681 key: mac ? shiftedKey : key,
682 code,
683 metaKey: mac,
684 ctrlKey: !mac,
685 altKey: !mac,
686 shiftKey: mac,
687 });
688 assert.strictEqual(
689 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
690 platform,
691 context: { terminalFocus: true },
692 }),
693 `terminal.${direction}`,
694 );
695 if (mac) {
696 assert.strictEqual(
697 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
698 platform,
699 context: { terminalFocus: false },
700 }),
701 `thread.${direction}`,
702 );
703 }
704 }
705 if (mac) {
706 for (const key of ["[", "]"]) {
707 const command = resolveShortcutCommand(
708 event({ key, metaKey: true }),
709 DEFAULT_RESOLVED_KEYBINDINGS,
710 {
711 platform,
712 context: { terminalFocus: true },
713 },
714 );
715 assert.notStrictEqual(command, "terminal.previous");
716 assert.notStrictEqual(command, "terminal.next");
717 }
718 }
719 assert.isNull(
720 resolveShortcutCommand(event({ key: "[", ctrlKey: true }), DEFAULT_RESOLVED_KEYBINDINGS, {
721 platform,
722 context: { terminalFocus: true },
723 }),
724 );
725 }
726 });
727
728 it("matches punctuation shortcuts by physical key across keyboard layouts", () => {
729 const keybindings = compile([
730 { shortcut: modShortcut("'", { shiftKey: true }), command: "diff.toggle" },
731 ]);
732
733 assert.strictEqual(
734 resolveShortcutCommand(
735 event({ key: "@", code: "Quote", metaKey: true, shiftKey: true }),
736 keybindings,
737 { platform: "MacIntel" },
738 ),
739 "diff.toggle",
740 );
741 assert.isNull(
742 resolveShortcutCommand(
743 event({ key: '"', code: "Digit2", metaKey: true, shiftKey: true }),
744 keybindings,
745 { platform: "MacIntel" },
746 ),
747 );
748 });
749
750 it('matches Ctrl+layout "+" as itself unless Shift, Alt or the number row is involved', () => {
751 assert.isNull(
752 resolveShortcutCommand(
753 event({ key: "+", code: "BracketRight", ctrlKey: true }),
754 DEFAULT_RESOLVED_KEYBINDINGS,
755 { platform: "Linux", context: { terminalFocus: false } },
756 ),
757 );
758 assert.strictEqual(
759 resolveShortcutCommand(
760 event({ key: "+", code: "Minus", ctrlKey: true }),
761 DEFAULT_RESOLVED_KEYBINDINGS,
762 { platform: "Linux", context: { previewFocus: true } },
763 ),
764 "preview.zoomIn",
765 );
766 assert.strictEqual(
767 resolveShortcutCommand(
768 event({ key: "+", code: "Digit1", ctrlKey: true }),
769 DEFAULT_RESOLVED_KEYBINDINGS,
770 { platform: "Linux", context: { isDesktop: true } },
771 ),
772 "thread.jump.1",
773 );
774 assert.strictEqual(
775 resolveShortcutCommand(
776 event({ key: "+", code: "BracketRight", altKey: true }),
777 compile([
778 {
779 shortcut: modShortcut("]", { modKey: false, altKey: true }),
780 command: "terminal.toggle",
781 },
782 ]),
783 { platform: "Linux" },
784 ),
785 "terminal.toggle",
786 );
787 assert.strictEqual(
788 resolveShortcutCommand(
789 event({ key: "+", code: "Equal", ctrlKey: true, shiftKey: true }),
790 compile([{ shortcut: modShortcut("=", { shiftKey: true }), command: "terminal.toggle" }]),
791 { platform: "Linux" },
792 ),
793 "terminal.toggle",
794 );
795 });
796
797 it("does not let a punctuation position shadow a Latin layout key", () => {
798 const keybindings = compile([
799 { shortcut: modShortcut("m"), command: "diff.toggle" },
800 { shortcut: modShortcut(";"), command: "sidebar.toggle" },
801 ]);
802
803 assert.strictEqual(
804 resolveShortcutCommand(event({ key: "m", code: "Semicolon", metaKey: true }), keybindings, {
805 platform: "MacIntel",
806 }),
807 "diff.toggle",
808 );
809 });
810
811 it("matches Option-modified letters using the physical key code on macOS", () => {
812 assert.strictEqual(
813 resolveShortcutCommand(
814 event({ key: "∫", code: "KeyB", metaKey: true, altKey: true }),
815 DEFAULT_BINDINGS,
816 { platform: "MacIntel" },
817 ),
818 "rightPanel.toggle",
819 );
820 });
821
822 it("matches Option symbols against the active Latin layout rather than the US position", () => {
823 const keybindings = compile([
824 { shortcut: modShortcut("a", { altKey: true, shiftKey: true }), command: "appearance.cycle" },
825 { shortcut: modShortcut("q", { altKey: true, shiftKey: true }), command: "sidebar.toggle" },
826 ]);
827 assert.strictEqual(
828 resolveShortcutCommand(
829 event({ key: "Æ", code: "KeyQ", metaKey: true, altKey: true, shiftKey: true }),
830 keybindings,
831 { platform: "MacIntel", keyboardLayoutMap: new Map([["KeyQ", "a"]]) },
832 ),
833 "appearance.cycle",
834 );
835 });
836
837 it("preserves physical punctuation alongside an Option-modified mapped letter", () => {
838 for (const key of [";", "m"]) {
839 const keybindings = compile([
840 { shortcut: modShortcut(key, { altKey: true }), command: "diff.toggle" },
841 ]);
842 assert.strictEqual(
843 resolveShortcutCommand(
844 event({ key: "µ", code: "Semicolon", metaKey: true, altKey: true }),
845 keybindings,
846 { platform: "MacIntel", keyboardLayoutMap: new Map([["Semicolon", "m"]]) },
847 ),
848 "diff.toggle",
849 );
850 }
851 });
852
853 it("keeps Latin event keys and unsupported layout fallbacks", () => {
854 const keybindings = compile([
855 { shortcut: modShortcut("a", { altKey: true }), command: "appearance.cycle" },
856 { shortcut: modShortcut("q", { altKey: true }), command: "sidebar.toggle" },
857 ]);
858 const options = { platform: "MacIntel", keyboardLayoutMap: new Map([["KeyQ", "a"]]) };
859 assert.strictEqual(
860 resolveShortcutCommand(
861 event({ key: "q", code: "KeyQ", metaKey: true, altKey: true }),
862 keybindings,
863 options,
864 ),
865 "sidebar.toggle",
866 );
867 for (const keyboardLayoutMap of [new Map<string, string>(), new Map([["KeyQ", "й"]])]) {
868 assert.strictEqual(
869 resolveShortcutCommand(
870 event({ key: "Æ", code: "KeyQ", metaKey: true, altKey: true }),
871 keybindings,
872 { ...options, keyboardLayoutMap },
873 ),
874 "sidebar.toggle",
875 );
876 }
877 assert.strictEqual(
878 resolveShortcutCommand(
879 event({
880 key: "Æ",
881 code: "KeyQ",
882 ctrlKey: true,
883 altKey: true,
884 getModifierState: () => true,
885 }),
886 keybindings,
887 { ...options, platform: "Win32" },
888 ),
889 null,
890 );
891 });
892
893 it("matches non-Latin layout letters using the physical key code", () => {
894 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
895
896 assert.strictEqual(
897 resolveShortcutCommand(event({ key: "в", code: "KeyD", metaKey: true }), keybindings, {
898 platform: "MacIntel",
899 }),
900 "diff.toggle",
901 );
902 });
903
904 it("ignores the physical key code when the layout types a different Latin letter", () => {
905 const keybindings = compile([{ shortcut: modShortcut("d"), command: "diff.toggle" }]);
906
907 assert.isNull(
908 resolveShortcutCommand(event({ key: "a", code: "KeyD", metaKey: true }), keybindings, {
909 platform: "MacIntel",
910 }),
911 );
912 assert.strictEqual(
913 resolveShortcutCommand(event({ key: "d", code: "KeyL", metaKey: true }), keybindings, {
914 platform: "MacIntel",
915 }),
916 "diff.toggle",
917 );
918 });
919});
920
921describe("isRichTextBoldShortcut", () => {
922 const selected = { isCollapsed: false };
923
924 it("matches the B key on non-Latin layouts, like the sidebar toggle does", () => {
925 const cyrillicB = event({ key: "и", code: "KeyB", ctrlKey: true });
926 assert.isTrue(isRichTextBoldShortcut(cyrillicB, selected));
927 assert.strictEqual(
928 resolveShortcutCommand(cyrillicB, DEFAULT_BINDINGS, { platform: "Win32" }),
929 "sidebar.toggle",
930 );
931 });
932
933 it("follows the letter a Latin layout types, not the physical key", () => {
934 assert.isFalse(
935 isRichTextBoldShortcut(event({ key: "x", code: "KeyB", ctrlKey: true }), selected),
936 );
937 assert.isTrue(
938 isRichTextBoldShortcut(event({ key: "b", code: "KeyN", ctrlKey: true }), selected),
939 );
940 });
941
942 it("leaves the chord to the sidebar toggle when no text is selected", () => {
943 const modB = event({ key: "b", metaKey: true });
944 assert.isFalse(isRichTextBoldShortcut(modB, { isCollapsed: true }));
945 assert.isFalse(isRichTextBoldShortcut(modB, null));
946 });
947});
948
949describe("isSidebarToggleShortcut", () => {
950 it("leaves Ctrl+B to a focused terminal, where it is the tmux prefix", () => {
951 const ctrlB = event({ key: "b", ctrlKey: true });
952 assert.isFalse(
953 isSidebarToggleShortcut(ctrlB, DEFAULT_BINDINGS, {
954 platform: "Linux",
955 context: { terminalFocus: true },
956 }),
957 );
958 assert.isTrue(
959 isSidebarToggleShortcut(ctrlB, DEFAULT_BINDINGS, {
960 platform: "Linux",
961 context: { terminalFocus: false },
962 }),
963 );
964 });
965
966 it("keeps Cmd+B toggling the sidebar from a focused terminal on macOS", () => {
967 assert.isTrue(
968 isSidebarToggleShortcut(event({ key: "b", metaKey: true }), DEFAULT_BINDINGS, {
969 platform: "MacIntel",
970 context: { terminalFocus: true },
971 }),
972 );
973 });
974});
975
976describe("composer and pull request shortcuts", () => {
977 it("uses the dictation chord outside the terminal and preserves terminal split", () => {
978 const chord = event({ key: "d", ctrlKey: true, shiftKey: true });
979 assert.strictEqual(
980 resolveShortcutCommand(chord, DEFAULT_RESOLVED_KEYBINDINGS, {
981 platform: "Linux",
982 context: { terminalFocus: false },
983 }),
984 "composer.dictation",
985 );
986 assert.strictEqual(
987 resolveShortcutCommand(chord, DEFAULT_RESOLVED_KEYBINDINGS, {
988 platform: "Linux",
989 context: { terminalFocus: true },
990 }),
991 "terminal.splitVertical",
992 );
993 });
994 it("fills missing number shortcuts without replacing the saved URL binding", () => {
995 const olderServerBindings = DEFAULT_RESOLVED_KEYBINDINGS.filter(
996 (binding) =>
997 binding.command !== "pullRequest.copyNumber" && binding.command !== "thread.copyReference",
998 );
999 const bindings = mergeWithDefaultKeybindings([
1000 ...olderServerBindings,
1001 ...compileResolvedKeybindingsConfig([
1002 { key: "mod+shift+8", command: "thread.copyReference", when: "!terminalFocus" },
1003 ]),
1004 ]);
1005 for (const [key, command] of [
1006 ["k", "pullRequest.copyNumber"],
1007 ["8", "thread.copyReference"],
1008 ["c", null],
1009 ["y", null],
1010 ] as const) {
1011 assert.strictEqual(
1012 resolveShortcutCommand(event({ key, metaKey: true, shiftKey: true }), bindings, {
1013 platform: "MacIntel",
1014 }),
1015 command,
1016 );
1017 }
1018 });
1019
1020 it.each(["MacIntel", "Win32", "Linux"])(
1021 "reserves the alternate send chord for the focused composer on %s",
1022 (platform) => {
1023 const modifier = {
1024 metaKey: platform === "MacIntel",
1025 ctrlKey: platform !== "MacIntel",
1026 };
1027 const shiftedEnter = event({ key: "Enter", shiftKey: true, ...modifier });
1028 for (const composerRequiresModifier of [false, true]) {
1029 const context = {
1030 composerFocus: true,
1031 turnRunning: true,
1032 composerRequiresModifier,
1033 };
1034 assert.strictEqual(
1035 resolveShortcutCommand(shiftedEnter, DEFAULT_RESOLVED_KEYBINDINGS, {
1036 platform,
1037 context,
1038 }),
1039 composerRequiresModifier ? "composer.sendAlternate" : "thread.steerQueuedMessage",
1040 );
1041 assert.strictEqual(
1042 shortcutLabelForCommand(DEFAULT_RESOLVED_KEYBINDINGS, "composer.sendAlternate", {
1043 platform,
1044 context,
1045 }),
1046 formatShortcutLabel(
1047 modShortcut("enter", { shiftKey: composerRequiresModifier }),
1048 platform,
1049 ),
1050 );
1051 assert.strictEqual(
1052 resolveShortcutCommand(shiftedEnter, DEFAULT_RESOLVED_KEYBINDINGS, {
1053 platform,
1054 context: { ...context, composerFocus: false },
1055 }),
1056 "thread.steerQueuedMessage",
1057 );
1058 }
1059 },
1060 );
1061
1062 const altEffortBindings = compileResolvedKeybindingsConfig([
1063 { key: "mod+alt+e", command: "composer.effort", when: "!terminalFocus" },
1064 ]);
1065
1066 it("leaves AltGr text entry alone with a custom Alt binding", () => {
1067 for (const platform of ["Win32", "Linux"]) {
1068 const input = event({
1069 key: "€",
1070 code: "KeyE",
1071 ctrlKey: true,
1072 altKey: true,
1073 getModifierState: (key) => key === "AltGraph",
1074 });
1075 assert.isNull(resolveShortcutCommand(input, altEffortBindings, { platform }));
1076 assert.strictEqual(
1077 resolveShortcutCommand({ ...input, getModifierState: () => false }, altEffortBindings, {
1078 platform,
1079 }),
1080 "composer.effort",
1081 );
1082 }
1083 });
1084
1085 it("keeps Firefox modifier reporting usable on Windows and macOS", () => {
1086 const getModifierState = (key: string) => key === "AltGraph";
1087 assert.strictEqual(
1088 resolveShortcutCommand(
1089 event({ key: "e", ctrlKey: true, altKey: true, getModifierState }),
1090 altEffortBindings,
1091 { platform: "Win32" },
1092 ),
1093 "composer.effort",
1094 );
1095 assert.strictEqual(
1096 resolveShortcutCommand(
1097 event({ key: "´", code: "KeyE", metaKey: true, altKey: true, getModifierState }),
1098 altEffortBindings,
1099 { platform: "MacIntel" },
1100 ),
1101 "composer.effort",
1102 );
1103 });
1104});
1105
1106describe("Usage shortcuts", () => {
1107 it.each(["Linux", "MacIntel"])(
1108 "preserves desktop numbered thread shortcuts on Usage on %s",
1109 (platform) => {
1110 const shortcut = event({
1111 key: "2",
1112 ctrlKey: platform === "Linux",
1113 metaKey: platform === "MacIntel",
1114 });
1115 assert.strictEqual(
1116 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1117 platform,
1118 context: { usagePageOpen: true, isDesktop: true },
1119 }),
1120 "thread.jump.2",
1121 );
1122 assert.isNotNull(
1123 shortcutLabelForCommand(DEFAULT_RESOLVED_KEYBINDINGS, "thread.jump.2", {
1124 platform,
1125 context: { usagePageOpen: true, isDesktop: true },
1126 }),
1127 );
1128 },
1129 );
1130
1131 it("matches shifted number keys for periods and only on Usage", () => {
1132 const shortcut = event({ key: "!", code: "Digit1", ctrlKey: true, shiftKey: true });
1133 assert.strictEqual(
1134 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1135 platform: "Linux",
1136 context: { usagePageOpen: true },
1137 }),
1138 "usage.period.day",
1139 );
1140 assert.isNull(
1141 resolveShortcutCommand(shortcut, DEFAULT_RESOLVED_KEYBINDINGS, {
1142 platform: "Linux",
1143 }),
1144 );
1145 });
1146});