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