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

keybindings.ts

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