@inflatable-cookie/longhorn 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/README.md +2 -2
  2. package/package.json +1 -7
  3. package/src/agent-control/generated/protocol.ts +228 -0
  4. package/src/agent-control/index.ts +1 -5
  5. package/src/agent-control/inject.ts +4 -1
  6. package/src/agent-control/selection.ts +42 -25
  7. package/src/agent-control/shim.ts +428 -198
  8. package/src/bridge/direct.ts +1 -1
  9. package/src/bridge/generated/fields.ts +4 -3
  10. package/src/bridge/generated/protocol.ts +62 -7
  11. package/src/bridge/generated/variant-fields.ts +1 -1
  12. package/src/bridge/job.ts +49 -12
  13. package/src/bridge/loopback.ts +7 -25
  14. package/src/bridge/operation.ts +13 -43
  15. package/src/bridge/router.ts +10 -23
  16. package/src/bridge/runtime/connection.ts +78 -109
  17. package/src/bridge/runtime/policy.ts +8 -28
  18. package/src/bridge/runtime/retry.ts +6 -18
  19. package/src/bridge/session.ts +4 -14
  20. package/src/bridge/stream/connection.ts +24 -55
  21. package/src/bridge/stream/source.ts +2 -6
  22. package/src/bridge/stream/tracker.ts +12 -12
  23. package/src/bridge/supervision.ts +104 -97
  24. package/src/bridge/validation/base.ts +21 -63
  25. package/src/bridge/validation/jobs.ts +11 -25
  26. package/src/bridge/validation/negotiation.ts +28 -43
  27. package/src/bridge/validation/operations.ts +29 -65
  28. package/src/bridge/validation/streams.ts +3 -10
  29. package/src/commands/client.ts +37 -28
  30. package/src/commands/controller.ts +119 -96
  31. package/src/commands/generated/fields.ts +7 -6
  32. package/src/commands/generated/protocol.ts +28 -1
  33. package/src/commands/generated/variant-fields.ts +1 -1
  34. package/src/commands/keyboard.ts +94 -100
  35. package/src/commands/physical-code.ts +11 -0
  36. package/src/commands/ports.ts +8 -13
  37. package/src/commands/projectors.ts +100 -75
  38. package/src/commands/validation.ts +434 -241
  39. package/src/config/client.ts +9 -22
  40. package/src/config/generated/base.ts +22 -6
  41. package/src/config/generated/fields.ts +4 -3
  42. package/src/config/generated/label-template.ts +1 -1
  43. package/src/config/generated/labels.ts +1 -1
  44. package/src/config/generated/protocol.ts +1 -1
  45. package/src/config/generated/restore.ts +35 -12
  46. package/src/config/generated/variant-fields.ts +1 -1
  47. package/src/config/names.ts +6 -12
  48. package/src/config/validation/commands.ts +11 -25
  49. package/src/config/validation/outcomes.ts +163 -70
  50. package/src/config/validation/primitives.ts +35 -80
  51. package/src/config/validation/projection.ts +100 -128
  52. package/src/core/connection.ts +187 -83
  53. package/src/core/controller-lifecycle.ts +144 -0
  54. package/src/core/generated/identifiers.ts +4 -0
  55. package/src/core/identifiers.ts +40 -0
  56. package/src/core/index.ts +10 -1
  57. package/src/core/json-value.ts +58 -0
  58. package/src/core/listeners.ts +35 -0
  59. package/src/core/record.ts +80 -0
  60. package/src/core/transport.ts +9 -11
  61. package/src/core/wire-copy.ts +7 -0
  62. package/src/history/client.ts +11 -13
  63. package/src/history/controller.ts +106 -108
  64. package/src/history/direct.ts +1 -3
  65. package/src/history/generated/fields.ts +4 -3
  66. package/src/history/generated/protocol.ts +8 -5
  67. package/src/history/generated/variant-fields.ts +1 -1
  68. package/src/history/ports.ts +7 -4
  69. package/src/history/serialized.ts +9 -13
  70. package/src/history/validation.ts +69 -75
  71. package/src/history-tree/client.ts +89 -11
  72. package/src/history-tree/controller.ts +380 -96
  73. package/src/history-tree/direct.ts +3 -1
  74. package/src/history-tree/generated/fields.ts +5 -3
  75. package/src/history-tree/generated/protocol.ts +43 -7
  76. package/src/history-tree/generated/variant-fields.ts +7 -1
  77. package/src/history-tree/ports.ts +12 -5
  78. package/src/history-tree/serialized.ts +39 -11
  79. package/src/history-tree/validation.ts +286 -78
  80. package/src/layout/generated/protocol.ts +4 -3
  81. package/src/layout/generated/variant-fields.ts +47 -0
  82. package/src/layout/index.ts +1 -1
  83. package/src/layout/ratio.ts +10 -9
  84. package/src/layout/validation.ts +98 -32
  85. package/src/layout/visibility.ts +1 -2
  86. package/src/licence/client.ts +48 -7
  87. package/src/licence/controller.ts +127 -70
  88. package/src/licence/direct.ts +3 -1
  89. package/src/licence/generated/fields.ts +4 -3
  90. package/src/licence/generated/key-conformance.json +28 -0
  91. package/src/licence/generated/protocol.ts +9 -5
  92. package/src/licence/generated/variant-fields.ts +1 -1
  93. package/src/licence/key.ts +42 -18
  94. package/src/licence/ports.ts +10 -3
  95. package/src/licence/serialized.ts +32 -9
  96. package/src/licence/validation.ts +134 -43
  97. package/src/native-content/client.ts +207 -234
  98. package/src/native-content/generated/fields.ts +6 -6
  99. package/src/native-content/generated/protocol.ts +16 -5
  100. package/src/native-content/generated/variant-fields.ts +1 -1
  101. package/src/native-content/ports.ts +11 -16
  102. package/src/native-content/serialized.ts +12 -19
  103. package/src/native-content/validation/base.ts +57 -80
  104. package/src/native-content/validation/messages.ts +24 -35
  105. package/src/native-content/validation/state.ts +31 -33
  106. package/src/notifications/client.ts +85 -51
  107. package/src/notifications/controller.ts +400 -145
  108. package/src/notifications/direct.ts +3 -1
  109. package/src/notifications/generated/fields.ts +4 -3
  110. package/src/notifications/generated/labels.ts +1 -1
  111. package/src/notifications/generated/protocol.ts +5 -1
  112. package/src/notifications/generated/variant-fields.ts +1 -1
  113. package/src/notifications/ports.ts +9 -4
  114. package/src/notifications/serialized.ts +11 -9
  115. package/src/notifications/tone.ts +1 -1
  116. package/src/notifications/validation.ts +165 -71
  117. package/src/operation/bridge.ts +187 -60
  118. package/src/operation/client.ts +48 -62
  119. package/src/operation/controller.ts +67 -97
  120. package/src/operation/direct.ts +1 -3
  121. package/src/operation/generated/fields.ts +4 -3
  122. package/src/operation/generated/labels.ts +1 -1
  123. package/src/operation/generated/protocol.ts +5 -1
  124. package/src/operation/generated/variant-fields.ts +1 -1
  125. package/src/operation/ports.ts +12 -10
  126. package/src/operation/serialized.ts +8 -9
  127. package/src/operation/validation.ts +189 -104
  128. package/src/settings/client.ts +11 -44
  129. package/src/settings/connection.ts +35 -53
  130. package/src/settings/generated/fields.ts +5 -4
  131. package/src/settings/generated/protocol.ts +14 -5
  132. package/src/settings/generated/variant-fields.ts +1 -1
  133. package/src/settings/names.ts +2 -4
  134. package/src/settings/registry.ts +23 -25
  135. package/src/settings/validation/authority.ts +21 -19
  136. package/src/settings/validation/command.ts +22 -41
  137. package/src/settings/validation/error.ts +2 -8
  138. package/src/settings/validation/primitives.ts +46 -97
  139. package/src/settings/validation/projection.ts +8 -24
  140. package/src/settings/validation/registry.ts +33 -79
  141. package/src/surface-transfer/client.ts +6 -18
  142. package/src/surface-transfer/generated/fields.ts +3 -3
  143. package/src/surface-transfer/generated/protocol.ts +3 -3
  144. package/src/surface-transfer/generated/variant-fields.ts +1 -1
  145. package/src/surface-transfer/validation.ts +91 -86
  146. package/src/surfaces/client.ts +33 -55
  147. package/src/surfaces/generated/fields.ts +3 -3
  148. package/src/surfaces/generated/protocol.ts +1 -1
  149. package/src/surfaces/generated/variant-fields.ts +1 -1
  150. package/src/surfaces/validation.ts +227 -99
  151. package/src/transfer/client.ts +26 -60
  152. package/src/transfer/connection.ts +29 -51
  153. package/src/transfer/generated/fields.ts +7 -6
  154. package/src/transfer/generated/protocol.ts +13 -5
  155. package/src/transfer/generated/variant-fields.ts +1 -1
  156. package/src/transfer/index.ts +8 -1
  157. package/src/transfer/payload.ts +20 -35
  158. package/src/transfer/validation.ts +169 -119
  159. package/src/update/client.ts +86 -9
  160. package/src/update/controller.ts +264 -116
  161. package/src/update/direct.ts +3 -1
  162. package/src/update/generated/fields.ts +5 -4
  163. package/src/update/generated/protocol.ts +25 -9
  164. package/src/update/generated/variant-fields.ts +4 -2
  165. package/src/update/ports.ts +14 -6
  166. package/src/update/serialized.ts +36 -10
  167. package/src/update/validation.ts +118 -48
