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