Skip to content

refactor: centralize matches_key in keybindings and make char matching case-insensitive - #44

Merged
Blankeos merged 1 commit into
Blankeos:mainfrom
felipecm-br:refactor/keybindings-matches-key
Nov 5, 2026
Merged

Blankeos merged 1 commit into
Blankeos:mainfrom
felipecm-br:refactor/keybindings-matches-key

Conversation

@felipecm-br

Copy link
Copy Markdown
Contributor

Description

Centralizes the duplicated matches_key implementations across all controller modules into a single function in src/config/keybindings.rs, while adding case-insensitive matching for character keys and normalizing shifted punctuation.

Details

  • Deduplication: Removed 12 duplicate copies of fn matches_key across controllers (branches, commits, commit_files, diff_mode, files, reflog, remote_branches, remotes, stash, status, tree, and gui/mod.rs), reducing code by 59 lines net.
  • Case-Insensitive Character Matching: Handles terminal emulators that report uppercase character codes with modifiers (e.g. Ctrl+L vs Ctrl+l), preventing ignored shortcuts.
  • Shifted Punctuation Normalization: Handles terminals reporting shifted punctuation (such as < or >) with KeyModifiers::SHIFT.
  • Preserved Semantics: Bare single characters without modifiers distinguish uppercase vs lowercase (e.g. D requires Shift whereas d does not).
  • Unit tests verifying case-insensitivity, modifier enforcement, and shift distinctions.

Validation

  • cargo test --bin lazygitrs (all 372 tests passed)
  • cargo fmt --check

@Blankeos

Blankeos commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

refactor/keybindings-matches-key → main
Model: claude-sonnet-5-5, effort unknown

Confidence: 4.5/5

Mechanical dedupe of 12 identical helpers into one. The only behavior change is the global helper gaining tree.rs's SHIFT-strip for punctuation plus ASCII case-folding. I merged it with current main, built it and ran it.
Checks run: merges cleanly with current main; cargo fmt --check clean; cargo test on the merged result: 380 passed, 0 failed; cargo clippy shows no new warnings except the dead wrapper below. Checked the default keybindings for d/D-style pairs: none collide, because parse_key gives uppercase bindings SHIFT and lowercase ones NONE. Ran the release build in Ghostty (kitty keyboard protocol) next to the build from main: ? opens the command palette identically on both, and uppercase W opens the diff-mode picker, so uppercase and shifted-punctuation bindings still fire.
Confidence bumps: drop the unused wrapper and add the two tests below → 5/5. Not covered: a terminal that does not report SHIFT for capitals (non-kitty), and </> (they are tree sibling moves, a no-op in my flat test list).

Summary

Thanks Felipe, nice cleanup. Removing the 12 copy-pasted matches_key functions in favour of one in config/keybindings.rs is a clear win, and the diff is easy to follow. Before, gui/mod.rs and the controllers compared code and modifiers exactly; now they use the old tree.rs rule (strip SHIFT from the event when the binding is a bare ASCII punctuation char) and compare chars case-insensitively. Because parse_key("D") yields Char('D')+SHIFT, uppercase bindings still require Shift and d still does not match D. Two small nits:

  • impl KeybindingConfig { fn matches_key } is unused (clippy: "method matches_key is never used"), so I'd drop it.
  • Case-folding means a bare d binding now also matches an event Char('D') with no modifiers, which some terminals send for Shift+d without reporting SHIFT. That is a real behavior change worth a line in the description. A test for shifted punctuation (< with SHIFT) and one for that case would round it out.

Map

flowchart LR
  KB[config/keybindings.rs matches_key + parse_key]
  CM[config/mod.rs re-export]
  GM[gui/mod.rs global keys]
  TR[controller/tree.rs pub use]
  C[controller/*.rs 11 files]
  KB --> CM
  CM --> GM
  CM --> C
  CM --> TR
  TR --> C
Loading

Start at src/config/keybindings.rs; everything else is deletions and import swaps.

Regressions

  • Global handler (gui/mod.rs) now strips SHIFT for bare punctuation bindings and folds case; previously exact match. Low risk, mostly a fix; confirmed ? and W still work live.
  • Bare lowercase binding now matches an uppercase char event with no modifiers. No collision in the defaults; custom configs unverified.
  • Dead KeybindingConfig::matches_key wrapper (clippy warning).

Migrations

None. No config or keybinding format changes; same binding strings.

Before merging

  • Remove the unused impl KeybindingConfig::matches_key
  • Add tests for shifted punctuation (<, > with SHIFT) and bare d vs Char('D') NONE, documenting the intended result

@Blankeos
Blankeos merged commit f892677 into Blankeos:main Nov 5, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants