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

keybindings.ts

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