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

keybindings.ts

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