fix(keys): key handlers were not working after reload

This commit is contained in:
Folke Lemaitre 2022-12-22 21:58:01 +01:00
parent b5d6afc4fa
commit 3f60f2dc13
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 35 additions and 9 deletions

View file

@ -29,7 +29,7 @@ end
---@param value string|LazyKeys
function M.parse(value)
local ret = vim.deepcopy(value)
ret = (type(ret) == "string" and { ret } or ret) --[[@as LazyKeys]]
ret = type(ret) == "string" and { ret } or ret --[[@as LazyKeys]]
ret.mode = ret.mode or "n"
return ret
end
@ -44,14 +44,33 @@ function M.opts(keys)
return opts
end
---@return string
function M:key(value)
if type(value) == "string" then
return value
end
local mode = value.mode or { "n" }
if type(mode) == "string" then
mode = { mode }
end
---@type string
local ret = value[1]
if #mode > 0 then
ret = table.concat(mode, ",") .. ": " .. ret
end
return ret
end
---@param value string|LazyKeys
function M:_add(value)
local keys = M.parse(value)
local lhs = keys[1]
local opts = M.opts(keys)
opts.noremap = true
vim.keymap.set(keys.mode, lhs, function()
pcall(vim.keymap.del, keys.mode, lhs)
Util.track({ keys = lhs })
Loader.load(self.active[value], { keys = lhs })
Loader.load(self.active[self:key(value)], { keys = lhs })
M.retrigger(lhs)
Util.track()
end, opts)