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