diff --git a/lua/codediff/keymap/slots.lua b/lua/codediff/keymap/slots.lua index 0565db79..33b74b18 100644 --- a/lua/codediff/keymap/slots.lua +++ b/lua/codediff/keymap/slots.lua @@ -64,6 +64,30 @@ local function read_current(bufnr, mode, lhs) return result end +-- which-key installs a buffer-local `nowait` trigger per prefix (e.g. ). +-- Neovim only honors `nowait` when that mapping is found before any longer one +-- sharing its prefix, and buffer-local mappings are searched newest first. So +-- mapping hs on a buffer which-key already attached to makes +-- wait the full 'timeoutlen' and the popup never appears in time. Asking +-- which-key to rebuild the buffer (what it does itself on LspAttach) puts its +-- trigger back in front and refreshes the listed mappings. +local which_key_pending = {} + +local function refresh_which_key(bufnr) + if not package.loaded["which-key.buf"] or which_key_pending[bufnr] then + return + end + which_key_pending[bufnr] = true + vim.schedule(function() + which_key_pending[bufnr] = nil + if vim.api.nvim_buf_is_valid(bufnr) then + pcall(function() + require("which-key.buf").clear({ buf = bufnr }) + end) + end + end) +end + --- True when `current` is a buffer-local mapping (not a global fallback). local function is_buffer_local(current) return current ~= nil and current.buffer == 1 @@ -135,6 +159,7 @@ local function install(slot, claim) -- against this snapshot: another plugin may reuse the same RHS, or even the -- same callback, while changing options, and that is still its mapping. slot.applied_map = read_current(slot.bufnr, slot.mode, slot.lhs) + refresh_which_key(slot.bufnr) else slot.applied = nil slot.applied_map = nil @@ -185,6 +210,7 @@ local function restore(slot) else pcall(vim.keymap.del, slot.mode, slot.lhs, { buffer = slot.bufnr }) end + refresh_which_key(slot.bufnr) slot.applied = nil slot.applied_map = nil end diff --git a/tests/integration/keymap/registry_spec.lua b/tests/integration/keymap/registry_spec.lua index a18f46a1..e66fe2c9 100644 --- a/tests/integration/keymap/registry_spec.lua +++ b/tests/integration/keymap/registry_spec.lua @@ -582,4 +582,43 @@ describe("keymap registry", function() assert.equals(0, slots.count()) end) end) + + describe("which-key", function() + local saved + + before_each(function() + saved = package.loaded["which-key.buf"] + end) + + after_each(function() + package.loaded["which-key.buf"] = saved + end) + + -- which-key's nowait trigger only fires immediately while it is + -- newer than every longer mapping on the buffer, so codediff must + -- ask which-key to re-attach after changing a buffer's mappings. + it("asks which-key to rebuild a buffer once per batch of claims and releases", function() + local cleared = {} + package.loaded["which-key.buf"] = { + clear = function(opts) + table.insert(cleared, opts.buf) + end, + } + + local bufnr = new_buf() + local r = keymap.new("test") + r:claim(bufnr, "n", "hs", noop, {}) + r:claim(bufnr, "n", "hu", noop, {}) + vim.wait(100, function() + return #cleared > 0 + end) + assert.same({ bufnr }, cleared) + + r:dispose() + vim.wait(100, function() + return #cleared > 1 + end) + assert.same({ bufnr, bufnr }, cleared) + end) + end) end)