Harden keybinding parsing and config resolution

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-02-15 13:20:36 -08:00
1 parent c70e2d5adc
commit be32ce9792
4 files changed
+266 -6

No files matched your search

+39
View File
@@ -110,6 +110,24 @@ describe("split/new terminal shortcuts", () => {
}),
);
});
it("supports when boolean literals", () => {
const keybindings: KeybindingsConfig = [
{ key: "mod+n", command: "terminal.new", when: "true" },
{ key: "mod+m", command: "terminal.new", when: "false" },
];
assert.isTrue(
isTerminalNewShortcut(event({ key: "n", ctrlKey: true }), keybindings, {
platform: "Linux",
}),
);
assert.isFalse(
isTerminalNewShortcut(event({ key: "m", ctrlKey: true }), keybindings, {
platform: "Linux",
}),
);
});
});
describe("shortcutLabelForCommand", () => {
@@ -220,6 +238,11 @@ describe("formatShortcutLabel", () => {
it("formats labels for non-macOS", () => {
assert.strictEqual(formatShortcutLabel("mod+shift+d", "Linux"), "Ctrl+Shift+D");
});
it("formats labels for plus key", () => {
assert.strictEqual(formatShortcutLabel("mod++", "MacIntel"), "⌘+");
assert.strictEqual(formatShortcutLabel("mod++", "Linux"), "Ctrl++");
});
});
describe("isTerminalClearShortcut", () => {
@@ -232,3 +255,19 @@ describe("isTerminalClearShortcut", () => {
assert.isTrue(isTerminalClearShortcut(event({ key: "k", metaKey: true }), "MacIntel"));
});
});
describe("plus key parsing", () => {
it("matches the plus key shortcut", () => {
const plusBindings: KeybindingsConfig = [{ key: "mod++", command: "terminal.toggle" }];
assert.isTrue(
isTerminalToggleShortcut(event({ key: "+", metaKey: true }), plusBindings, {
platform: "MacIntel",
}),
);
assert.isTrue(
isTerminalToggleShortcut(event({ key: "+", ctrlKey: true }), plusBindings, {
platform: "Linux",
}),
);
});
});
+19 -5
View File
@@ -45,6 +45,7 @@ type WhenToken =
| { type: "rparen" };
const WHEN_AST_CACHE = new Map<string, WhenNode | null>();
const MAX_WHEN_AST_CACHE_ENTRIES = 512;
function isMacPlatform(platform: string): boolean {
return /mac|iphone|ipad|ipod/i.test(platform);
@@ -57,11 +58,19 @@ function normalizeKeyToken(token: string): string {
}
function parseShortcutValue(value: string): ParsedShortcut | null {
const tokens = value
.toLowerCase()
.split("+")
.map((token) => token.trim())
.filter((token) => token.length > 0);
const rawTokens = value.toLowerCase().split("+").map((token) => token.trim());
const tokens = [...rawTokens];
let trailingEmptyCount = 0;
while (tokens[tokens.length - 1] === "") {
trailingEmptyCount += 1;
tokens.pop();
}
if (trailingEmptyCount > 0) {
tokens.push("+");
}
if (tokens.some((token) => token.length === 0)) {
return null;
}
if (tokens.length === 0) return null;
let key: string | null = null;
@@ -264,6 +273,8 @@ function parseWhenExpression(expression: string): WhenNode | null {
function evaluateWhenNode(node: WhenNode, context: ShortcutMatchContext): boolean {
switch (node.type) {
case "identifier":
if (node.name === "true") return true;
if (node.name === "false") return false;
return Boolean(context[node.name]);
case "not":
return !evaluateWhenNode(node.node, context);
@@ -280,6 +291,9 @@ function matchesWhenExpression(when: string | undefined, context: ShortcutMatchC
if (normalized.length === 0) return true;
if (!WHEN_AST_CACHE.has(normalized)) {
if (WHEN_AST_CACHE.size >= MAX_WHEN_AST_CACHE_ENTRIES) {
WHEN_AST_CACHE.clear();
}
WHEN_AST_CACHE.set(normalized, parseWhenExpression(normalized));
}
const ast = WHEN_AST_CACHE.get(normalized);