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

keybindings.ts

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