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

keybindings.ts

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