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

keybindings.ts

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