Skip to content

Commit

Permalink
fix(state): deal with the fact that ModeChanged doesn't always seems …
Browse files Browse the repository at this point in the history
…to trigger. Fixes #787
  • Loading branch information
folke committed Jul 24, 2024
1 parent c5e7993 commit 388bd3f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
7 changes: 0 additions & 7 deletions lua/which-key/buf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,6 @@ function M.clear(opts)
M.bufs[b]:clear(opts)
end
end
if opts.check ~= false then
M.check()
end
end

M.check = Util.debounce(50, function()
M.get()
end)

return M
2 changes: 1 addition & 1 deletion lua/which-key/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ function M.add(mapping, ret, opts)
local modes = mapping.mode or { "n" } --[[@as string|string[] ]]
modes = type(modes) == "string" and vim.split(modes, "") or modes --[[@as string[] ]]
for _, mode in ipairs(modes) do
if mode ~= "v" and mode ~= Util.mapmode({ mode = mode }) then
if mode ~= "v" and mode ~= Util.mapmode(mode) then
M.warn("Invalid mode `" .. mode .. "`", mapping)
end
local m = vim.deepcopy(mapping)
Expand Down
29 changes: 19 additions & 10 deletions lua/which-key/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ function M.setup()
callback = function(ev)
Util.debug(ev.event)
if ev.event == "RecordingEnter" then
Buf.clear({ buf = ev.buf, check = false })
Buf.clear({ buf = ev.buf })
M.stop()
else
Buf.check()
end
end,
})
Expand Down Expand Up @@ -89,16 +87,11 @@ function M.setup()
return Config.defer(ctx)
end

-- this prevents restarting which-key in the same tick
local cooldown = Util.cooldown()

-- cache the mode, since it can change outside of ModeChanged events
Util.mode = vim.api.nvim_get_mode().mode

-- this prevents restarting which-key in the same tick
vim.api.nvim_create_autocmd("ModeChanged", {
group = group,
callback = function(ev)
Util.mode = vim.api.nvim_get_mode().mode
Util.trace("ModeChanged(" .. ev.match .. ")")
local mode = Buf.get()

Expand Down Expand Up @@ -147,16 +140,32 @@ function M.setup()
end,
})

local current_buf = vim.api.nvim_get_current_buf()
vim.api.nvim_create_autocmd({ "BufEnter" }, {
group = group,
callback = function(ev)
current_buf = ev.buf ---@type number
Util.trace(ev.event .. "(" .. ev.buf .. ")")
Buf.get()
Util.trace()
end,
})

Buf.check()
-- HACK: ModeChanged does not always trigger, so we need to manually
-- check for mode changes. This seems to be due to the usage of `:norm` in autocmds.
-- See https://github.com/folke/which-key.nvim/issues/787
local last_mode = nil ---@type string?
local last_buf = nil ---@type number?
local timer = uv.new_timer()
timer:start(0, 50, function()
local mode = vim.api.nvim_get_mode().mode
if mode == last_mode and last_buf == current_buf then
return
end
last_mode = mode
last_buf = current_buf
vim.schedule(Buf.get)
end)
end

function M.stop()
Expand Down
8 changes: 3 additions & 5 deletions lua/which-key/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ M.BS = M.t("<bs>")
M.EXIT = M.t("<C-\\><C-n>")
M.LUA_CALLBACK = "\x80\253g"
M.CMD = "\x80\253h"
M.mode = "n"

function M.exit()
vim.api.nvim_feedkeys(M.EXIT, "n", false)
Expand Down Expand Up @@ -77,10 +76,9 @@ function M.keys(lhs, opts)
return ret
end

---@param opts? {mode?: string, cached?: boolean}
function M.mapmode(opts)
opts = opts or {}
local mode = opts.mode or (opts.cached ~= false and M.mode) or vim.api.nvim_get_mode().mode
---@param mode? string
function M.mapmode(mode)
mode = mode or vim.api.nvim_get_mode().mode
mode = mode:gsub(M.t("<C-V>"), "v"):gsub(M.t("<C-S>"), "s"):lower()
if mode:sub(1, 2) == "no" then
return "o"
Expand Down

0 comments on commit 388bd3f

Please sign in to comment.