Skip to content

Conversation

@esmuellert
Copy link
Owner

Summary

Fixes #211 - Keymaps not restored properly after quitting :CodeDiff with q

Problem

When users had buffer-local keymaps (e.g., J, K, ]h, [h) and opened CodeDiff, the plugin would overwrite these keymaps. On close, it only deleted the CodeDiff keymaps without restoring the user's originals.

Solution

  • Add save_existing_keymap() to capture keymap metadata (callback, rhs, options) before CodeDiff sets its own
  • Store saved keymaps in session keyed by bufnr:mode:lhs
  • On cleanup, restore original keymaps with their callbacks and all options (expr, silent, noremap, nowait, desc)
  • Add guard to prevent double-cleanup from deleting restored keymaps

Changes

  • lua/codediff/ui/lifecycle/accessors.lua: Save and restore keymaps logic
  • tests/keymap_restore_spec.lua: Comprehensive tests for the fix

Testing

All tests pass including 3 new keymap restoration tests covering:

  • Exact issue scenario (buffer-local keymaps restored after file navigation)
  • Buffers without pre-existing keymaps (no errors)
  • Keymaps with various options (expr, silent, etc.)

Fixes #211 - Keymaps not restored properly after quitting :CodeDiff with q

The issue was that CodeDiff overwrote user's buffer-local keymaps (J, K, ]h, [h, etc.)
but only deleted them on close, never restoring the originals.

Changes:
- Add save_existing_keymap() to capture keymap metadata before overwriting
- Store saved keymaps in session (sess.saved_keymaps) keyed by bufnr:mode:lhs
- On cleanup, restore original keymaps with their callbacks and options
- Add guard to prevent double-cleanup from deleting restored keymaps
- Add comprehensive tests for keymap restoration scenarios
@esmuellert esmuellert enabled auto-merge January 30, 2026 01:49
@esmuellert esmuellert merged commit 9a18715 into main Jan 30, 2026
13 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.

Bug: Keymaps not restored properly after quitting :CodeDiff with q

2 participants