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

keybindings.ts

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