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

keybindings.ts

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