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

keybindings.ts

234 lines6.4 KB

1import * as Schema from "effect/Schema";
2import { ForwardCompatibleArray, TrimmedString } from "./baseSchemas.ts";
3​
4const MAX_KEYBINDING_VALUE_LENGTH = 64;
5const MAX_KEYBINDING_WHEN_LENGTH = 256;
6export const MAX_WHEN_EXPRESSION_DEPTH = 64;
7export const MAX_SCRIPT_ID_LENGTH = 24;
8export const MAX_KEYBINDINGS_COUNT = 256;
9​
10export const THREAD_JUMP_KEYBINDING_COMMANDS = [
11 "thread.jump.1",
12 "thread.jump.2",
13 "thread.jump.3",
14 "thread.jump.4",
15 "thread.jump.5",
16 "thread.jump.6",
17 "thread.jump.7",
18 "thread.jump.8",
19 "thread.jump.9",
20] as const;
21export type ThreadJumpKeybindingCommand = (typeof THREAD_JUMP_KEYBINDING_COMMANDS)[number];
22​
23export const MODEL_PICKER_JUMP_KEYBINDING_COMMANDS = [
24 "modelPicker.jump.1",
25 "modelPicker.jump.2",
26 "modelPicker.jump.3",
27 "modelPicker.jump.4",
28 "modelPicker.jump.5",
29 "modelPicker.jump.6",
30 "modelPicker.jump.7",
31 "modelPicker.jump.8",
32 "modelPicker.jump.9",
33] as const;
34export type ModelPickerJumpKeybindingCommand =
35 (typeof MODEL_PICKER_JUMP_KEYBINDING_COMMANDS)[number];
36​
37const THREAD_KEYBINDING_COMMANDS = [
38 "thread.stop",
39 "thread.steerQueuedMessage",
40 "thread.editQueuedMessage",
41 "thread.previous",
42 "thread.next",
43 "thread.copyReference",
44 "thread.settle",
45 "thread.pin",
46 "thread.regenerateTitle",
47 "thread.undo",
48 ...THREAD_JUMP_KEYBINDING_COMMANDS,
49] as const;
50​
51const MODEL_PICKER_KEYBINDING_COMMANDS = [
52 "modelPicker.toggle",
53 "modelPicker.previousProvider",
54 "modelPicker.nextProvider",
55 ...MODEL_PICKER_JUMP_KEYBINDING_COMMANDS,
56] as const;
57​
58export const STATIC_KEYBINDING_COMMANDS = [
59 "workspace.focusNext",
60 "workspace.focusPrevious",
61 "sidebar.toggle",
62 "navigation.back",
63 "navigation.forward",
64 "terminal.toggle",
65 "terminal.split",
66 "terminal.splitVertical",
67 "terminal.new",
68 "terminal.close",
69 "terminal.previous",
70 "terminal.next",
71 "rightPanel.toggle",
72 "rightPanel.new",
73 "threadPanel.toggle",
74 "rightPanel.toggleMaximized",
75 "rightPanel.close",
76 "view.reopenClosed",
77 "pullRequest.copyNumber",
78 "diff.toggle",
79 "preview.toggle",
80 "preview.refresh",
81 "preview.focusUrl",
82 "preview.zoomIn",
83 "preview.zoomOut",
84 "preview.resetZoom",
85 "preview.find",
86 "commandPalette.toggle",
87 "filePicker.toggle",
88 "projectSearch.toggle",
89 "usage.open",
90 "theme.select",
91 "appearance.cycle",
92 "themeEditor.toggle",
93 "composer.stash",
94 "composer.sendAlternate",
95 "composer.sendBackground",
96 "composer.sendAndNewThread",
97 "composer.dictation",
98 "composer.host",
99 "composer.cycleHost",
100 "composer.effort",
101 "composer.increaseEffort",
102 "composer.decreaseEffort",
103 "composer.mode",
104 "composer.workspace",
105 "composer.previousWorktree",
106 "composer.branch",
107 "chat.new",
108 "chat.newLocal",
109 "chat.newWithoutProject",
110 "chat.find",
111 "editor.openFavorite",
112 "usage.cost",
113 "usage.tokens",
114 "usage.limits",
115 "usage.period.day",
116 "usage.period.week",
117 "usage.period.month",
118 "usage.period.quarter",
119 ...MODEL_PICKER_KEYBINDING_COMMANDS,
120 ...THREAD_KEYBINDING_COMMANDS,
121] as const;
122​
123export const SCRIPT_RUN_COMMAND_PATTERN = Schema.TemplateLiteral([
124 Schema.Literal("script."),
125 Schema.NonEmptyString.check(
126 Schema.isMaxLength(MAX_SCRIPT_ID_LENGTH),
127 Schema.isPattern(/^[a-z0-9][a-z0-9-]*$/),
128 ),
129 Schema.Literal(".run"),
130]);
131​
132export const KeybindingCommand = Schema.Union([
133 Schema.Literals(STATIC_KEYBINDING_COMMANDS),
134 SCRIPT_RUN_COMMAND_PATTERN,
135]);
136export type KeybindingCommand = typeof KeybindingCommand.Type;
137​
138export const KeybindingValue = TrimmedString.check(
139 Schema.isMinLength(1),
140 Schema.isMaxLength(MAX_KEYBINDING_VALUE_LENGTH),
141);
142​
143export const KeybindingWhen = TrimmedString.check(
144 Schema.isMinLength(1),
145 Schema.isMaxLength(MAX_KEYBINDING_WHEN_LENGTH),
146);
147export const KeybindingRule = Schema.Struct({
148 key: KeybindingValue,
149 command: KeybindingCommand,
150 when: Schema.optional(KeybindingWhen),
151});
152export type KeybindingRule = typeof KeybindingRule.Type;
153​
154export const KeybindingsConfig = Schema.Array(KeybindingRule).check(
155 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
156);
157export type KeybindingsConfig = typeof KeybindingsConfig.Type;
158​
159// Untrimmed: the `space` token resolves to the key " ".
160const KeybindingShortcutKey = Schema.String.check(
161 Schema.isMinLength(1),
162 Schema.isMaxLength(MAX_KEYBINDING_VALUE_LENGTH),
163);
164​
165export const KeybindingShortcut = Schema.Struct({
166 key: KeybindingShortcutKey,
167 metaKey: Schema.Boolean,
168 ctrlKey: Schema.Boolean,
169 shiftKey: Schema.Boolean,
170 altKey: Schema.Boolean,
171 modKey: Schema.Boolean,
172});
173export type KeybindingShortcut = typeof KeybindingShortcut.Type;
174​
175export const PreviewForwardedShortcut = Schema.Struct({
176 command: KeybindingCommand,
177 shortcut: KeybindingShortcut,
178});
179export type PreviewForwardedShortcut = typeof PreviewForwardedShortcut.Type;
180​
181const KeybindingWhenNodeRef = Schema.suspend(
182 (): Schema.Codec<KeybindingWhenNode> => KeybindingWhenNode,
183);
184export const KeybindingWhenNode = Schema.Union([
185 Schema.Struct({
186 type: Schema.Literal("identifier"),
187 name: Schema.NonEmptyString,
188 }),
189 Schema.Struct({
190 type: Schema.Literal("not"),
191 node: KeybindingWhenNodeRef,
192 }),
193 Schema.Struct({
194 type: Schema.Literal("and"),
195 left: KeybindingWhenNodeRef,
196 right: KeybindingWhenNodeRef,
197 }),
198 Schema.Struct({
199 type: Schema.Literal("or"),
200 left: KeybindingWhenNodeRef,
201 right: KeybindingWhenNodeRef,
202 }),
203]);
204export type KeybindingWhenNode =
205 | { type: "identifier"; name: string }
206 | { type: "not"; node: KeybindingWhenNode }
207 | { type: "and"; left: KeybindingWhenNode; right: KeybindingWhenNode }
208 | { type: "or"; left: KeybindingWhenNode; right: KeybindingWhenNode };
209​
210export const ResolvedKeybindingRule = Schema.Struct({
211 command: KeybindingCommand,
212 shortcut: KeybindingShortcut,
213 whenAst: Schema.optional(KeybindingWhenNode),
214}).annotate({ parseOptions: { onExcessProperty: "ignore" } });
215export type ResolvedKeybindingRule = typeof ResolvedKeybindingRule.Type;
216​
217/** Drops undecodable rules without failing the connection. */
218export const ResolvedKeybindingsConfig = ForwardCompatibleArray(ResolvedKeybindingRule).check(
219 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
220);
221export type ResolvedKeybindingsConfig = typeof ResolvedKeybindingsConfig.Type;
222​
223export class KeybindingsConfigError extends Schema.TaggedError<KeybindingsConfigError>()(
224 "KeybindingsConfigParseError",
225 {
226 configPath: Schema.String,
227 detail: Schema.String,
228 cause: Schema.optional(Schema.Defect()),
229 },
230) {
231 override get message(): string {
232 return `Unable to parse keybindings config at ${this.configPath}: ${this.detail}`;
233 }