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

keybindings.ts

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