Open History to see the last commit
1import { Schema } from "effect";
2import { TrimmedString } from "./baseSchemas";
3
4export const MAX_KEYBINDING_VALUE_LENGTH = 64;
5const 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 THREAD_KEYBINDING_COMMANDS = [
24 "thread.previous",
25 "thread.next",
26 ...THREAD_JUMP_KEYBINDING_COMMANDS,
27] as const;
28export type ThreadKeybindingCommand = (typeof THREAD_KEYBINDING_COMMANDS)[number];
29
30const STATIC_KEYBINDING_COMMANDS = [
31 "terminal.toggle",
32 "terminal.split",
33 "terminal.new",
34 "terminal.close",
35 "diff.toggle",
36 "commandPalette.toggle",
37 "chat.new",
38 "chat.newLocal",
39 "editor.openFavorite",
40 ...THREAD_KEYBINDING_COMMANDS,
41] as const;
42
43export const SCRIPT_RUN_COMMAND_PATTERN = Schema.TemplateLiteral([
44 Schema.Literal("script."),
45 Schema.NonEmptyString.check(
46 Schema.isMaxLength(MAX_SCRIPT_ID_LENGTH),
47 Schema.isPattern(/^[a-z0-9][a-z0-9-]*$/),
48 ),
49 Schema.Literal(".run"),
50]);
51
52export const KeybindingCommand = Schema.Union([
53 Schema.Literals(STATIC_KEYBINDING_COMMANDS),
54 SCRIPT_RUN_COMMAND_PATTERN,
55]);
56export type KeybindingCommand = typeof KeybindingCommand.Type;
57
58const KeybindingValue = TrimmedString.check(
59 Schema.isMinLength(1),
60 Schema.isMaxLength(MAX_KEYBINDING_VALUE_LENGTH),
61);
62
63const KeybindingWhen = TrimmedString.check(
64 Schema.isMinLength(1),
65 Schema.isMaxLength(MAX_KEYBINDING_WHEN_LENGTH),
66);
67export const KeybindingRule = Schema.Struct({
68 key: KeybindingValue,
69 command: KeybindingCommand,
70 when: Schema.optional(KeybindingWhen),
71});
72export type KeybindingRule = typeof KeybindingRule.Type;
73
74export const KeybindingsConfig = Schema.Array(KeybindingRule).check(
75 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
76);
77export type KeybindingsConfig = typeof KeybindingsConfig.Type;
78
79export const KeybindingShortcut = Schema.Struct({
80 key: KeybindingValue,
81 metaKey: Schema.Boolean,
82 ctrlKey: Schema.Boolean,
83 shiftKey: Schema.Boolean,
84 altKey: Schema.Boolean,
85 modKey: Schema.Boolean,
86});
87export type KeybindingShortcut = typeof KeybindingShortcut.Type;
88
89const KeybindingWhenNodeRef = Schema.suspend(
90 (): Schema.Codec<KeybindingWhenNode> => KeybindingWhenNode,
91);
92export const KeybindingWhenNode = Schema.Union([
93 Schema.Struct({
94 type: Schema.Literal("identifier"),
95 name: Schema.NonEmptyString,
96 }),
97 Schema.Struct({
98 type: Schema.Literal("not"),
99 node: KeybindingWhenNodeRef,
100 }),
101 Schema.Struct({
102 type: Schema.Literal("and"),
103 left: KeybindingWhenNodeRef,
104 right: KeybindingWhenNodeRef,
105 }),
106 Schema.Struct({
107 type: Schema.Literal("or"),
108 left: KeybindingWhenNodeRef,
109 right: KeybindingWhenNodeRef,
110 }),
111]);
112export type KeybindingWhenNode =
113 | { type: "identifier"; name: string }
114 | { type: "not"; node: KeybindingWhenNode }
115 | { type: "and"; left: KeybindingWhenNode; right: KeybindingWhenNode }
116 | { type: "or"; left: KeybindingWhenNode; right: KeybindingWhenNode };
117
118export const ResolvedKeybindingRule = Schema.Struct({
119 command: KeybindingCommand,
120 shortcut: KeybindingShortcut,
121 whenAst: Schema.optional(KeybindingWhenNode),
122}).annotate({ parseOptions: { onExcessProperty: "ignore" } });
123export type ResolvedKeybindingRule = typeof ResolvedKeybindingRule.Type;
124
125export const ResolvedKeybindingsConfig = Schema.Array(ResolvedKeybindingRule).check(
126 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
127);
128export type ResolvedKeybindingsConfig = typeof ResolvedKeybindingsConfig.Type;
129
130export class KeybindingsConfigError extends Schema.TaggedErrorClass<KeybindingsConfigError>()(
131 "KeybindingsConfigParseError",
132 {
133 configPath: Schema.String,
134 detail: Schema.String,
135 cause: Schema.optional(Schema.Defect),
136 },
137) {
138 override get message(): string {
139 return `Unable to parse keybindings config at ${this.configPath}: ${this.detail}`;
140 }
141}