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

keybindings.ts

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