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

keybindings.ts

208 lines6.0 KB

1import * as Schema from "effect/Schema";
2import { ForwardCompatibleArray, TrimmedString } from "./baseSchemas.ts";
3​
4export const 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;
49export type ThreadKeybindingCommand = (typeof THREAD_KEYBINDING_COMMANDS)[number];
50​
51const MODEL_PICKER_KEYBINDING_COMMANDS = [
52 "modelPicker.toggle",
53 "modelPicker.previousProvider",
54 "modelPicker.nextProvider",
55 ...MODEL_PICKER_JUMP_KEYBINDING_COMMANDS,
56] as const;
57export type ModelPickerKeybindingCommand = (typeof MODEL_PICKER_KEYBINDING_COMMANDS)[number];
58​
59export const STATIC_KEYBINDING_COMMANDS = [
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 "threadPanel.toggle",
70 "rightPanel.toggleMaximized",
71 "rightPanel.close",
72 "pullRequest.copyNumber",
73 "diff.toggle",
74 "preview.toggle",
75 "preview.refresh",
76 "preview.focusUrl",
77 "preview.zoomIn",
78 "preview.zoomOut",
79 "preview.resetZoom",
80 "commandPalette.toggle",
81 "filePicker.toggle",
82 "projectSearch.toggle",
83 "usage.open",
84 "theme.select",
85 "appearance.cycle",
86 "themeEditor.toggle",
87 "composer.stash",
88 "composer.sendAlternate",
89 "composer.sendBackground",
90 "composer.host",
91 "composer.effort",
92 "composer.mode",
93 "composer.workspace",
94 "composer.previousWorktree",
95 "composer.branch",
96 "chat.new",
97 "chat.newLocal",
98 "editor.openFavorite",
99 ...MODEL_PICKER_KEYBINDING_COMMANDS,
100 ...THREAD_KEYBINDING_COMMANDS,
101] as const;
102​
103export const SCRIPT_RUN_COMMAND_PATTERN = Schema.TemplateLiteral([
104 Schema.Literal("script."),
105 Schema.NonEmptyString.check(
106 Schema.isMaxLength(MAX_SCRIPT_ID_LENGTH),
107 Schema.isPattern(/^[a-z0-9][a-z0-9-]*$/),
108 ),
109 Schema.Literal(".run"),
110]);
111​
112export const KeybindingCommand = Schema.Union([
113 Schema.Literals(STATIC_KEYBINDING_COMMANDS),
114 SCRIPT_RUN_COMMAND_PATTERN,
115]);
116export type KeybindingCommand = typeof KeybindingCommand.Type;
117​
118export const KeybindingValue = TrimmedString.check(
119 Schema.isMinLength(1),
120 Schema.isMaxLength(MAX_KEYBINDING_VALUE_LENGTH),
121);
122​
123export const KeybindingWhen = TrimmedString.check(
124 Schema.isMinLength(1),
125 Schema.isMaxLength(MAX_KEYBINDING_WHEN_LENGTH),
126);
127export const KeybindingRule = Schema.Struct({
128 key: KeybindingValue,
129 command: KeybindingCommand,
130 when: Schema.optional(KeybindingWhen),
131});
132export type KeybindingRule = typeof KeybindingRule.Type;
133​
134export const KeybindingsConfig = Schema.Array(KeybindingRule).check(
135 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
136);
137export type KeybindingsConfig = typeof KeybindingsConfig.Type;
138​
139export const KeybindingShortcut = Schema.Struct({
140 key: KeybindingValue,
141 metaKey: Schema.Boolean,
142 ctrlKey: Schema.Boolean,
143 shiftKey: Schema.Boolean,
144 altKey: Schema.Boolean,
145 modKey: Schema.Boolean,
146});
147export type KeybindingShortcut = typeof KeybindingShortcut.Type;
148​
149const KeybindingWhenNodeRef = Schema.suspend(
150 (): Schema.Codec<KeybindingWhenNode> => KeybindingWhenNode,
151);
152export const KeybindingWhenNode = Schema.Union([
153 Schema.Struct({
154 type: Schema.Literal("identifier"),
155 name: Schema.NonEmptyString,
156 }),
157 Schema.Struct({
158 type: Schema.Literal("not"),
159 node: KeybindingWhenNodeRef,
160 }),
161 Schema.Struct({
162 type: Schema.Literal("and"),
163 left: KeybindingWhenNodeRef,
164 right: KeybindingWhenNodeRef,
165 }),
166 Schema.Struct({
167 type: Schema.Literal("or"),
168 left: KeybindingWhenNodeRef,
169 right: KeybindingWhenNodeRef,
170 }),
171]);
172export type KeybindingWhenNode =
173 | { type: "identifier"; name: string }
174 | { type: "not"; node: KeybindingWhenNode }
175 | { type: "and"; left: KeybindingWhenNode; right: KeybindingWhenNode }
176 | { type: "or"; left: KeybindingWhenNode; right: KeybindingWhenNode };
177​
178export const ResolvedKeybindingRule = Schema.Struct({
179 command: KeybindingCommand,
180 shortcut: KeybindingShortcut,
181 whenAst: Schema.optional(KeybindingWhenNode),
182}).annotate({ parseOptions: { onExcessProperty: "ignore" } });
183export type ResolvedKeybindingRule = typeof ResolvedKeybindingRule.Type;
184​
185/**
186 * The command set grows over time, so a client may receive rules it cannot
187 * represent (a command or `when` node added after that client shipped).
188 * Decoding drops those rules instead of failing the whole payload —
189 * rejecting the config would take down the connection over a shortcut the
190 * client couldn't dispatch anyway.
191 */
192export const ResolvedKeybindingsConfig = ForwardCompatibleArray(ResolvedKeybindingRule).check(
193 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
194);
195export type ResolvedKeybindingsConfig = typeof ResolvedKeybindingsConfig.Type;
196​
197export class KeybindingsConfigError extends Schema.TaggedError<KeybindingsConfigError>()(
198 "KeybindingsConfigParseError",
199 {
200 configPath: Schema.String,
201 detail: Schema.String,
202 cause: Schema.optional(Schema.Defect()),
203 },
204) {
205 override get message(): string {
206 return `Unable to parse keybindings config at ${this.configPath}: ${this.detail}`;
207 }