Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

keybindings.ts

331 lines9.7 KB

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