Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { 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 "composer.stash",
67 "chat.new",
68 "chat.newLocal",
69 "editor.openFavorite",
70 ...MODEL_PICKER_KEYBINDING_COMMANDS,
71 ...THREAD_KEYBINDING_COMMANDS,
72] as const;
73
74export const SCRIPT_RUN_COMMAND_PATTERN = Schema.TemplateLiteral([
75 Schema.Literal("script."),
76 Schema.NonEmptyString.check(
77 Schema.isMaxLength(MAX_SCRIPT_ID_LENGTH),
78 Schema.isPattern(/^[a-z0-9][a-z0-9-]*$/),
79 ),
80 Schema.Literal(".run"),
81]);
82
83export const KeybindingCommand = Schema.Union([
84 Schema.Literals(STATIC_KEYBINDING_COMMANDS),
85 SCRIPT_RUN_COMMAND_PATTERN,
86]);
87export type KeybindingCommand = typeof KeybindingCommand.Type;
88
89export const KeybindingValue = TrimmedString.check(
90 Schema.isMinLength(1),
91 Schema.isMaxLength(MAX_KEYBINDING_VALUE_LENGTH),
92);
93
94export const KeybindingWhen = TrimmedString.check(
95 Schema.isMinLength(1),
96 Schema.isMaxLength(MAX_KEYBINDING_WHEN_LENGTH),
97);
98export const KeybindingRule = Schema.Struct({
99 key: KeybindingValue,
100 command: KeybindingCommand,
101 when: Schema.optional(KeybindingWhen),
102});
103export type KeybindingRule = typeof KeybindingRule.Type;
104
105export const KeybindingsConfig = Schema.Array(KeybindingRule).check(
106 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
107);
108export type KeybindingsConfig = typeof KeybindingsConfig.Type;
109
110export const KeybindingShortcut = Schema.Struct({
111 key: KeybindingValue,
112 metaKey: Schema.Boolean,
113 ctrlKey: Schema.Boolean,
114 shiftKey: Schema.Boolean,
115 altKey: Schema.Boolean,
116 modKey: Schema.Boolean,
117});
118export type KeybindingShortcut = typeof KeybindingShortcut.Type;
119
120const KeybindingWhenNodeRef = Schema.suspend(
121 (): Schema.Codec<KeybindingWhenNode> => KeybindingWhenNode,
122);
123export const KeybindingWhenNode = Schema.Union([
124 Schema.Struct({
125 type: Schema.Literal("identifier"),
126 name: Schema.NonEmptyString,
127 }),
128 Schema.Struct({
129 type: Schema.Literal("not"),
130 node: KeybindingWhenNodeRef,
131 }),
132 Schema.Struct({
133 type: Schema.Literal("and"),
134 left: KeybindingWhenNodeRef,
135 right: KeybindingWhenNodeRef,
136 }),
137 Schema.Struct({
138 type: Schema.Literal("or"),
139 left: KeybindingWhenNodeRef,
140 right: KeybindingWhenNodeRef,
141 }),
142]);
143export type KeybindingWhenNode =
144 | { type: "identifier"; name: string }
145 | { type: "not"; node: KeybindingWhenNode }
146 | { type: "and"; left: KeybindingWhenNode; right: KeybindingWhenNode }
147 | { type: "or"; left: KeybindingWhenNode; right: KeybindingWhenNode };
148
149export const ResolvedKeybindingRule = Schema.Struct({
150 command: KeybindingCommand,
151 shortcut: KeybindingShortcut,
152 whenAst: Schema.optional(KeybindingWhenNode),
153}).annotate({ parseOptions: { onExcessProperty: "ignore" } });
154export type ResolvedKeybindingRule = typeof ResolvedKeybindingRule.Type;
155
156export const ResolvedKeybindingsConfig = Schema.Array(ResolvedKeybindingRule).check(
157 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
158);
159export type ResolvedKeybindingsConfig = typeof ResolvedKeybindingsConfig.Type;
160
161export class KeybindingsConfigError extends Schema.TaggedErrorClass<KeybindingsConfigError>()(
162 "KeybindingsConfigParseError",
163 {
164 configPath: Schema.String,
165 detail: Schema.String,
166 cause: Schema.optional(Schema.Defect()),
167 },
168) {
169 override get message(): string {
170 return `Unable to parse keybindings config at ${this.configPath}: ${this.detail}`;
171 }
172}