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

keybindings.ts

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