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

keybindings.ts

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