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

keybindings.ts

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