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

keybindings.ts

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