Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/reject-empty-keychord-segments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hunkdiff": patch
---

Reject malformed configured and extension key chords instead of silently rebinding them after empty `+` segments.
25 changes: 25 additions & 0 deletions src/extension-api/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,31 @@ describe("parseKeyChord", () => {
expect(parseKeyChord("")).toHaveProperty("error");
});

test("rejects chords with an empty segment around a plus", () => {
// A missing component around "+" must be refused, not silently
// normalized into a different, real shortcut.
expect(parseKeyChord("s+")).toHaveProperty("error");
expect(parseKeyChord("+s")).toHaveProperty("error");
expect(parseKeyChord("ctrl++s")).toHaveProperty("error");
expect(parseKeyChord("ctrl+s+")).toHaveProperty("error");
});

test("keeps the literal plus-key chord valid", () => {
const literalPlus = {
base: "+",
ctrl: false,
meta: false,
option: false,
shift: false,
};
expect(parsed("+")).toEqual(literalPlus);
expect(parsed(" + ")).toEqual(literalPlus);
});

test("tolerates whitespace around valid components", () => {
expect(parsed("ctrl + s")).toEqual(parsed("ctrl+s"));
});

test("refuses shift on symbols and digits, keeps it for letters and named keys", () => {
// Shifted symbols have no layout-independent identity; the binding must
// name the character shift produces instead.
Expand Down
18 changes: 9 additions & 9 deletions src/extension-api/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ const MODIFIER_TOKENS: Record<string, keyof Omit<ParsedKeyChord, "base">> = {
* registration instead of silently never firing.
*/
export function parseKeyChord(chord: string): ParsedKeyChord | { error: string } {
const tokens = chord
.split("+")
.map((token) => token.trim())
.filter((token) => token.length > 0);
// A literal "+" binding arrives as empty tokens; treat the lone "+" specially.
if (tokens.length === 0) {
return chord.trim() === "+"
? { base: "+", ctrl: false, meta: false, option: false, shift: false }
: { error: `Empty key chord "${chord}"` };
// A literal "+" binding splits into empty segments below; recognize it before
// segments are validated, so the intentional plus-key chord stays valid.
if (chord.trim() === "+") {
return { base: "+", ctrl: false, meta: false, option: false, shift: false };
}

const tokens = chord.split("+").map((token) => token.trim());
if (tokens.some((token) => token.length === 0)) {
return { error: `Key chord "${chord}" has an empty component around "+"` };
}

const parsed: ParsedKeyChord = {
Expand Down
8 changes: 8 additions & 0 deletions src/ui/lib/keymap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ describe("resolveCommandKeys", () => {
expect(issues[0]?.message).toContain("not a usable key chord");
});

test("a chord with an empty segment is rejected without claiming the real shortcut", () => {
const { keys, issues } = resolve({ "hunk.app.quit": ["s+", "ctrl+q"] });

expect(keys.get("hunk.app.quit")).toEqual(["ctrl+q"]);
expect(issues).toHaveLength(1);
expect(issues[0]?.message).toContain("not a usable key chord");
});

test("unknown ids are reported, softly when they look like extension commands", () => {
const { keys, issues } = resolve({ "hunk.app.quti": "x", "ghost.command": "z" });

Expand Down
Loading