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

keybindings.ts

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