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

keybindings.ts

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