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.
22 changes: 22 additions & 0 deletions src/extension-api/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ describe("parseKeyChord", () => {
expect(parseKeyChord("")).toHaveProperty("error");
});

test("refuses chords with an empty segment around +", () => {
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 valid, with or without surrounding whitespace", () => {
expect(parsed("+")).toEqual({
base: "+",
ctrl: false,
meta: false,
option: false,
shift: false,
});
expect(parsed(" + ")).toEqual(parsed("+"));
});

test("tolerates whitespace around otherwise valid chord 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
22 changes: 13 additions & 9 deletions src/extension-api/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,19 @@ 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}"` };
// The literal "+" binding (optionally whitespace-padded) splits into two
// empty segments; recognize it before empty segments are rejected below.
if (chord.trim() === "+") {
return { base: "+", ctrl: false, meta: false, option: false, shift: false };
}

if (chord.trim().length === 0) {
return { error: `Empty key chord "${chord}"` };
}

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

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

test("a malformed chord with an empty + segment does not claim another command's shortcut", () => {
const { keys, issues } = resolve({ "hunk.app.quit": ["s+", "ctrl+q"] });

expect(keys.get("hunk.app.quit")).toEqual(["ctrl+q"]);
// "s+" must not be normalized into "s" and steal it from its default owner.
expect(keys.get("hunk.view.toggleFilesPane")).toEqual(["s"]);
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