feat: extend lz.n with custom handlers (#17)

added require('lz.n').register_handler(lz.n.HandlerSpec)

added lz.n.Plugin.extras for extra spec items from custom handlers.

feat: extend lz.n with custom handlers

feat: extend lz.n with custom handlers

added tests

Update spec/register_handler_spec.lua



Update spec/register_handler_spec.lua



feat: extend lz.n with custom handlers

Update README.md



Update lua/lz/n/handler/init.lua

Co-authored-by: Marc Jakobi <mrcjkb89@outlook.com>
This commit is contained in:
Birdee
2024-06-24 09:07:08 -07:00
committed by GitHub
parent d530764a4e
commit d61186fc23
11 changed files with 126 additions and 26 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ local loader = require("lz.n.loader")
---@type lz.n.CmdHandler
local M = {
pending = {},
type = "cmd",
spec_field = "cmd",
}
---@param cmd string
+1 -1
View File
@@ -6,8 +6,8 @@ local loader = require("lz.n.loader")
---@type lz.n.ColorschemeHandler
local M = {
pending = {},
type = "colorscheme",
augroup = nil,
spec_field = "colorscheme",
}
---@param plugin lz.n.Plugin
+1 -1
View File
@@ -23,7 +23,7 @@ local M = {
pending = {},
events = {},
group = vim.api.nvim_create_augroup("lz_n_handler_event", { clear = true }),
type = "event",
spec_field = "event",
---@param spec lz.n.EventSpec
parse = function(spec)
local ret = lz_n_events[spec]
+1 -1
View File
@@ -6,7 +6,7 @@ local event = require("lz.n.handler.event")
---@type lz.n.FtHandler
local M = {
pending = {},
type = "ft",
spec_field = "ft",
---@param value string
---@return lz.n.Event
parse = function(value)
+26 -16
View File
@@ -1,20 +1,5 @@
---@class lz.n.Handler
---@field type lz.n.HandlerTypes
---@field pending table<string, table<string, string>> -- key: plugin_name: plugin_name
---@field add fun(plugin: lz.n.Plugin)
---@field del? fun(plugin: lz.n.Plugin)
local M = {}
---@enum lz.n.HandlerTypes
M.types = {
cmd = "cmd",
event = "event",
ft = "ft",
keys = "keys",
colorscheme = "colorscheme",
}
local handlers = {
cmd = require("lz.n.handler.cmd"),
event = require("lz.n.handler.event"),
@@ -23,6 +8,31 @@ local handlers = {
colorscheme = require("lz.n.handler.colorscheme"),
}
---@param spec lz.n.PluginSpec
---@return boolean
function M.is_lazy(spec)
---@diagnostic disable-next-line: undefined-field
return spec.lazy or vim.iter(handlers):any(function(spec_field, _)
return spec[spec_field] ~= nil
end)
end
---@param handler lz.n.Handler
---@return boolean success
function M.register_handler(handler)
if handlers[handler.spec_field] == nil then
handlers[handler.spec_field] = handler
return true
else
vim.notify(
"Handler already exists for " .. handler.spec_field .. ". Refusing to register new handler.",
vim.log.levels.ERROR,
{ title = "lz.n" }
)
return false
end
end
---@param plugin lz.n.Plugin
local function enable(plugin)
for _, handler in pairs(handlers) do
@@ -32,7 +42,7 @@ end
function M.disable(plugin)
for _, handler in pairs(handlers) do
if type(handler.del) == "function" then
if handler.del then
handler.del(plugin)
end
end
+1 -1
View File
@@ -5,7 +5,7 @@ local loader = require("lz.n.loader")
---@type lz.n.KeysHandler
local M = {
pending = {},
type = "keys",
spec_field = "keys",
---@param value string|lz.n.KeysSpec
---@param mode? string
---@return lz.n.Keys
+6
View File
@@ -14,6 +14,12 @@ local deferred_ui_enter = vim.schedule_wrap(function()
vim.api.nvim_exec_autocmds("User", { pattern = "DeferredUIEnter", modeline = false })
end)
---@type fun(handler: lz.n.Handler): boolean
M.register_handler = require("lz.n.handler").register_handler
---@type fun(plugins: string | lz.n.Plugin | string[] | lz.n.Plugin[])
M.trigger_load = require("lz.n.loader").load
---@overload fun(spec: lz.n.Spec)
---@overload fun(import: string)
function M.load(spec)
+5
View File
@@ -88,5 +88,10 @@ error("Cannot import a meta module")
--- Takes the plugin name (not the module name). Defaults to |packadd| if not set.
--- @field load? fun(name: string)
--- @class lz.n.Handler
--- @field spec_field string
--- @field add fun(plugin: lz.n.Plugin)
--- @field del? fun(plugin: lz.n.Plugin)
--- @type lz.n.Config
vim.g.lz_n = vim.g.lz_n
+1 -5
View File
@@ -144,11 +144,7 @@ local function parse(spec)
table.insert(result.colorscheme, _colorscheme_spec)
end
end
result.lazy = result.lazy
or result.event ~= nil
or result.keys ~= nil
or result.cmd ~= nil
or result.colorscheme ~= nil
result.lazy = require("lz.n.handler").is_lazy(spec)
return result
end