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

keybindings.ts

171 lines5.0 KB

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