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