Open History to see the last commit
1import {
2 type KeybindingRule,
3 type KeybindingShortcut,
4 type KeybindingWhenNode,
5 MAX_KEYBINDINGS_COUNT,
6 MAX_WHEN_EXPRESSION_DEPTH,
7 MODEL_PICKER_JUMP_KEYBINDING_COMMANDS,
8 type ResolvedKeybindingRule,
9 type ResolvedKeybindingsConfig,
10 THREAD_JUMP_KEYBINDING_COMMANDS,
11} from "@t3tools/contracts";
12
13type WhenToken =
14 | { type: "identifier"; value: string }
15 | { type: "not" }
16 | { type: "and" }
17 | { type: "or" }
18 | { type: "lparen" }
19 | { type: "rparen" };
20
21export const DEFAULT_KEYBINDINGS: ReadonlyArray<KeybindingRule> = [
22 { key: "mod+b", command: "sidebar.toggle" },
23 { key: "mod+j", command: "terminal.toggle" },
24 { key: "mod+alt+b", command: "rightPanel.toggle" },
25 { key: "mod+d", command: "terminal.split", when: "terminalFocus" },
26 { key: "mod+shift+d", command: "terminal.splitVertical", when: "terminalFocus" },
27 { key: "mod+n", command: "terminal.new", when: "terminalFocus" },
28 { key: "mod+w", command: "terminal.close", when: "terminalFocus" },
29 { key: "mod+w", command: "rightPanel.close", when: "!terminalFocus" },
30 { key: "mod+d", command: "diff.toggle", when: "!terminalFocus" },
31 { key: "mod+shift+j", command: "preview.toggle" },
32 { key: "mod+r", command: "preview.refresh", when: "previewFocus" },
33 { key: "mod+l", command: "preview.focusUrl", when: "previewFocus" },
34 { key: "mod+=", command: "preview.zoomIn", when: "previewFocus" },
35 { key: "mod++", command: "preview.zoomIn", when: "previewFocus" },
36 { key: "mod+-", command: "preview.zoomOut", when: "previewFocus" },
37 { key: "mod+0", command: "preview.resetZoom", when: "previewFocus" },
38 { key: "mod+k", command: "commandPalette.toggle", when: "!terminalFocus" },
39 { key: "mod+p", command: "filePicker.toggle", when: "!terminalFocus" },
40 { key: "mod+shift+f", command: "projectSearch.toggle", when: "!terminalFocus" },
41 { key: "mod+alt+a", command: "theme.select", when: "!terminalFocus" },
42 { key: "mod+alt+shift+a", command: "appearance.cycle", when: "!terminalFocus" },
43 { key: "mod+alt+shift+t", command: "themeEditor.toggle" },
44 { key: "mod+s", command: "composer.stash", when: "!terminalFocus" },
45 { key: "mod+shift+enter", command: "thread.steerQueuedMessage", when: "!terminalFocus" },
46 { key: "mod+n", command: "chat.new", when: "!terminalFocus" },
47 { key: "mod+shift+o", command: "chat.new", when: "!terminalFocus" },
48 { key: "mod+shift+n", command: "chat.newLocal", when: "!terminalFocus" },
49 { key: "mod+shift+m", command: "modelPicker.toggle", when: "!terminalFocus" },
50 { key: "mod+shift+h", command: "composer.host", when: "!terminalFocus" },
51 { key: "mod+shift+e", command: "composer.effort", when: "!terminalFocus" },
52 { key: "mod+shift+a", command: "composer.mode", when: "!terminalFocus" },
53 { key: "mod+shift+x", command: "composer.workspace", when: "!terminalFocus" },
54 { key: "mod+shift+g", command: "composer.branch", when: "!terminalFocus" },
55 { key: "mod+shift+l", command: "composer.previousWorktree", when: "!terminalFocus" },
56 { key: "mod+shift+k", command: "pullRequest.copyNumber", when: "!terminalFocus" },
57 { key: "mod+shift+arrowup", command: "modelPicker.previousProvider", when: "modelPickerOpen" },
58 { key: "mod+shift+arrowdown", command: "modelPicker.nextProvider", when: "modelPickerOpen" },
59 { key: "mod+o", command: "editor.openFavorite" },
60 { key: "mod+shift+[", command: "thread.previous" },
61 { key: "mod+shift+]", command: "thread.next" },
62 { key: "mod+shift+c", command: "thread.copyReference", when: "!terminalFocus" },
63 { key: "mod+shift+s", command: "thread.settle", when: "!terminalFocus" },
64 { key: "mod+shift+p", command: "thread.pin", when: "!terminalFocus" },
65 ...THREAD_JUMP_KEYBINDING_COMMANDS.map((command, index) => ({
66 key: `mod+${index + 1}`,
67 command,
68 })),
69 ...MODEL_PICKER_JUMP_KEYBINDING_COMMANDS.map((command, index) => ({
70 key: `mod+${index + 1}`,
71 command,
72 when: "modelPickerOpen",
73 })),
74];
75
76function normalizeKeyToken(token: string): string {
77 if (token === "space") return " ";
78 if (token === "esc") return "escape";
79 return token;
80}
81
82export function parseKeybindingShortcut(value: string): KeybindingShortcut | null {
83 const rawTokens = value
84 .toLowerCase()
85 .split("+")
86 .map((token) => token.trim());
87 const tokens = [...rawTokens];
88 let trailingEmptyCount = 0;
89 while (tokens[tokens.length - 1] === "") {
90 trailingEmptyCount += 1;
91 tokens.pop();
92 }
93 if (trailingEmptyCount > 0) {
94 tokens.push("+");
95 }
96 if (tokens.some((token) => token.length === 0)) {
97 return null;
98 }
99 if (tokens.length === 0) return null;
100
101 let key: string | null = null;
102 let metaKey = false;
103 let ctrlKey = false;
104 let shiftKey = false;
105 let altKey = false;
106 let modKey = false;
107
108 for (const token of tokens) {
109 switch (token) {
110 case "cmd":
111 case "meta":
112 metaKey = true;
113 break;
114 case "ctrl":
115 case "control":
116 ctrlKey = true;
117 break;
118 case "shift":
119 shiftKey = true;
120 break;
121 case "alt":
122 case "option":
123 altKey = true;
124 break;
125 case "mod":
126 modKey = true;
127 break;
128 default: {
129 if (key !== null) return null;
130 key = normalizeKeyToken(token);
131 }
132 }
133 }
134
135 if (key === null) return null;
136 return {
137 key,
138 metaKey,
139 ctrlKey,
140 shiftKey,
141 altKey,
142 modKey,
143 };
144}
145
146function tokenizeWhenExpression(expression: string): WhenToken[] | null {
147 const tokens: WhenToken[] = [];
148 let index = 0;
149
150 while (index < expression.length) {
151 const current = expression[index];
152 if (!current) break;
153
154 if (/\s/.test(current)) {
155 index += 1;
156 continue;
157 }
158 if (expression.startsWith("&&", index)) {
159 tokens.push({ type: "and" });
160 index += 2;
161 continue;
162 }
163 if (expression.startsWith("||", index)) {
164 tokens.push({ type: "or" });
165 index += 2;
166 continue;
167 }
168 if (current === "!") {
169 tokens.push({ type: "not" });
170 index += 1;
171 continue;
172 }
173 if (current === "(") {
174 tokens.push({ type: "lparen" });
175 index += 1;
176 continue;
177 }
178 if (current === ")") {
179 tokens.push({ type: "rparen" });
180 index += 1;
181 continue;
182 }
183
184 const identifier = /^[A-Za-z_][A-Za-z0-9_.-]*/.exec(expression.slice(index));
185 if (!identifier) {
186 return null;
187 }
188 tokens.push({ type: "identifier", value: identifier[0] });
189 index += identifier[0].length;
190 }
191
192 return tokens;
193}
194
195export function parseKeybindingWhenExpression(expression: string): KeybindingWhenNode | null {
196 const tokens = tokenizeWhenExpression(expression);
197 if (!tokens || tokens.length === 0) return null;
198 let index = 0;
199
200 const parsePrimary = (depth: number): KeybindingWhenNode | null => {
201 if (depth > MAX_WHEN_EXPRESSION_DEPTH) {
202 return null;
203 }
204 const token = tokens[index];
205 if (!token) return null;
206
207 if (token.type === "identifier") {
208 index += 1;
209 return { type: "identifier", name: token.value };
210 }
211
212 if (token.type === "lparen") {
213 index += 1;
214 const expressionNode = parseOr(depth + 1);
215 const closeToken = tokens[index];
216 if (!expressionNode || !closeToken || closeToken.type !== "rparen") {
217 return null;
218 }
219 index += 1;
220 return expressionNode;
221 }
222
223 return null;
224 };
225
226 const parseUnary = (depth: number): KeybindingWhenNode | null => {
227 let notCount = 0;
228 while (tokens[index]?.type === "not") {
229 index += 1;
230 notCount += 1;
231 if (notCount > MAX_WHEN_EXPRESSION_DEPTH) {
232 return null;
233 }
234 }
235
236 let node = parsePrimary(depth);
237 if (!node) return null;
238
239 while (notCount > 0) {
240 node = { type: "not", node };
241 notCount -= 1;
242 }
243
244 return node;
245 };
246
247 const parseAnd = (depth: number): KeybindingWhenNode | null => {
248 let left = parseUnary(depth);
249 if (!left) return null;
250
251 while (tokens[index]?.type === "and") {
252 index += 1;
253 const right = parseUnary(depth);
254 if (!right) return null;
255 left = { type: "and", left, right };
256 }
257
258 return left;
259 };
260
261 const parseOr = (depth: number): KeybindingWhenNode | null => {
262 let left = parseAnd(depth);
263 if (!left) return null;
264
265 while (tokens[index]?.type === "or") {
266 index += 1;
267 const right = parseAnd(depth);
268 if (!right) return null;
269 left = { type: "or", left, right };
270 }
271
272 return left;
273 };
274
275 const ast = parseOr(0);
276 if (!ast || index !== tokens.length) return null;
277 return ast;
278}
279
280export function compileResolvedKeybindingRule(rule: KeybindingRule): ResolvedKeybindingRule | null {
281 const shortcut = parseKeybindingShortcut(rule.key);
282 if (!shortcut) return null;
283
284 if (rule.when !== undefined) {
285 const whenAst = parseKeybindingWhenExpression(rule.when);
286 if (!whenAst) return null;
287 return {
288 command: rule.command,
289 shortcut,
290 whenAst,
291 };
292 }
293
294 return {
295 command: rule.command,
296 shortcut,
297 };
298}
299
300export function compileResolvedKeybindingsConfig(
301 config: ReadonlyArray<KeybindingRule>,
302): ResolvedKeybindingsConfig {
303 const compiled: ResolvedKeybindingRule[] = [];
304 for (const rule of config) {
305 const result = compileResolvedKeybindingRule(rule);
306 if (result) {
307 compiled.push(result);
308 }
309 }
310 return compiled.slice(-MAX_KEYBINDINGS_COUNT);
311}
312
313export const DEFAULT_RESOLVED_KEYBINDINGS = compileResolvedKeybindingsConfig(DEFAULT_KEYBINDINGS);
314
315export function mergeWithDefaultKeybindings(
316 custom: ResolvedKeybindingsConfig,
317): ResolvedKeybindingsConfig {
318 if (custom.length === 0) {
319 return [...DEFAULT_RESOLVED_KEYBINDINGS];
320 }
321
322 const overriddenCommands = new Set(custom.map((binding) => binding.command));
323 const retainedDefaults = DEFAULT_RESOLVED_KEYBINDINGS.filter(
324 (binding) => !overriddenCommands.has(binding.command),
325 );
326 const merged = [...retainedDefaults, ...custom];
327
328 if (merged.length <= MAX_KEYBINDINGS_COUNT) {
329 return merged;
330 }
331
332 return merged.slice(-MAX_KEYBINDINGS_COUNT);
333}