@devries-cameron20 / t3code Mirror · read-only / commits / 38c93a2abd
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 25, 18:30 UTC

Resolve keybinding precedence across commands

- Evaluate shortcut matches by resolving the last matching key+when rule, regardless of command - Add cross-command precedence tests and switch assertions to `vitest` `assert` - Clarify precedence behavior in `KEYBINDINGS.md`

Julius Marminge committed Feb 15, 2026 at 12:00 UTC 38c93a2abdabb59b5a24c402d17397d1fd1493b8
3 files changed +105 -37
KEYBINDINGS.md
+2 -1
@@ -97,4 +97,5 @@ If you define any rules for a command, the default rule for that command is remo
97 ## Precedence
98
99 - Rules are evaluated in array order.
100 -- Later matching rules win over earlier matching rules for the same command.
100 +- For a key event, the server/client resolves the last rule where both `key` matches and `when` evaluates to `true`.
101 +- That means precedence is across commands, not only within the same command.
apps/web/src/keybindings.test.ts
+92 -33
@@ -1,4 +1,4 @@
1 -import { describe, expect, it } from "vitest";
1 +import { assert, describe, it } from "vitest";
2
3 import type { KeybindingsConfig } from "@t3tools/contracts";
4 import {
@@ -34,43 +34,47 @@ const DEFAULT_BINDINGS: KeybindingsConfig = [
34
35 describe("isTerminalToggleShortcut", () => {
36 it("matches Cmd+J on macOS", () => {
37 - expect(isTerminalToggleShortcut(event({ metaKey: true }), DEFAULT_BINDINGS, { platform: "MacIntel" })).toBe(true);
37 + assert.isTrue(
38 + isTerminalToggleShortcut(event({ metaKey: true }), DEFAULT_BINDINGS, { platform: "MacIntel" }),
39 + );
40 });
41
42 it("matches Ctrl+J on non-macOS", () => {
41 - expect(isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, { platform: "Win32" })).toBe(true);
43 + assert.isTrue(
44 + isTerminalToggleShortcut(event({ ctrlKey: true }), DEFAULT_BINDINGS, { platform: "Win32" }),
45 + );
46 });
47 });
48
49 describe("split/new terminal shortcuts", () => {
50 it("requires terminalFocus for default split/new bindings", () => {
47 - expect(
51 + assert.isFalse(
52 isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
53 platform: "MacIntel",
54 context: { terminalFocus: false },
55 }),
52 - ).toBe(false);
53 - expect(
56 + );
57 + assert.isFalse(
58 isTerminalNewShortcut(event({ key: "d", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
59 platform: "Linux",
60 context: { terminalFocus: false },
61 }),
58 - ).toBe(false);
62 + );
63 });
64
65 it("matches split/new when terminalFocus is true", () => {
62 - expect(
66 + assert.isTrue(
67 isTerminalSplitShortcut(event({ key: "d", metaKey: true }), DEFAULT_BINDINGS, {
68 platform: "MacIntel",
69 context: { terminalFocus: true },
70 }),
67 - ).toBe(true);
68 - expect(
71 + );
72 + assert.isTrue(
73 isTerminalNewShortcut(event({ key: "d", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
74 platform: "Linux",
75 context: { terminalFocus: true },
76 }),
73 - ).toBe(true);
77 + );
78 });
79
80 it("supports when expressions", () => {
@@ -87,24 +91,24 @@ describe("split/new terminal shortcuts", () => {
91 },
92 { key: "mod+j", command: "terminal.toggle" },
93 ];
90 - expect(
94 + assert.isTrue(
95 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
96 platform: "Win32",
97 context: { terminalOpen: true, terminalFocus: false },
98 }),
95 - ).toBe(true);
96 - expect(
99 + );
100 + assert.isFalse(
101 isTerminalSplitShortcut(event({ key: "\\", ctrlKey: true }), keybindings, {
102 platform: "Win32",
103 context: { terminalOpen: false, terminalFocus: false },
104 }),
101 - ).toBe(false);
102 - expect(
105 + );
106 + assert.isTrue(
107 isTerminalNewShortcut(event({ key: "n", ctrlKey: true, shiftKey: true }), keybindings, {
108 platform: "Win32",
109 context: { terminalOpen: true, terminalFocus: false },
110 }),
107 - ).toBe(true);
111 + );
112 });
113 });
114
@@ -114,12 +118,13 @@ describe("shortcutLabelForCommand", () => {
118 { key: "mod+\\", command: "terminal.split", when: "terminalFocus" },
119 { key: "mod+shift+\\", command: "terminal.split", when: "!terminalFocus" },
120 ];
117 - expect(shortcutLabelForCommand(bindings, "terminal.split", "Linux")).toBe("Ctrl+Shift+\\");
121 + assert.strictEqual(shortcutLabelForCommand(bindings, "terminal.split", "Linux"), "Ctrl+Shift+\\");
122 });
123
124 it("returns labels for non-terminal commands", () => {
121 - expect(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel")).toBe("⇧⌘O");
122 - expect(shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux")).toBe(
125 + assert.strictEqual(shortcutLabelForCommand(DEFAULT_BINDINGS, "chat.new", "MacIntel"), "⇧⌘O");
126 + assert.strictEqual(
127 + shortcutLabelForCommand(DEFAULT_BINDINGS, "editor.openFavorite", "Linux"),
128 "Ctrl+O",
129 );
130 });
@@ -127,49 +132,103 @@ describe("shortcutLabelForCommand", () => {
132
133 describe("chat/editor shortcuts", () => {
134 it("matches chat.new shortcut", () => {
130 - expect(
135 + assert.isTrue(
136 isChatNewShortcut(event({ key: "o", metaKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
137 platform: "MacIntel",
138 }),
134 - ).toBe(true);
135 - expect(
139 + );
140 + assert.isTrue(
141 isChatNewShortcut(event({ key: "o", ctrlKey: true, shiftKey: true }), DEFAULT_BINDINGS, {
142 platform: "Linux",
143 }),
139 - ).toBe(true);
144 + );
145 });
146
147 it("matches editor.openFavorite shortcut", () => {
143 - expect(
148 + assert.isTrue(
149 isOpenFavoriteEditorShortcut(event({ key: "o", metaKey: true }), DEFAULT_BINDINGS, {
150 platform: "MacIntel",
151 }),
147 - ).toBe(true);
148 - expect(
152 + );
153 + assert.isTrue(
154 isOpenFavoriteEditorShortcut(event({ key: "o", ctrlKey: true }), DEFAULT_BINDINGS, {
155 platform: "Linux",
156 }),
152 - ).toBe(true);
157 + );
158 + });
159 +});
160 +
161 +describe("cross-command precedence", () => {
162 + it("uses when + order so a later focused rule overrides a global rule", () => {
163 + const keybindings: KeybindingsConfig = [
164 + { key: "mod+n", command: "chat.new" },
165 + { key: "mod+n", command: "terminal.new", when: "terminalFocus" },
166 + ];
167 +
168 + assert.isTrue(
169 + isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
170 + platform: "MacIntel",
171 + context: { terminalFocus: true },
172 + }),
173 + );
174 + assert.isFalse(
175 + isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
176 + platform: "MacIntel",
177 + context: { terminalFocus: true },
178 + }),
179 + );
180 + assert.isFalse(
181 + isTerminalNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
182 + platform: "MacIntel",
183 + context: { terminalFocus: false },
184 + }),
185 + );
186 + assert.isTrue(
187 + isChatNewShortcut(event({ key: "n", metaKey: true }), keybindings, {
188 + platform: "MacIntel",
189 + context: { terminalFocus: false },
190 + }),
191 + );
192 + });
193 +
194 + it("still lets a later global rule win when both rules match", () => {
195 + const keybindings: KeybindingsConfig = [
196 + { key: "mod+n", command: "terminal.new", when: "terminalFocus" },
197 + { key: "mod+n", command: "chat.new" },
198 + ];
199 +
200 + assert.isFalse(
201 + isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
202 + platform: "Linux",
203 + context: { terminalFocus: true },
204 + }),
205 + );
206 + assert.isTrue(
207 + isChatNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
208 + platform: "Linux",
209 + context: { terminalFocus: true },
210 + }),
211 + );
212 });
213 });
214
215 describe("formatShortcutLabel", () => {
216 it("formats labels for macOS", () => {
158 - expect(formatShortcutLabel("mod+shift+d", "MacIntel")).toBe("⇧⌘D");
217 + assert.strictEqual(formatShortcutLabel("mod+shift+d", "MacIntel"), "⇧⌘D");
218 });
219
220 it("formats labels for non-macOS", () => {
162 - expect(formatShortcutLabel("mod+shift+d", "Linux")).toBe("Ctrl+Shift+D");
221 + assert.strictEqual(formatShortcutLabel("mod+shift+d", "Linux"), "Ctrl+Shift+D");
222 });
223 });
224
225 describe("isTerminalClearShortcut", () => {
226 it("matches Ctrl+L on all platforms", () => {
168 - expect(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux")).toBe(true);
169 - expect(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel")).toBe(true);
227 + assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "Linux"));
228 + assert.isTrue(isTerminalClearShortcut(event({ key: "l", ctrlKey: true }), "MacIntel"));
229 });
230
231 it("matches Cmd+K on macOS", () => {
173 - expect(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel")).toBe(true);
232 + assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
233 });
234 });
apps/web/src/keybindings.ts
+11 -3
@@ -305,17 +305,25 @@ function matchesCommandShortcut(
305 command: KeybindingCommand,
306 options?: ShortcutMatchOptions,
307 ): boolean {
308 + return resolveShortcutCommand(event, keybindings, options) === command;
309 +}
310 +
311 +function resolveShortcutCommand(
312 + event: ShortcutEventLike,
313 + keybindings: ResolvedKeybindings,
314 + options?: ShortcutMatchOptions,
315 +): KeybindingCommand | null {
316 const platform = resolvePlatform(options);
317 const context = resolveContext(options);
318
319 for (let index = keybindings.length - 1; index >= 0; index -= 1) {
320 const binding = keybindings[index];
313 - if (!binding || binding.command !== command) continue;
321 + if (!binding) continue;
322 if (!matchesWhenExpression(binding.when, context)) continue;
323 if (!matchesShortcutValue(event, binding.key, platform)) continue;
316 - return true;
324 + return binding.command;
325 }
318 - return false;
326 + return null;
327 }
328
329 function formatShortcutKeyLabel(key: string): string {