TypeScript
fix(server): editing or removing a hand-written shortcut in Settings works again (#25556)
MarcoDotIO286bc5eb1e2e
diff --git a/apps/server/src/keybindings.test.ts b/apps/server/src/keybindings.test.tsindex 97d7e375f80..8b513c4ca30 100644--- a/apps/server/src/keybindings.test.ts+++ b/apps/server/src/keybindings.test.ts@@ -444,6 +444,61 @@ it.layer(NodeServices.layer)("keybindings", (it) => {}).pipe(Effect.provide(layerKeybindings())),);+ it.effect("removes a hand-written keybinding targeted by its settings spelling", () =>+ Effect.gen(function* () {+ const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;+ yield* writeKeybindingsConfig(keybindingsConfigPath, [+ {+ key: "Shift+Cmd+R",+ command: "script.run-tests.run",+ when: "terminalFocus&&!previewFocus",+ },+ { key: "mod+shift+r", command: "script.run-tests.run" },+ { key: "x\u0000true", command: "script.run-tests.run" },+ { key: "x", command: "script.run-tests.run", when: "true" },+ ]);+ yield* Effect.gen(function* () {+ const keybindings = yield* Keybindings.Keybindings;+ yield* keybindings.removeKeybindingRule({+ key: "meta+shift+r",+ command: "script.run-tests.run",+ when: "terminalFocus && !previewFocus",+ });+ yield* keybindings.removeKeybindingRule({+ key: "x",+ command: "script.run-tests.run",+ when: "true",+ });+ });++ const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);+ assert.deepEqual(persisted, [+ { key: "mod+shift+r", command: "script.run-tests.run" },+ { key: "x\u0000true", command: "script.run-tests.run" },+ ]);+ }).pipe(Effect.provide(layerKeybindings())),+ );++ it.effect("replaces a hand-written keybinding targeted by its settings spelling", () =>+ Effect.gen(function* () {+ const { keybindingsConfigPath } = yield* ServerConfig.ServerConfig;+ yield* writeKeybindingsConfig(keybindingsConfigPath, [+ { key: "escape", command: "script.run-tests.run", when: "a && b && c" },+ ]);+ yield* Effect.gen(function* () {+ const keybindings = yield* Keybindings.Keybindings;+ return yield* keybindings.upsertKeybindingRule({+ key: "mod+alt+r",+ command: "script.run-tests.run",+ replace: { key: "esc", command: "script.run-tests.run", when: "(a && b) && c" },+ });+ });++ const persisted = yield* readKeybindingsConfig(keybindingsConfigPath);+ assert.deepEqual(persisted, [{ key: "mod+alt+r", command: "script.run-tests.run" }]);+ }).pipe(Effect.provide(layerKeybindings())),+ );+it.effect("refuses to overwrite malformed keybindings config", () =>Effect.gen(function* () {const fs = yield* FileSystem.FileSystem;diff --git a/apps/server/src/keybindings.ts b/apps/server/src/keybindings.tsindex 44dae514dd0..1247428a56d 100644--- a/apps/server/src/keybindings.ts+++ b/apps/server/src/keybindings.ts@@ -41,6 +41,7 @@ import {compileResolvedKeybindingRule,compileResolvedKeybindingsConfig,parseKeybindingShortcut,+ parseKeybindingWhenExpression,} from "@t3tools/shared/keybindings";export const ResolvedKeybindingFromConfig = KeybindingRule.pipe(@@ -80,12 +81,18 @@ export const ResolvedKeybindingFromConfig = KeybindingRule.pipe(),);+// Settings sends replace/remove targets in its own normalized spelling, which can differ from a+// hand-edited file ("cmd+k" vs "meta+k", "a&&b" vs "a && b"), so compare parsed forms.+function keybindingRuleIdentity(rule: KeybindingRule): string {+ const shortcut = parseKeybindingShortcut(rule.key);+ const key = (shortcut && encodeShortcut(shortcut)) ?? rule.key;+ if (rule.when === undefined) return JSON.stringify([rule.command, key, null]);+ const whenAst = parseKeybindingWhenExpression(rule.when);+ return JSON.stringify([rule.command, key, whenAst ? encodeWhenAst(whenAst) : rule.when]);+}+function isSameKeybindingRule(left: KeybindingRule, right: KeybindingRule): boolean {- return (- left.command === right.command &&- left.key === right.key &&- (left.when ?? undefined) === (right.when ?? undefined)- );+ return keybindingRuleIdentity(left) === keybindingRuleIdentity(right);}const LATE_DEFAULT_KEYBINDINGS: ReadonlyArray<{