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

keybindings.ts

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