@@ -1,4 +1,3 @@
1
- import { COMMAND_MAXIMUM_PHYSICAL_CODE_BYTES } from "./generated/protocol.ts";
2
1
  import type {
3
2
  CommandBindingCandidate,
4
3
  CommandDiscoveryRecord,
@@ -11,11 +10,9 @@ import type {
11
10
  CommandKeymapConflict,
12
11
  CommandPlatform,
13
12
  } from "./generated/protocol.ts";
13
+ import { isPhysicalCode } from "./physical-code.ts";
14
14
  import { shortcutLabel } from "./projectors.ts";
15
- import type {
16
- CommandExecutionOutcome,
17
- CommandExecutionSource,
18
- } from "./ports.ts";
15
+ import type { CommandExecutionOutcome, CommandExecutionSource } from "./ports.ts";
19
16
 
20
17
  export interface KeyboardEventLike {
21
18
  readonly code: string;
@@ -33,11 +30,11 @@ export interface KeyboardEventLike {
33
30
  export interface CommandKeyboardResolutionOptions {
34
31
  readonly platform: CommandPlatform;
35
32
  readonly input: CommandKeyboardInput;
36
- readonly contextPath: readonly string[];
33
+ readonly contextPath: ReadonlyArray<string>;
37
34
  readonly mode: CommandKeyboardMode;
38
- readonly bindings: readonly CommandEffectiveBinding[];
39
- readonly commands: readonly CommandDiscoveryRecord[];
40
- readonly reserved?: (platform: CommandPlatform, chord: CommandKeyChord) => boolean;
35
+ readonly bindings: ReadonlyArray<CommandEffectiveBinding>;
36
+ readonly commands: ReadonlyArray<CommandDiscoveryRecord>;
37
+ readonly reserved?: ((platform: CommandPlatform, chord: CommandKeyChord) => boolean) | undefined;
41
38
  }
42
39
 
43
40
  export interface CommandKeyboardDispatch {
@@ -47,8 +44,7 @@ export interface CommandKeyboardDispatch {
47
44
  ): Promise<CommandExecutionOutcome>;
48
45
  }
49
46
 
50
- export interface RouteKeyboardEventOptions
51
- extends Omit<CommandKeyboardResolutionOptions, "input"> {
47
+ export interface RouteKeyboardEventOptions extends Omit<CommandKeyboardResolutionOptions, "input"> {
52
48
  readonly dispatcher: CommandKeyboardDispatch;
53
49
  readonly editableTarget?: (target: unknown) => boolean;
54
50
  readonly onCapture?: (chord: CommandKeyChord, label: string) => void;
@@ -84,9 +80,15 @@ export function resolveCommandKeyboard(
84
80
  options: CommandKeyboardResolutionOptions,
85
81
  ): CommandKeyResolution {
86
82
  const { input } = options;
87
- if (!validPhysicalCode(input.chord.code)) return { kind: "unbound" };
88
- if (input.repeat) return gated("repeat");
89
- if (input.composing) return gated("composition");
83
+ if (!isPhysicalCode(input.chord.code)) {
84
+ return { kind: "unbound" };
85
+ }
86
+ if (input.repeat) {
87
+ return gated("repeat");
88
+ }
89
+ if (input.composing) {
90
+ return gated("composition");
91
+ }
90
92
  if (options.reserved?.(options.platform, input.chord) === true) {
91
93
  return gated("reserved");
92
94
  }
@@ -98,9 +100,7 @@ export function resolveCommandKeyboard(
98
100
  };
99
101
  }
100
102
 
101
- const positions = new Map(
102
- options.contextPath.map((contextId, index) => [contextId, index]),
103
- );
103
+ const positions = new Map(options.contextPath.map((contextId, index) => [contextId, index]));
104
104
  const matches = options.bindings
105
105
  .flatMap((binding) => {
106
106
  const specificity = positions.get(binding.contextId);
@@ -115,38 +115,34 @@ export function resolveCommandKeyboard(
115
115
  })
116
116
  .sort(
117
117
  (left, right) =>
118
- right.specificity - left.specificity ||
119
- left.binding.id.localeCompare(right.binding.id),
118
+ right.specificity - left.specificity || compareText(left.binding.id, right.binding.id),
120
119
  );
121
- if (matches.length === 0) return { kind: "unbound" };
120
+ const [firstMatch] = matches;
121
+ if (firstMatch === undefined) {
122
+ return { kind: "unbound" };
123
+ }
122
124
 
123
- const winningSpecificity = matches[0]!.specificity;
124
- const winningContext = matches[0]!.binding.contextId;
125
- const winning = matches.filter(
126
- ({ specificity }) => specificity === winningSpecificity,
127
- );
128
- const distinctInvocations = uniqueInvocations(
129
- winning.map(({ binding }) => binding.invocation),
130
- );
125
+ const winningSpecificity = firstMatch.specificity;
126
+ const winningContext = firstMatch.binding.contextId;
127
+ const winning = matches.filter(({ specificity }) => specificity === winningSpecificity);
128
+ const distinctInvocations = uniqueInvocations(winning.map(({ binding }) => binding.invocation));
131
129
  const conflict = distinctInvocations.length > 1;
132
- const representativeId = matches[0]!.binding.id;
133
- const candidates: CommandBindingCandidate[] = matches.map(
134
- ({ binding, specificity }) => ({
135
- bindingId: binding.id,
136
- source: binding.source,
137
- matchedContextId: binding.contextId,
138
- specificity,
139
- invocation: binding.invocation,
140
- disposition:
141
- specificity < winningSpecificity
142
- ? { kind: "shadowed", byContextId: winningContext }
143
- : conflict
144
- ? { kind: "conflict" }
145
- : binding.id === representativeId
146
- ? { kind: "winner" }
147
- : { kind: "equivalent" },
148
- }),
149
- );
130
+ const representativeId = firstMatch.binding.id;
131
+ const candidates: Array<CommandBindingCandidate> = matches.map(({ binding, specificity }) => ({
132
+ bindingId: binding.id,
133
+ source: binding.source,
134
+ matchedContextId: binding.contextId,
135
+ specificity,
136
+ invocation: binding.invocation,
137
+ disposition:
138
+ specificity < winningSpecificity
139
+ ? { kind: "shadowed", byContextId: winningContext }
140
+ : conflict
141
+ ? { kind: "conflict" }
142
+ : binding.id === representativeId
143
+ ? { kind: "winner" }
144
+ : { kind: "equivalent" },
145
+ }));
150
146
 
151
147
  if (conflict) {
152
148
  const record: CommandKeymapConflict = {
@@ -159,10 +155,8 @@ export function resolveCommandKeyboard(
159
155
  return { kind: "conflict", conflict: record, candidates };
160
156
  }
161
157
 
162
- const binding = matches[0]!.binding;
163
- const declaration = options.commands.find(
164
- ({ id }) => id === binding.invocation.commandId,
165
- );
158
+ const binding = firstMatch.binding;
159
+ const declaration = options.commands.find(({ id }) => id === binding.invocation.commandId);
166
160
  if (input.editableText && declaration?.textInputPolicy === "blocked") {
167
161
  return { kind: "gated", gate: "textInput", candidates };
168
162
  }
@@ -185,8 +179,7 @@ export function routeCommandKeyboardEvent(
185
179
  ...options,
186
180
  input: normalizeKeyboardEvent(event, options.editableTarget),
187
181
  });
188
- const consumed =
189
- resolution.kind === "captured" || resolution.kind === "resolved";
182
+ const consumed = resolution.kind === "captured" || resolution.kind === "resolved";
190
183
  if (consumed) {
191
184
  event.preventDefault();
192
185
  event.stopPropagation();
@@ -199,27 +192,29 @@ export function routeCommandKeyboardEvent(
199
192
  return {
200
193
  consumed,
201
194
  resolution,
202
- execution: options.dispatcher.dispatchInvocation(
203
- resolution.winner.invocation,
204
- "keyboard",
205
- ),
195
+ execution: options.dispatcher.dispatchInvocation(resolution.winner.invocation, "keyboard"),
206
196
  };
207
197
  }
208
198
  return { consumed, resolution };
209
199
  }
210
200
 
211
201
  export function isEditableCommandTarget(target: unknown): boolean {
212
- if (typeof target !== "object" || target === null) return false;
202
+ if (typeof target !== "object" || target === null) {
203
+ return false;
204
+ }
213
205
  const record = target as {
214
206
  readonly isContentEditable?: boolean;
215
207
  readonly tagName?: unknown;
216
208
  readonly role?: unknown;
217
209
  getAttribute?(name: string): string | null;
218
210
  };
219
- if (record.isContentEditable === true) return true;
220
- const tag =
221
- typeof record.tagName === "string" ? record.tagName.toLowerCase() : "";
222
- if (tag === "input" || tag === "textarea" || tag === "select") return true;
211
+ if (record.isContentEditable === true) {
212
+ return true;
213
+ }
214
+ const tag = typeof record.tagName === "string" ? record.tagName.toLowerCase() : "";
215
+ if (tag === "input" || tag === "textarea" || tag === "select") {
216
+ return true;
217
+ }
223
218
  const role =
224
219
  typeof record.getAttribute === "function"
225
220
  ? record.getAttribute("role")
@@ -229,9 +224,7 @@ export function isEditableCommandTarget(target: unknown): boolean {
229
224
  return role === "textbox" || role === "searchbox";
230
225
  }
231
226
 
232
- function gated(
233
- gate: "repeat" | "composition" | "reserved",
234
- ): CommandKeyResolution {
227
+ function gated(gate: "repeat" | "composition" | "reserved"): CommandKeyResolution {
235
228
  return { kind: "gated", gate, candidates: [] };
236
229
  }
237
230
 
@@ -251,8 +244,7 @@ function resolveBindingChord(
251
244
  modifiers: {
252
245
  control:
253
246
  binding.trigger.modifiers.control ||
254
- (binding.trigger.modifiers.primary &&
255
- (platform === "windows" || platform === "linux")),
247
+ (binding.trigger.modifiers.primary && (platform === "windows" || platform === "linux")),
256
248
  alt: binding.trigger.modifiers.alt,
257
249
  shift: binding.trigger.modifiers.shift,
258
250
  meta:
@@ -273,8 +265,8 @@ function sameChord(left: CommandKeyChord, right: CommandKeyChord): boolean {
273
265
  }
274
266
 
275
267
  function uniqueInvocations(
276
- invocations: readonly CommandInvocation[],
277
- ): CommandInvocation[] {
268
+ invocations: ReadonlyArray<CommandInvocation>,
269
+ ): Array<CommandInvocation> {
278
270
  const keyed = new Map<string, CommandInvocation>();
279
271
  for (const invocation of invocations) {
280
272
  keyed.set(invocationKey(invocation), invocation);
@@ -284,31 +276,35 @@ function uniqueInvocations(
284
276
 
285
277
  function invocationKey(invocation: CommandInvocation): string {
286
278
  const argumentsKey = Object.entries(invocation.arguments)
287
- .sort(([left], [right]) => left.localeCompare(right))
279
+ .sort(([left], [right]) => compareText(left, right))
288
280
  .map(([key, value]) => [key, value]);
289
281
  return JSON.stringify([invocation.commandId, argumentsKey]);
290
282
  }
291
283
 
292
- function compareInvocation(
293
- left: CommandInvocation,
294
- right: CommandInvocation,
295
- ): number {
284
+ function compareInvocation(left: CommandInvocation, right: CommandInvocation): number {
296
285
  const id = compareText(left.commandId, right.commandId);
297
- if (id !== 0) return id;
298
- const leftArguments = Object.entries(left.arguments).sort(([a], [b]) =>
299
- compareText(a, b),
300
- );
301
- const rightArguments = Object.entries(right.arguments).sort(([a], [b]) =>
302
- compareText(a, b),
303
- );
286
+ if (id !== 0) {
287
+ return id;
288
+ }
289
+ const leftArguments = Object.entries(left.arguments).sort(([a], [b]) => compareText(a, b));
290
+ const rightArguments = Object.entries(right.arguments).sort(([a], [b]) => compareText(a, b));
304
291
  const length = Math.min(leftArguments.length, rightArguments.length);
305
292
  for (let index = 0; index < length; index += 1) {
306
- const [leftId, leftValue] = leftArguments[index]!;
307
- const [rightId, rightValue] = rightArguments[index]!;
293
+ const leftArgument = leftArguments[index];
294
+ const rightArgument = rightArguments[index];
295
+ if (leftArgument === undefined || rightArgument === undefined) {
296
+ break;
297
+ }
298
+ const [leftId, leftValue] = leftArgument;
299
+ const [rightId, rightValue] = rightArgument;
308
300
  const field = compareText(leftId, rightId);
309
- if (field !== 0) return field;
301
+ if (field !== 0) {
302
+ return field;
303
+ }
310
304
  const value = compareArgumentValue(leftValue, rightValue);
311
- if (value !== 0) return value;
305
+ if (value !== 0) {
306
+ return value;
307
+ }
312
308
  }
313
309
  return leftArguments.length - rightArguments.length;
314
310
  }
@@ -325,7 +321,9 @@ function compareArgumentValue(
325
321
  enum: 4,
326
322
  } as const;
327
323
  const rank = ranks[left.kind] - ranks[right.kind];
328
- if (rank !== 0) return rank;
324
+ if (rank !== 0) {
325
+ return rank;
326
+ }
329
327
  if (
330
328
  (left.kind === "number" || left.kind === "integer") &&
331
329
  (right.kind === "number" || right.kind === "integer")
@@ -339,23 +337,19 @@ function compareArgumentValue(
339
337
  }
340
338
 
341
339
  function compareText(left: string, right: string): number {
342
- const leftValues = [...left];
343
- const rightValues = [...right];
340
+ const leftValues = Array.from(left);
341
+ const rightValues = Array.from(right);
344
342
  const length = Math.min(leftValues.length, rightValues.length);
345
343
  for (let index = 0; index < length; index += 1) {
346
- const difference =
347
- leftValues[index]!.codePointAt(0)! -
348
- rightValues[index]!.codePointAt(0)!;
349
- if (difference !== 0) return difference;
344
+ const leftCode = leftValues[index]?.codePointAt(0);
345
+ const rightCode = rightValues[index]?.codePointAt(0);
346
+ if (leftCode === undefined || rightCode === undefined) {
347
+ break;
348
+ }
349
+ const difference = leftCode - rightCode;
350
+ if (difference !== 0) {
351
+ return difference;
352
+ }
350
353
  }
351
354
  return leftValues.length - rightValues.length;
352
355
  }
353
-
354
- function validPhysicalCode(code: string): boolean {
355
- return (
356
- code !== "Unidentified" &&
357
- code.length > 0 &&
358
- code.length <= COMMAND_MAXIMUM_PHYSICAL_CODE_BYTES &&
359
- /^[A-Z][A-Za-z0-9]*$/u.test(code)
360
- );
361
- }
@@ -0,0 +1,11 @@
1
+ import { COMMAND_MAXIMUM_PHYSICAL_CODE_BYTES } from "./generated/protocol.ts";
2
+
3
+ /** Whether Rust's `CommandPhysicalCode` would admit `value`. */
4
+ export function isPhysicalCode(value: unknown): value is string {
5
+ return (
6
+ typeof value === "string" &&
7
+ value !== "Unidentified" &&
8
+ value.length <= COMMAND_MAXIMUM_PHYSICAL_CODE_BYTES &&
9
+ /^[A-Z][A-Za-z0-9]*$/u.test(value)
10
+ );
11
+ }
@@ -1,3 +1,4 @@
1
+ import type { Unlisten } from "../core/transport.ts";
1
2
  import type {
2
3
  CommandArguments,
3
4
  CommandAvailabilitySnapshot,
@@ -12,32 +13,26 @@ import type {
12
13
  CommandRequestId,
13
14
  } from "./generated/protocol.ts";
14
15
 
15
- export type CommandUnlisten = () => void | Promise<void>;
16
+ export type CommandUnlisten = Unlisten;
16
17
 
17
18
  export interface CommandSnapshotPort<Snapshot> {
18
19
  load(): Promise<Snapshot>;
19
20
  listen?(invalidate: () => void): CommandUnlisten | Promise<CommandUnlisten>;
20
21
  }
21
22
 
22
- export interface CommandCataloguePort
23
- extends CommandSnapshotPort<CommandCatalogueSnapshot> {}
23
+ // oxlint-disable-next-line typescript/no-empty-object-type -- This public interface permits consumer declaration merging.
24
+ export interface CommandCataloguePort extends CommandSnapshotPort<CommandCatalogueSnapshot> {}
24
25
 
25
- export interface CommandKeymapPort
26
- extends CommandSnapshotPort<CommandKeymapLoadOutcome> {
26
+ export interface CommandKeymapPort extends CommandSnapshotPort<CommandKeymapLoadOutcome> {
27
27
  preview?(request: CommandKeymapPreview): Promise<CommandKeymapPreviewResult>;
28
28
  commit?(request: CommandKeymapCommit): Promise<CommandKeymapMutationResult>;
29
29
  reset?(request: CommandKeymapReset): Promise<CommandKeymapMutationResult>;
30
30
  }
31
31
 
32
- export interface CommandAvailabilityPort
33
- extends CommandSnapshotPort<CommandAvailabilitySnapshot> {}
32
+ // oxlint-disable-next-line typescript/no-empty-object-type -- This public interface permits consumer declaration merging.
33
+ export interface CommandAvailabilityPort extends CommandSnapshotPort<CommandAvailabilitySnapshot> {}
34
34
 
35
- export type CommandExecutionSource =
36
- | "palette"
37
- | "keyboard"
38
- | "menu"
39
- | "help"
40
- | "consumer";
35
+ export type CommandExecutionSource = "palette" | "keyboard" | "menu" | "help" | "consumer";
41
36
 
42
37
  export interface CommandExecutionIntent {
43
38
  readonly requestId: CommandRequestId;
@@ -23,32 +23,27 @@ export interface CommandProjectionRecord {
23
23
  readonly id: CommandId;
24
24
  readonly label: string;
25
25
  readonly description: string | null;
26
- readonly categoryPath: readonly string[];
27
- readonly keywords: readonly string[];
26
+ readonly categoryPath: ReadonlyArray<string>;
27
+ readonly keywords: ReadonlyArray<string>;
28
28
  readonly icon: string | null;
29
29
  readonly availability: CommandAvailability;
30
- readonly shortcuts: readonly CommandShortcutRecord[];
30
+ readonly shortcuts: ReadonlyArray<CommandShortcutRecord>;
31
31
  }
32
32
 
33
33
  export interface CommandSettingsRecord extends CommandProjectionRecord {
34
- readonly bindings: readonly CommandEffectiveBinding[];
34
+ readonly bindings: ReadonlyArray<CommandEffectiveBinding>;
35
35
  }
36
36
 
37
37
  export interface CommandSurfaceProjection {
38
- readonly palette: readonly CommandProjectionRecord[];
39
- readonly menu: readonly CommandProjectionRecord[];
40
- readonly help: readonly CommandProjectionRecord[];
41
- readonly settings: readonly CommandSettingsRecord[];
42
- readonly shortcuts: readonly CommandShortcutRecord[];
43
- readonly reverseLookup: ReadonlyMap<CommandId, readonly CommandShortcutRecord[]>;
44
- readonly conflicts: readonly CommandKeymapConflict[];
38
+ readonly palette: ReadonlyArray<CommandProjectionRecord>;
39
+ readonly menu: ReadonlyArray<CommandProjectionRecord>;
40
+ readonly help: ReadonlyArray<CommandProjectionRecord>;
41
+ readonly settings: ReadonlyArray<CommandSettingsRecord>;
42
+ readonly shortcuts: ReadonlyArray<CommandShortcutRecord>;
43
+ readonly reverseLookup: ReadonlyMap<CommandId, ReadonlyArray<CommandShortcutRecord>>;
44
+ readonly conflicts: ReadonlyArray<CommandKeymapConflict>;
45
45
  }
46
46
 
47
- const AVAILABLE: CommandAvailability = {
48
- state: "available",
49
- reason: null,
50
- };
51
-
52
47
  export function joinCommandState(
53
48
  catalogue: CommandCatalogueSnapshot,
54
49
  keymap: CommandKeymapSnapshot,
@@ -63,9 +58,7 @@ export function joinCommandState(
63
58
  );
64
59
  }
65
60
  if (keymap.registryDigest !== catalogue.registryDigest) {
66
- throw new CommandProjectionConsistencyError(
67
- "catalogue and keymap registry digests differ",
68
- );
61
+ throw new CommandProjectionConsistencyError("catalogue and keymap registry digests differ");
69
62
  }
70
63
  const known = new Set(catalogue.commands.map(({ id }) => id));
71
64
  const records = new Set<string>();
@@ -90,48 +83,51 @@ export function projectCommandSurfaces(
90
83
  platform: CommandPlatform,
91
84
  ): CommandSurfaceProjection {
92
85
  const availability = new Map(
93
- state.availability.records.map((record) => [
94
- record.commandId,
95
- record.availability,
96
- ]),
86
+ state.availability.records.map((record) => [record.commandId, record.availability]),
97
87
  );
98
- const reverseLookup = new Map<CommandId, readonly CommandShortcutRecord[]>();
99
- const shortcuts: CommandShortcutRecord[] = [];
100
- for (const command of state.catalogue.commands) {
101
- const records = shortcutsForCommand(state.keymap, command.id, platform);
102
- reverseLookup.set(command.id, records);
88
+ const reverseLookup = new Map<CommandId, ReadonlyArray<CommandShortcutRecord>>();
89
+ const shortcuts: Array<CommandShortcutRecord> = [];
90
+ const rows = state.catalogue.commands.map((command) => {
91
+ const { id, label, description, categoryPath, keywords, icon, visibility } = command;
92
+ const value = availability.get(id);
93
+ if (value === undefined) {
94
+ throw new CommandProjectionConsistencyError(`availability is missing command ${id}`);
95
+ }
96
+ const records = shortcutsForCommand(state.keymap, id, platform);
97
+ reverseLookup.set(id, records);
103
98
  shortcuts.push(...records);
104
- }
105
- const project = (record: CommandDiscoveryRecord): CommandProjectionRecord => ({
106
- id: record.id,
107
- label: record.label,
108
- description: record.description,
109
- categoryPath: record.categoryPath,
110
- keywords: record.keywords,
111
- icon: record.icon,
112
- availability: availability.get(record.id) ?? AVAILABLE,
113
- shortcuts: reverseLookup.get(record.id) ?? [],
99
+ return {
100
+ record: {
101
+ id,
102
+ label,
103
+ description,
104
+ categoryPath,
105
+ keywords,
106
+ icon,
107
+ availability: value,
108
+ shortcuts: records,
109
+ },
110
+ visibility,
111
+ };
114
112
  });
115
- const visible = state.catalogue.commands.filter(
116
- ({ visibility }) => !visibility.hidden,
117
- );
113
+ const visible = rows.filter(({ visibility }) => !visibility.hidden);
118
114
  return {
119
115
  palette: visible
120
116
  .filter(({ visibility }) => visibility.palette)
121
- .map(project)
117
+ .map(({ record }) => record)
122
118
  .filter(({ availability }) => availability.state !== "hidden"),
123
119
  menu: visible
124
120
  .filter(({ visibility }) => visibility.menu)
125
- .map(project)
121
+ .map(({ record }) => record)
126
122
  .filter(({ availability }) => availability.state !== "hidden"),
127
123
  help: visible
128
124
  .filter(({ visibility }) => visibility.help)
129
- .map(project)
125
+ .map(({ record }) => record)
130
126
  .filter(({ availability }) => availability.state !== "hidden"),
131
127
  settings: visible
132
128
  .filter(({ visibility }) => visibility.settings)
133
- .map((record) => ({
134
- ...project(record),
129
+ .map(({ record }) => ({
130
+ ...record,
135
131
  bindings: state.keymap.bindings.filter(
136
132
  ({ invocation }) => invocation.commandId === record.id,
137
133
  ),
@@ -143,10 +139,35 @@ export function projectCommandSurfaces(
143
139
  }
144
140
 
145
141
  export function searchCommands(
146
- records: readonly CommandDiscoveryRecord[],
142
+ records: ReadonlyArray<CommandDiscoveryRecord>,
147
143
  query: string,
148
- ): readonly CommandSearchHit[] {
149
- const terms = query.split(/\s+/u).filter(Boolean).map((term) => term.toLowerCase());
144
+ ): ReadonlyArray<CommandSearchHit> {
145
+ return searchCommandRecords(records, query);
146
+ }
147
+
148
+ export function searchCommandSettings(
149
+ records: ReadonlyArray<CommandSettingsRecord>,
150
+ query: string,
151
+ ): ReadonlyArray<CommandSettingsRecord> {
152
+ if (query.trim().length === 0) {
153
+ return records;
154
+ }
155
+ return searchCommandRecords(records, query).map(({ record }) => record);
156
+ }
157
+
158
+ type SearchableCommand = Pick<
159
+ CommandProjectionRecord,
160
+ "id" | "label" | "description" | "categoryPath" | "keywords"
161
+ >;
162
+
163
+ function searchCommandRecords<Record extends SearchableCommand>(
164
+ records: ReadonlyArray<Record>,
165
+ query: string,
166
+ ): ReadonlyArray<{ record: Record; score: number }> {
167
+ const terms = query
168
+ .split(/\s+/u)
169
+ .filter(Boolean)
170
+ .map((term) => term.toLowerCase());
150
171
  return records
151
172
  .flatMap((record) => {
152
173
  const score = scoreRecord(record, terms);
@@ -155,10 +176,7 @@ export function searchCommands(
155
176
  .sort(
156
177
  (left, right) =>
157
178
  left.score - right.score ||
158
- compareUnicodeScalars(
159
- left.record.label.toLowerCase(),
160
- right.record.label.toLowerCase(),
161
- ) ||
179
+ compareUnicodeScalars(left.record.label.toLowerCase(), right.record.label.toLowerCase()) ||
162
180
  compareUnicodeScalars(left.record.id, right.record.id),
163
181
  );
164
182
  }
@@ -167,12 +185,11 @@ export function shortcutsForCommand(
167
185
  keymap: CommandKeymapSnapshot,
168
186
  commandId: CommandId,
169
187
  platform: CommandPlatform,
170
- ): readonly CommandShortcutRecord[] {
188
+ ): ReadonlyArray<CommandShortcutRecord> {
171
189
  return keymap.bindings
172
190
  .filter(
173
191
  (binding) =>
174
- binding.invocation.commandId === commandId &&
175
- includesPlatform(binding.platform, platform),
192
+ binding.invocation.commandId === commandId && includesPlatform(binding.platform, platform),
176
193
  )
177
194
  .map((binding) => {
178
195
  const chord = resolveTrigger(binding, platform);
@@ -186,10 +203,7 @@ export function shortcutsForCommand(
186
203
  });
187
204
  }
188
205
 
189
- export function shortcutLabel(
190
- chord: CommandKeyChord,
191
- platform: CommandPlatform,
192
- ): string {
206
+ export function shortcutLabel(chord: CommandKeyChord, platform: CommandPlatform): string {
193
207
  const key = keyLabel(chord.code);
194
208
  if (platform === "macOs") {
195
209
  return [
@@ -218,11 +232,11 @@ export class CommandProjectionConsistencyError extends Error {
218
232
  }
219
233
  }
220
234
 
221
- function scoreRecord(
222
- record: CommandDiscoveryRecord,
223
- terms: readonly string[],
224
- ): number | undefined {
225
- if (terms.length === 0) return 0;
235
+ function scoreRecord(record: SearchableCommand, terms: ReadonlyArray<string>): number | undefined {
236
+ if (terms.length === 0) {
237
+ return 0;
238
+ }
239
+ const id = record.id.toLowerCase();
226
240
  const label = record.label.toLowerCase();
227
241
  const description = record.description?.toLowerCase() ?? "";
228
242
  const keywords = record.keywords.map((keyword) => keyword.toLowerCase());
@@ -243,13 +257,15 @@ function scoreRecord(
243
257
  ? 50
244
258
  : record.categoryPath.some((category) => category.includes(term))
245
259
  ? 60
246
- : record.id.includes(term)
260
+ : id.includes(term)
247
261
  ? 70
248
262
  : description.includes(term)
249
263
  ? 80
250
264
  : undefined;
251
- if (termScore === undefined) return undefined;
252
- score = Math.min(0xffff_ffff, score + termScore);
265
+ if (termScore === undefined) {
266
+ return undefined;
267
+ }
268
+ score = Math.min(0xff_ff_ff_ff, score + termScore);
253
269
  }
254
270
  return score;
255
271
  }
@@ -280,8 +296,12 @@ function resolveTrigger(
280
296
  }
281
297
 
282
298
  function keyLabel(code: string): string {
283
- if (code.length === 4 && code.startsWith("Key")) return code.slice(3);
284
- if (code.length === 6 && code.startsWith("Digit")) return code.slice(5);
299
+ if (code.length === 4 && code.startsWith("Key")) {
300
+ return code.slice(3);
301
+ }
302
+ if (code.length === 6 && code.startsWith("Digit")) {
303
+ return code.slice(5);
304
+ }
285
305
  return (
286
306
  {
287
307
  ArrowUp: "↑",
@@ -294,13 +314,18 @@ function keyLabel(code: string): string {
294
314
  }
295
315
 
296
316
  function compareUnicodeScalars(left: string, right: string): number {
297
- const leftValues = [...left];
298
- const rightValues = [...right];
317
+ const leftValues = Array.from(left);
318
+ const rightValues = Array.from(right);
299
319
  const length = Math.min(leftValues.length, rightValues.length);
300
320
  for (let index = 0; index < length; index += 1) {
301
- const leftCode = leftValues[index]!.codePointAt(0)!;
302
- const rightCode = rightValues[index]!.codePointAt(0)!;
303
- if (leftCode !== rightCode) return leftCode - rightCode;
321
+ const leftCode = leftValues[index]?.codePointAt(0);
322
+ const rightCode = rightValues[index]?.codePointAt(0);
323
+ if (leftCode === undefined || rightCode === undefined) {
324
+ break;
325
+ }
326
+ if (leftCode !== rightCode) {
327
+ return leftCode - rightCode;
328
+ }
304
329
  }
305
330
  return leftValues.length - rightValues.length;
306
331
  }