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

keybindings.ts

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