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
26 changes: 26 additions & 0 deletions lua/codediff/keymap/slots.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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. <leader>).
-- 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 <leader>hs on a buffer which-key already attached to makes <leader>
-- 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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions tests/integration/keymap/registry_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 <leader> trigger only fires immediately while it is
-- newer than every longer <leader> 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", "<leader>hs", noop, {})
r:claim(bufnr, "n", "<leader>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)