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

keybindings.ts

124 lines3.9 KB

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 "chat.new",
37 "chat.newLocal",
38 "editor.openFavorite",
39 ...THREAD_KEYBINDING_COMMANDS,
40] as const;
41​
42export const SCRIPT_RUN_COMMAND_PATTERN = Schema.TemplateLiteral([
43 Schema.Literal("script."),
44 Schema.NonEmptyString.check(
45 Schema.isMaxLength(MAX_SCRIPT_ID_LENGTH),
46 Schema.isPattern(/^[a-z0-9][a-z0-9-]*$/),
47 ),
48 Schema.Literal(".run"),
49]);
50​
51export const KeybindingCommand = Schema.Union([
52 Schema.Literals(STATIC_KEYBINDING_COMMANDS),
53 SCRIPT_RUN_COMMAND_PATTERN,
54]);
55export type KeybindingCommand = typeof KeybindingCommand.Type;
56​
57const KeybindingValue = TrimmedString.check(
58 Schema.isMinLength(1),
59 Schema.isMaxLength(MAX_KEYBINDING_VALUE_LENGTH),
60);
61​
62const KeybindingWhen = TrimmedString.check(
63 Schema.isMinLength(1),
64 Schema.isMaxLength(MAX_KEYBINDING_WHEN_LENGTH),
65);
66export const KeybindingRule = Schema.Struct({
67 key: KeybindingValue,
68 command: KeybindingCommand,
69 when: Schema.optional(KeybindingWhen),
70});
71export type KeybindingRule = typeof KeybindingRule.Type;
72​
73export const KeybindingsConfig = Schema.Array(KeybindingRule).check(
74 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
75);
76export type KeybindingsConfig = typeof KeybindingsConfig.Type;
77​
78export const KeybindingShortcut = Schema.Struct({
79 key: KeybindingValue,
80 metaKey: Schema.Boolean,
81 ctrlKey: Schema.Boolean,
82 shiftKey: Schema.Boolean,
83 altKey: Schema.Boolean,
84 modKey: Schema.Boolean,
85});
86export type KeybindingShortcut = typeof KeybindingShortcut.Type;
87​
88export const KeybindingWhenNode: Schema.Schema<KeybindingWhenNode> = Schema.Union([
89 Schema.Struct({
90 type: Schema.Literal("identifier"),
91 name: Schema.NonEmptyString,
92 }),
93 Schema.Struct({
94 type: Schema.Literal("not"),
95 node: Schema.suspend((): Schema.Schema<KeybindingWhenNode> => KeybindingWhenNode),
96 }),
97 Schema.Struct({
98 type: Schema.Literal("and"),
99 left: Schema.suspend((): Schema.Schema<KeybindingWhenNode> => KeybindingWhenNode),
100 right: Schema.suspend((): Schema.Schema<KeybindingWhenNode> => KeybindingWhenNode),
101 }),
102 Schema.Struct({
103 type: Schema.Literal("or"),
104 left: Schema.suspend((): Schema.Schema<KeybindingWhenNode> => KeybindingWhenNode),
105 right: Schema.suspend((): Schema.Schema<KeybindingWhenNode> => KeybindingWhenNode),
106 }),
107]);
108export type KeybindingWhenNode =
109 | { type: "identifier"; name: string }
110 | { type: "not"; node: KeybindingWhenNode }
111 | { type: "and"; left: KeybindingWhenNode; right: KeybindingWhenNode }
112 | { type: "or"; left: KeybindingWhenNode; right: KeybindingWhenNode };
113​
114export const ResolvedKeybindingRule = Schema.Struct({
115 command: KeybindingCommand,
116 shortcut: KeybindingShortcut,
117 whenAst: Schema.optional(KeybindingWhenNode),
118}).annotate({ parseOptions: { onExcessProperty: "ignore" } });
119export type ResolvedKeybindingRule = typeof ResolvedKeybindingRule.Type;
120​
121export const ResolvedKeybindingsConfig = Schema.Array(ResolvedKeybindingRule).check(
122 Schema.isMaxLength(MAX_KEYBINDINGS_COUNT),
123);
124export type ResolvedKeybindingsConfig = typeof ResolvedKeybindingsConfig.Type;