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
10const STATIC_KEYBINDING_COMMANDS = [
11 "terminal.toggle",
12 "terminal.split",
13 "terminal.new",
14 "terminal.close",
15 "diff.toggle",
16 "chat.new",
17 "chat.newLocal",
18 "editor.openFavorite",
19] as const;
20
21export const SCRIPT_RUN_COMMAND_PATTERN = Schema.TemplateLiteral([
22 Schema.Literal("script."),
23 Schema.NonEmptyString.check(
24 Schema.isMaxLength(MAX_SCRIPT_ID_LENGTH),
25 Schema.isPattern(/^[a-z0-9][a-z0-9-]*$/),
26 ),
27 Schema.Literal(".run"),
28]);
29
30export const KeybindingCommand = Schema.Union([
31 Schema.Literals(STATIC_KEYBINDING_COMMANDS),
32 SCRIPT_RUN_COMMAND_PATTERN,
33]);
34export type KeybindingCommand = typeof KeybindingCommand.Type;
35
36const KeybindingValue = TrimmedString.check(
37 Schema.isMinLength(1),
38 Schema.isMaxLength(MAX_KEYBINDING_VALUE_LENGTH),
39);
40
41const KeybindingWhen = TrimmedString.check(
42 Schema.isMinLength(1),
43 Schema.isMaxLength(MAX_KEYBINDING_WHEN_LENGTH),
44);
45export const KeybindingRule = Schema.Struct({
46 key: KeybindingValue,
47 command: KeybindingCommand,
48 when: Schema.optional(KeybindingWhen),
49});
50export type KeybindingRule = typeof KeybindingRule.Type;
51
52export const KeybindingsConfig = Schema.Array(KeybindingRule).check(
53 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
54);
55export type KeybindingsConfig = typeof KeybindingsConfig.Type;
56
57export const KeybindingShortcut = Schema.Struct({
58 key: KeybindingValue,
59 metaKey: Schema.Boolean,
60 ctrlKey: Schema.Boolean,
61 shiftKey: Schema.Boolean,
62 altKey: Schema.Boolean,
63 modKey: Schema.Boolean,
64});
65export type KeybindingShortcut = typeof KeybindingShortcut.Type;
66
67export const KeybindingWhenNode: Schema.Schema<KeybindingWhenNode> = Schema.Union([
68 Schema.Struct({
69 type: Schema.Literal("identifier"),
70 name: Schema.NonEmptyString,
71 }),
72 Schema.Struct({
73 type: Schema.Literal("not"),
74 node: Schema.suspend((): Schema.Schema<KeybindingWhenNode> => KeybindingWhenNode),
75 }),
76 Schema.Struct({
77 type: Schema.Literal("and"),
78 left: Schema.suspend((): Schema.Schema<KeybindingWhenNode> => KeybindingWhenNode),
79 right: Schema.suspend((): Schema.Schema<KeybindingWhenNode> => KeybindingWhenNode),
80 }),
81 Schema.Struct({
82 type: Schema.Literal("or"),
83 left: Schema.suspend((): Schema.Schema<KeybindingWhenNode> => KeybindingWhenNode),
84 right: Schema.suspend((): Schema.Schema<KeybindingWhenNode> => KeybindingWhenNode),
85 }),
86]);
87export type KeybindingWhenNode =
88 | { type: "identifier"; name: string }
89 | { type: "not"; node: KeybindingWhenNode }
90 | { type: "and"; left: KeybindingWhenNode; right: KeybindingWhenNode }
91 | { type: "or"; left: KeybindingWhenNode; right: KeybindingWhenNode };
92
93export const ResolvedKeybindingRule = Schema.Struct({
94 command: KeybindingCommand,
95 shortcut: KeybindingShortcut,
96 whenAst: Schema.optional(KeybindingWhenNode),
97}).annotate({ parseOptions: { onExcessProperty: "ignore" } });
98export type ResolvedKeybindingRule = typeof ResolvedKeybindingRule.Type;
99
100export const ResolvedKeybindingsConfig = Schema.Array(ResolvedKeybindingRule).check(
101 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
102);
103export type ResolvedKeybindingsConfig = typeof ResolvedKeybindingsConfig.Type;