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

keybindings.ts

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