From 3f60f2dc13faf4d958fdaec16596436ade2ec23d Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Thu, 22 Dec 2022 21:58:01 +0100 Subject: [PATCH] fix(keys): key handlers were not working after reload --- lua/lazy/core/handler/init.lua | 21 ++++++++++++++------- lua/lazy/core/handler/keys.lua | 23 +++++++++++++++++++++-- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/lua/lazy/core/handler/init.lua b/lua/lazy/core/handler/init.lua index 4cca808..99568f4 100644 --- a/lua/lazy/core/handler/init.lua +++ b/lua/lazy/core/handler/init.lua @@ -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 diff --git a/lua/lazy/core/handler/keys.lua b/lua/lazy/core/handler/keys.lua index 840be87..a6eb9ee 100644 --- a/lua/lazy/core/handler/keys.lua +++ b/lua/lazy/core/handler/keys.lua @@ -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)