chore: rework handler add signatures

This commit is contained in:
Marc Jakobi
2024-05-24 20:27:00 +02:00
parent e61adde6ce
commit b10b02f0b4
6 changed files with 38 additions and 27 deletions
+13 -6
View File
@@ -4,8 +4,7 @@ local loader = require('lz.n.loader')
---@type LzKeysHandler
local M = {
active = {},
managed = {},
pending = {},
type = 'keys',
}
@@ -74,17 +73,17 @@ local function del(keys)
end
---@param keys LzKeys
M.add = function(keys)
local function add_keys(keys)
local lhs = keys.lhs
local opts = get_opts(keys)
---@param buf? number
local function add(buf)
vim.keymap.set(keys.mode, lhs, function()
local plugins = M.active[keys.id]
local plugins = M.pending[keys.id]
-- always delete the mapping immediately to prevent recursive mappings
del(keys)
M.active[keys.id] = nil
M.pending[keys.id] = nil
if plugins then
loader.load(plugins)
end
@@ -111,7 +110,7 @@ M.add = function(keys)
vim.api.nvim_create_autocmd('FileType', {
pattern = keys.ft,
callback = function(event)
if M.active[keys.id] then
if M.pending[keys.id] then
add(event.buf)
else
-- Only create the mapping if its managed by lz.n
@@ -125,4 +124,12 @@ M.add = function(keys)
end
end
---@param plugin LzPlugin
function M.add(plugin)
-- TODO add plugin to M.pending
for _, key in pairs(plugin.keys or {}) do
add_keys(key)
end
end
return M