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