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