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

@ -67,25 +67,32 @@ function M:_add(value) end
---@protected
function M:_del(value) end
---@return string
function M:key(value)
return value
end
---@param plugin LazyPlugin
function M:add(plugin)
for _, value in ipairs(plugin[self.type] or {}) do
if not self.active[value] then
self.active[value] = {}
local key = self:key(value)
if not self.active[key] then
self.active[key] = {}
self:_add(value)
end
self.active[value][plugin.name] = plugin.name
self.active[key][plugin.name] = plugin.name
end
end
---@param plugin LazyPlugin
function M:del(plugin)
for _, value in ipairs(plugin[self.type] or {}) do
if self.active[value] and self.active[value][plugin.name] then
self.active[value][plugin.name] = nil
if vim.tbl_isempty(self.active[value]) then
local key = self:key(value)
if self.active[key] and self.active[key][plugin.name] then
self.active[key][plugin.name] = nil
if vim.tbl_isempty(self.active[key]) then
self:_del(value)
self.active[value] = nil
self.active[key] = nil
end
end
end

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)