mirror of
https://github.com/zoriya/lz.n.git
synced 2026-05-31 18:13:01 +00:00
refactor(handlers): don't expose state (#64)
This commit is contained in:
@@ -2,23 +2,24 @@ local loader = require("lz.n.loader")
|
||||
|
||||
---@class lz.n.CmdHandler: lz.n.Handler
|
||||
|
||||
---@type table<string, table<string, lz.n.Plugin[]>>
|
||||
local pending = {}
|
||||
|
||||
---@type lz.n.CmdHandler
|
||||
local M = {
|
||||
---@type table<string, table<string, lz.n.Plugin[]>>
|
||||
pending = {},
|
||||
spec_field = "cmd",
|
||||
}
|
||||
|
||||
---@param name string
|
||||
---@return lz.n.Plugin?
|
||||
function M.lookup(name)
|
||||
return require("lz.n.handler.extra").lookup(M.pending, name)
|
||||
return require("lz.n.handler.extra").lookup(pending, name)
|
||||
end
|
||||
|
||||
---@param cmd string
|
||||
local function load(cmd)
|
||||
vim.api.nvim_del_user_command(cmd)
|
||||
loader.load(vim.tbl_values(M.pending[cmd]))
|
||||
loader.load(vim.tbl_values(pending[cmd]))
|
||||
end
|
||||
|
||||
---@param cmd string
|
||||
@@ -49,7 +50,7 @@ local function add_cmd(cmd)
|
||||
if not info then
|
||||
vim.schedule(function()
|
||||
---@type string
|
||||
local plugins = "`" .. table.concat(vim.tbl_values(M.pending[cmd]), ", ") .. "`"
|
||||
local plugins = "`" .. table.concat(vim.tbl_values(pending[cmd]), ", ") .. "`"
|
||||
vim.notify("Command `" .. cmd .. "` not found after loading " .. plugins, vim.log.levels.ERROR)
|
||||
end)
|
||||
return
|
||||
@@ -75,7 +76,7 @@ end
|
||||
|
||||
---@param name string
|
||||
function M.del(name)
|
||||
vim.iter(M.pending)
|
||||
vim.iter(pending)
|
||||
:filter(function(_, plugins)
|
||||
return plugins[name] ~= nil
|
||||
end)
|
||||
@@ -92,8 +93,8 @@ function M.add(plugin)
|
||||
end
|
||||
---@param cmd string
|
||||
vim.iter(plugin.cmd):each(function(cmd)
|
||||
M.pending[cmd] = M.pending[cmd] or {}
|
||||
M.pending[cmd][plugin.name] = plugin
|
||||
pending[cmd] = pending[cmd] or {}
|
||||
pending[cmd][plugin.name] = plugin
|
||||
add_cmd(cmd)
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -3,10 +3,11 @@ local loader = require("lz.n.loader")
|
||||
---@class lz.n.ColorschemeHandler: lz.n.Handler
|
||||
---@field augroup? integer
|
||||
|
||||
local pending = {}
|
||||
|
||||
---@type lz.n.ColorschemeHandler
|
||||
local M = {
|
||||
---@type table<string, table<string, lz.n.Plugin[]>>
|
||||
pending = {},
|
||||
augroup = nil,
|
||||
spec_field = "colorscheme",
|
||||
}
|
||||
@@ -14,24 +15,24 @@ local M = {
|
||||
---@param name string
|
||||
---@return lz.n.Plugin?
|
||||
function M.lookup(name)
|
||||
return require("lz.n.handler.extra").lookup(M.pending, name)
|
||||
return require("lz.n.handler.extra").lookup(pending, name)
|
||||
end
|
||||
|
||||
---@param name string
|
||||
function M.del(name)
|
||||
vim.iter(M.pending):each(function(_, plugins)
|
||||
vim.iter(pending):each(function(_, plugins)
|
||||
plugins[name] = nil
|
||||
end)
|
||||
end
|
||||
|
||||
---@param name string
|
||||
local function on_colorscheme(name)
|
||||
local pending = M.pending[name] or {}
|
||||
if vim.tbl_isempty(pending) then
|
||||
local plugins = pending[name] or {}
|
||||
if vim.tbl_isempty(plugins) then
|
||||
-- already loaded
|
||||
return
|
||||
end
|
||||
loader.load(vim.tbl_values(pending))
|
||||
loader.load(vim.tbl_values(plugins))
|
||||
end
|
||||
|
||||
local function init()
|
||||
@@ -55,8 +56,8 @@ function M.add(plugin)
|
||||
init()
|
||||
---@param colorscheme string
|
||||
vim.iter(plugin.colorscheme):each(function(colorscheme)
|
||||
M.pending[colorscheme] = M.pending[colorscheme] or {}
|
||||
M.pending[colorscheme][plugin.name] = plugin
|
||||
pending[colorscheme] = pending[colorscheme] or {}
|
||||
pending[colorscheme][plugin.name] = plugin
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
@@ -18,10 +18,11 @@ local lz_n_events = {
|
||||
|
||||
lz_n_events["User DeferredUIEnter"] = lz_n_events.DeferredUIEnter
|
||||
|
||||
---@type table<string, table<string, lz.n.Plugin[]>>
|
||||
local pending = {}
|
||||
|
||||
---@type lz.n.EventHandler
|
||||
local M = {
|
||||
---@type table<string, table<string, lz.n.Plugin[]>>
|
||||
pending = {},
|
||||
events = {},
|
||||
group = vim.api.nvim_create_augroup("lz_n_handler_event", { clear = true }),
|
||||
spec_field = "event",
|
||||
@@ -60,7 +61,7 @@ local M = {
|
||||
---@param name string
|
||||
---@return lz.n.Plugin?
|
||||
function M.lookup(name)
|
||||
return require("lz.n.handler.extra").lookup(M.pending, name)
|
||||
return require("lz.n.handler.extra").lookup(pending, name)
|
||||
end
|
||||
|
||||
-- Get all augroups for an event
|
||||
@@ -151,14 +152,14 @@ local function add_event(event)
|
||||
once = true,
|
||||
pattern = event.pattern,
|
||||
callback = function(ev)
|
||||
if done or not M.pending[event.id] then
|
||||
if done or not pending[event.id] then
|
||||
return
|
||||
end
|
||||
-- HACK: work-around for https://github.com/neovim/neovim/issues/25526
|
||||
done = true
|
||||
local state = get_state(ev.event, ev.buf, ev.data)
|
||||
-- load the plugins
|
||||
loader.load(M.pending[event.id])
|
||||
loader.load(pending[event.id])
|
||||
-- check if any plugin created an event handler for this event and fire the group
|
||||
---@param s lz.n.EventOpts
|
||||
vim.iter(state):each(function(s)
|
||||
@@ -172,15 +173,15 @@ end
|
||||
function M.add(plugin)
|
||||
---@param event lz.n.Event
|
||||
vim.iter(plugin.event or {}):each(function(event)
|
||||
M.pending[event.id] = M.pending[event.id] or {}
|
||||
M.pending[event.id][plugin.name] = plugin
|
||||
pending[event.id] = pending[event.id] or {}
|
||||
pending[event.id][plugin.name] = plugin
|
||||
add_event(event)
|
||||
end)
|
||||
end
|
||||
|
||||
---@param name string
|
||||
function M.del(name)
|
||||
vim.iter(M.pending):each(function(_, plugins)
|
||||
vim.iter(pending):each(function(_, plugins)
|
||||
plugins[name] = nil
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -23,10 +23,11 @@ local function parse(value, mode)
|
||||
return ret
|
||||
end
|
||||
|
||||
---@type table<string, table<string, lz.n.Plugin[]>>
|
||||
local pending = {}
|
||||
|
||||
---@type lz.n.KeysHandler
|
||||
local M = {
|
||||
---@type table<string, table<string, lz.n.Plugin[]>>
|
||||
pending = {},
|
||||
spec_field = "keys",
|
||||
---@param value string|lz.n.KeysSpec
|
||||
---@return lz.n.Keys[]
|
||||
@@ -47,7 +48,7 @@ local M = {
|
||||
---@param name string
|
||||
---@return lz.n.Plugin?
|
||||
function M.lookup(name)
|
||||
return require("lz.n.handler.extra").lookup(M.pending, name)
|
||||
return require("lz.n.handler.extra").lookup(pending, name)
|
||||
end
|
||||
|
||||
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
|
||||
@@ -100,10 +101,10 @@ local function add_keys(keys)
|
||||
---@param buf? number
|
||||
local function add(buf)
|
||||
vim.keymap.set(keys.mode, lhs, function()
|
||||
local plugins = M.pending[keys.id]
|
||||
local plugins = pending[keys.id]
|
||||
-- always delete the mapping immediately to prevent recursive mappings
|
||||
del(keys)
|
||||
M.pending[keys.id] = nil
|
||||
pending[keys.id] = nil
|
||||
if plugins then
|
||||
loader.load(plugins)
|
||||
end
|
||||
@@ -130,7 +131,7 @@ local function add_keys(keys)
|
||||
vim.api.nvim_create_autocmd("FileType", {
|
||||
pattern = keys.ft,
|
||||
callback = function(event)
|
||||
if M.pending[keys.id] then
|
||||
if pending[keys.id] then
|
||||
add(event.buf)
|
||||
else
|
||||
-- Only create the mapping if its managed by lz.n
|
||||
@@ -148,15 +149,15 @@ end
|
||||
function M.add(plugin)
|
||||
---@param key lz.n.Keys
|
||||
vim.iter(plugin.keys or {}):each(function(key)
|
||||
M.pending[key.id] = M.pending[key.id] or {}
|
||||
M.pending[key.id][plugin.name] = plugin
|
||||
pending[key.id] = pending[key.id] or {}
|
||||
pending[key.id][plugin.name] = plugin
|
||||
add_keys(key)
|
||||
end)
|
||||
end
|
||||
|
||||
---@param name string
|
||||
function M.del(name)
|
||||
vim.iter(M.pending):each(function(_, plugins)
|
||||
vim.iter(pending):each(function(_, plugins)
|
||||
plugins[name] = nil
|
||||
end)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user