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

keybindings.ts

188 lines5.6 KB

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