mirror of
https://github.com/zoriya/lz.n.git
synced 2025-12-22 14:45:42 +00:00
chore: types: Lz -> lz.n prefix
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
local loader = require("lz.n.loader")
|
local loader = require("lz.n.loader")
|
||||||
|
|
||||||
---@class LzCmdHandler: LzHandler
|
---@class lz.n.CmdHandler: lz.n.Handler
|
||||||
|
|
||||||
---@type LzCmdHandler
|
---@type lz.n.CmdHandler
|
||||||
local M = {
|
local M = {
|
||||||
pending = {},
|
pending = {},
|
||||||
type = "cmd",
|
type = "cmd",
|
||||||
@@ -66,7 +66,7 @@ local function add_cmd(cmd)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param plugin LzPlugin
|
---@param plugin lz.n.Plugin
|
||||||
function M.del(plugin)
|
function M.del(plugin)
|
||||||
pcall(vim.api.nvim_del_user_command, plugin.cmd)
|
pcall(vim.api.nvim_del_user_command, plugin.cmd)
|
||||||
for _, plugins in pairs(M.pending) do
|
for _, plugins in pairs(M.pending) do
|
||||||
@@ -74,7 +74,7 @@ function M.del(plugin)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param plugin LzPlugin
|
---@param plugin lz.n.Plugin
|
||||||
function M.add(plugin)
|
function M.add(plugin)
|
||||||
if not plugin.cmd then
|
if not plugin.cmd then
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
local loader = require("lz.n.loader")
|
local loader = require("lz.n.loader")
|
||||||
|
|
||||||
---@class LzEventOpts
|
---@class lz.n.EventOpts
|
||||||
---@field event string
|
---@field event string
|
||||||
---@field group? string
|
---@field group? string
|
||||||
---@field exclude? string[] augroups to exclude
|
---@field exclude? string[] augroups to exclude
|
||||||
---@field data? unknown
|
---@field data? unknown
|
||||||
---@field buffer? number
|
---@field buffer? number
|
||||||
|
|
||||||
---@class LzEventHandler: LzHandler
|
---@class lz.n.EventHandler: lz.n.Handler
|
||||||
---@field events table<string,true>
|
---@field events table<string,true>
|
||||||
---@field group number
|
---@field group number
|
||||||
---@field parse fun(spec: LzEventSpec): LzEvent
|
---@field parse fun(spec: lz.n.EventSpec): lz.n.Event
|
||||||
|
|
||||||
---@type LzEventHandler
|
---@type lz.n.EventHandler
|
||||||
local M = {
|
local M = {
|
||||||
pending = {},
|
pending = {},
|
||||||
events = {},
|
events = {},
|
||||||
@@ -27,7 +27,7 @@ local M = {
|
|||||||
elseif vim.islist(spec) then
|
elseif vim.islist(spec) then
|
||||||
ret = { id = table.concat(spec, "|"), event = spec }
|
ret = { id = table.concat(spec, "|"), event = spec }
|
||||||
else
|
else
|
||||||
ret = spec --[[@as LzEvent]]
|
ret = spec --[[@as lz.n.Event]]
|
||||||
if not ret.id then
|
if not ret.id then
|
||||||
---@diagnostic disable-next-line: assign-type-mismatch, param-type-mismatch
|
---@diagnostic disable-next-line: assign-type-mismatch, param-type-mismatch
|
||||||
ret.id = type(ret.event) == "string" and ret.event or table.concat(ret.event, "|")
|
ret.id = type(ret.event) == "string" and ret.event or table.concat(ret.event, "|")
|
||||||
@@ -67,12 +67,12 @@ local event_triggers = {
|
|||||||
---@param event string
|
---@param event string
|
||||||
---@param buf integer
|
---@param buf integer
|
||||||
---@param data unknown
|
---@param data unknown
|
||||||
---@return LzEventOpts[]
|
---@return lz.n.EventOpts[]
|
||||||
local function get_state(event, buf, data)
|
local function get_state(event, buf, data)
|
||||||
---@type LzEventOpts[]
|
---@type lz.n.EventOpts[]
|
||||||
local state = {}
|
local state = {}
|
||||||
while event do
|
while event do
|
||||||
---@type LzEventOpts
|
---@type lz.n.EventOpts
|
||||||
local event_opts = {
|
local event_opts = {
|
||||||
event = event,
|
event = event,
|
||||||
exclude = event ~= "FileType" and get_augroups(event) or nil,
|
exclude = event ~= "FileType" and get_augroups(event) or nil,
|
||||||
@@ -87,7 +87,7 @@ local function get_state(event, buf, data)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Trigger an event
|
-- Trigger an event
|
||||||
---@param opts LzEventOpts
|
---@param opts lz.n.EventOpts
|
||||||
local function _trigger(opts)
|
local function _trigger(opts)
|
||||||
xpcall(
|
xpcall(
|
||||||
function()
|
function()
|
||||||
@@ -106,7 +106,7 @@ end
|
|||||||
|
|
||||||
-- Trigger an event. When a group is given, only the events in that group will be triggered.
|
-- Trigger an event. When a group is given, only the events in that group will be triggered.
|
||||||
-- When exclude is set, the events in those groups will be skipped.
|
-- When exclude is set, the events in those groups will be skipped.
|
||||||
---@param opts LzEventOpts
|
---@param opts lz.n.EventOpts
|
||||||
local function trigger(opts)
|
local function trigger(opts)
|
||||||
if opts.group or opts.exclude == nil then
|
if opts.group or opts.exclude == nil then
|
||||||
return _trigger(opts)
|
return _trigger(opts)
|
||||||
@@ -125,7 +125,7 @@ local function trigger(opts)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param event LzEvent
|
---@param event lz.n.Event
|
||||||
local function add_event(event)
|
local function add_event(event)
|
||||||
local done = false
|
local done = false
|
||||||
vim.api.nvim_create_autocmd(event.event, {
|
vim.api.nvim_create_autocmd(event.event, {
|
||||||
@@ -149,7 +149,7 @@ local function add_event(event)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param plugin LzPlugin
|
---@param plugin lz.n.Plugin
|
||||||
function M.add(plugin)
|
function M.add(plugin)
|
||||||
for _, event in pairs(plugin.event or {}) do
|
for _, event in pairs(plugin.event or {}) do
|
||||||
M.pending[event.id] = M.pending[event.id] or {}
|
M.pending[event.id] = M.pending[event.id] or {}
|
||||||
@@ -158,7 +158,7 @@ function M.add(plugin)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param plugin LzPlugin
|
---@param plugin lz.n.Plugin
|
||||||
function M.del(plugin)
|
function M.del(plugin)
|
||||||
for _, plugins in pairs(M.pending) do
|
for _, plugins in pairs(M.pending) do
|
||||||
plugins[plugin.name] = nil
|
plugins[plugin.name] = nil
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
local event = require("lz.n.handler.event")
|
local event = require("lz.n.handler.event")
|
||||||
|
|
||||||
---@class LzFtHandler: LzHandler
|
---@class lz.n.FtHandler: lz.n.Handler
|
||||||
---@field parse fun(spec: LzEventSpec): LzEvent
|
---@field parse fun(spec: lz.n.EventSpec): lz.n.Event
|
||||||
|
|
||||||
---@type LzFtHandler
|
---@type lz.n.FtHandler
|
||||||
local M = {
|
local M = {
|
||||||
pending = {},
|
pending = {},
|
||||||
type = "ft",
|
type = "ft",
|
||||||
---@param value string
|
---@param value string
|
||||||
---@return LzEvent
|
---@return lz.n.Event
|
||||||
parse = function(value)
|
parse = function(value)
|
||||||
return {
|
return {
|
||||||
id = value,
|
id = value,
|
||||||
@@ -18,12 +18,12 @@ local M = {
|
|||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
---@param plugin LzPlugin
|
---@param plugin lz.n.Plugin
|
||||||
function M.add(plugin)
|
function M.add(plugin)
|
||||||
event.add(plugin)
|
event.add(plugin)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param plugin LzPlugin
|
---@param plugin lz.n.Plugin
|
||||||
function M.del(plugin)
|
function M.del(plugin)
|
||||||
event.del(plugin)
|
event.del(plugin)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
---@class LzHandler
|
---@class lz.n.Handler
|
||||||
---@field type LzHandlerTypes
|
---@field type lz.n.HandlerTypes
|
||||||
---@field pending table<string, table<string, string>> -- key: plugin_name: plugin_name
|
---@field pending table<string, table<string, string>> -- key: plugin_name: plugin_name
|
||||||
---@field add fun(plugin: LzPlugin)
|
---@field add fun(plugin: lz.n.Plugin)
|
||||||
---@field del? fun(plugin: LzPlugin)
|
---@field del? fun(plugin: lz.n.Plugin)
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@enum LzHandlerTypes
|
---@enum lz.n.HandlerTypes
|
||||||
M.types = {
|
M.types = {
|
||||||
cmd = "cmd",
|
cmd = "cmd",
|
||||||
event = "event",
|
event = "event",
|
||||||
@@ -21,24 +21,24 @@ local handlers = {
|
|||||||
keys = require("lz.n.handler.keys"),
|
keys = require("lz.n.handler.keys"),
|
||||||
}
|
}
|
||||||
|
|
||||||
---@param plugin LzPlugin
|
---@param plugin lz.n.Plugin
|
||||||
local function enable(plugin)
|
local function enable(plugin)
|
||||||
for _, handler in pairs(handlers) do
|
for _, handler in pairs(handlers) do
|
||||||
handler.add(plugin)
|
handler.add(plugin)
|
||||||
end
|
end
|
||||||
-- TODO: Change handler add implementations to take a LzPlugin
|
-- TODO: Change handler add implementations to take a lz.n.Plugin
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.disable(plugin)
|
function M.disable(plugin)
|
||||||
for _, handler in pairs(handlers) do
|
for _, handler in pairs(handlers) do
|
||||||
if type(handler.del) == "function" then
|
if type(handler.del) == "function" then
|
||||||
-- TODO: Change handler del implementations to take a LzPlugin?
|
-- TODO: Change handler del implementations to take a lz.n.Plugin?
|
||||||
handler.del(plugin)
|
handler.del(plugin)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param plugins table<string, LzPlugin>
|
---@param plugins table<string, lz.n.Plugin>
|
||||||
function M.init(plugins)
|
function M.init(plugins)
|
||||||
for _, plugin in pairs(plugins) do
|
for _, plugin in pairs(plugins) do
|
||||||
xpcall(
|
xpcall(
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
local loader = require("lz.n.loader")
|
local loader = require("lz.n.loader")
|
||||||
|
|
||||||
---@class LzKeysHandler: LzHandler
|
---@class lz.n.KeysHandler: lz.n.Handler
|
||||||
|
|
||||||
---@type LzKeysHandler
|
---@type lz.n.KeysHandler
|
||||||
local M = {
|
local M = {
|
||||||
pending = {},
|
pending = {},
|
||||||
type = "keys",
|
type = "keys",
|
||||||
---@param value string|LzKeysSpec
|
---@param value string|lz.n.KeysSpec
|
||||||
---@param mode? string
|
---@param mode? string
|
||||||
---@return LzKeys
|
---@return lz.n.Keys
|
||||||
parse = function(value, mode)
|
parse = function(value, mode)
|
||||||
value = type(value) == "string" and { value } or value --[[@as LzKeysSpec]]
|
value = type(value) == "string" and { value } or value --[[@as lz.n.KeysSpec]]
|
||||||
local ret = vim.deepcopy(value) --[[@as LzKeys]]
|
local ret = vim.deepcopy(value) --[[@as lz.n.Keys]]
|
||||||
ret.lhs = ret[1] or ""
|
ret.lhs = ret[1] or ""
|
||||||
ret.rhs = ret[2]
|
ret.rhs = ret[2]
|
||||||
ret[1] = nil
|
ret[1] = nil
|
||||||
@@ -31,10 +31,10 @@ local M = {
|
|||||||
|
|
||||||
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
|
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
|
||||||
|
|
||||||
---@param keys LzKeys
|
---@param keys lz.n.Keys
|
||||||
---@return LzKeysBase
|
---@return lz.n.KeysBase
|
||||||
local function get_opts(keys)
|
local function get_opts(keys)
|
||||||
---@type LzKeysBase
|
---@type lz.n.KeysBase
|
||||||
local opts = {}
|
local opts = {}
|
||||||
for k, v in pairs(keys) do
|
for k, v in pairs(keys) do
|
||||||
if type(k) ~= "number" and not skip[k] then
|
if type(k) ~= "number" and not skip[k] then
|
||||||
@@ -45,7 +45,7 @@ local function get_opts(keys)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Create a mapping if it is managed by lz.n
|
-- Create a mapping if it is managed by lz.n
|
||||||
---@param keys LzKeys
|
---@param keys lz.n.Keys
|
||||||
---@param buf integer?
|
---@param buf integer?
|
||||||
local function set(keys, buf)
|
local function set(keys, buf)
|
||||||
if keys.rhs then
|
if keys.rhs then
|
||||||
@@ -58,7 +58,7 @@ end
|
|||||||
|
|
||||||
-- Delete a mapping and create the real global
|
-- Delete a mapping and create the real global
|
||||||
-- mapping when needed
|
-- mapping when needed
|
||||||
---@param keys LzKeys
|
---@param keys lz.n.Keys
|
||||||
local function del(keys)
|
local function del(keys)
|
||||||
pcall(vim.keymap.del, keys.mode, keys.lhs, {
|
pcall(vim.keymap.del, keys.mode, keys.lhs, {
|
||||||
-- NOTE: for buffer-local mappings, we only delete the mapping for the current buffer
|
-- NOTE: for buffer-local mappings, we only delete the mapping for the current buffer
|
||||||
@@ -72,7 +72,7 @@ local function del(keys)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param keys LzKeys
|
---@param keys lz.n.Keys
|
||||||
local function add_keys(keys)
|
local function add_keys(keys)
|
||||||
local lhs = keys.lhs
|
local lhs = keys.lhs
|
||||||
local opts = get_opts(keys)
|
local opts = get_opts(keys)
|
||||||
@@ -124,7 +124,7 @@ local function add_keys(keys)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param plugin LzPlugin
|
---@param plugin lz.n.Plugin
|
||||||
function M.add(plugin)
|
function M.add(plugin)
|
||||||
for _, key in pairs(plugin.keys or {}) do
|
for _, key in pairs(plugin.keys or {}) do
|
||||||
M.pending[key.id] = M.pending[key.id] or {}
|
M.pending[key.id] = M.pending[key.id] or {}
|
||||||
@@ -133,7 +133,7 @@ function M.add(plugin)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param plugin LzPlugin
|
---@param plugin lz.n.Plugin
|
||||||
function M.del(plugin)
|
function M.del(plugin)
|
||||||
for _, plugins in pairs(M.pending) do
|
for _, plugins in pairs(M.pending) do
|
||||||
plugins[plugin.name] = nil
|
plugins[plugin.name] = nil
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ if not vim.loader or vim.fn.has("nvim-0.9.1") ~= 1 then
|
|||||||
error("lz.n requires Neovim >= 0.9.1")
|
error("lz.n requires Neovim >= 0.9.1")
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param spec string | LzSpec
|
---@param spec string | lz.n.Spec
|
||||||
function M.load(spec)
|
function M.load(spec)
|
||||||
if vim.g.lzn_did_load then
|
if vim.g.lzn_did_load then
|
||||||
return vim.notify("lz.n has already loaded your plugins.", vim.log.levels.WARN, { title = "lz.n" })
|
return vim.notify("lz.n has already loaded your plugins.", vim.log.levels.WARN, { title = "lz.n" })
|
||||||
@@ -17,7 +17,7 @@ function M.load(spec)
|
|||||||
if type(spec) == "string" then
|
if type(spec) == "string" then
|
||||||
spec = { import = spec }
|
spec = { import = spec }
|
||||||
end
|
end
|
||||||
---@cast spec LzSpec
|
---@cast spec lz.n.Spec
|
||||||
local plugins = require("lz.n.spec").parse(spec)
|
local plugins = require("lz.n.spec").parse(spec)
|
||||||
require("lz.n.loader").load_startup_plugins(plugins)
|
require("lz.n.loader").load_startup_plugins(plugins)
|
||||||
require("lz.n.state").plugins = plugins
|
require("lz.n.state").plugins = plugins
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ local M = {}
|
|||||||
local DEFAULT_PRIORITY = 50
|
local DEFAULT_PRIORITY = 50
|
||||||
|
|
||||||
---@package
|
---@package
|
||||||
---@param plugin LzPlugin
|
---@param plugin lz.n.Plugin
|
||||||
function M._load(plugin)
|
function M._load(plugin)
|
||||||
if plugin.enable == false or (type(plugin.enable) == "function" and not plugin.enable()) then
|
if plugin.enable == false or (type(plugin.enable) == "function" and not plugin.enable()) then
|
||||||
return
|
return
|
||||||
@@ -16,7 +16,7 @@ function M._load(plugin)
|
|||||||
-- TODO: Load plugin
|
-- TODO: Load plugin
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param plugins table<string, LzPlugin>
|
---@param plugins table<string, lz.n.Plugin>
|
||||||
local function run_before_all(plugins)
|
local function run_before_all(plugins)
|
||||||
for _, plugin in pairs(plugins) do
|
for _, plugin in pairs(plugins) do
|
||||||
if plugin.beforeAll then
|
if plugin.beforeAll then
|
||||||
@@ -34,8 +34,8 @@ local function run_before_all(plugins)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param plugins table<string, LzPlugin>
|
---@param plugins table<string, lz.n.Plugin>
|
||||||
---@return LzPlugin[]
|
---@return lz.n.Plugin[]
|
||||||
local function get_eager_plugins(plugins)
|
local function get_eager_plugins(plugins)
|
||||||
local result = {}
|
local result = {}
|
||||||
for _, plugin in pairs(plugins) do
|
for _, plugin in pairs(plugins) do
|
||||||
@@ -44,15 +44,15 @@ local function get_eager_plugins(plugins)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.sort(result, function(a, b)
|
table.sort(result, function(a, b)
|
||||||
---@cast a LzPlugin
|
---@cast a lz.n.Plugin
|
||||||
---@cast b LzPlugin
|
---@cast b lz.n.Plugin
|
||||||
return (a.priority or DEFAULT_PRIORITY) > (b.priority or DEFAULT_PRIORITY)
|
return (a.priority or DEFAULT_PRIORITY) > (b.priority or DEFAULT_PRIORITY)
|
||||||
end)
|
end)
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Loads startup plugins, removing loaded plugins from the table
|
--- Loads startup plugins, removing loaded plugins from the table
|
||||||
---@param plugins table<string, LzPlugin>
|
---@param plugins table<string, lz.n.Plugin>
|
||||||
function M.load_startup_plugins(plugins)
|
function M.load_startup_plugins(plugins)
|
||||||
run_before_all(plugins)
|
run_before_all(plugins)
|
||||||
for _, plugin in pairs(get_eager_plugins(plugins)) do
|
for _, plugin in pairs(get_eager_plugins(plugins)) do
|
||||||
@@ -61,10 +61,10 @@ function M.load_startup_plugins(plugins)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param plugins string | LzPlugin | string[] | LzPlugin[]
|
---@param plugins string | lz.n.Plugin | string[] | lz.n.Plugin[]
|
||||||
function M.load(plugins)
|
function M.load(plugins)
|
||||||
plugins = (type(plugins) == "string" or plugins.name) and { plugins } or plugins
|
plugins = (type(plugins) == "string" or plugins.name) and { plugins } or plugins
|
||||||
---@cast plugins (string|LzPlugin)[]
|
---@cast plugins (string|lz.n.Plugin)[]
|
||||||
for _, plugin in pairs(plugins) do
|
for _, plugin in pairs(plugins) do
|
||||||
local loadable = true
|
local loadable = true
|
||||||
if type(plugin) == "string" then
|
if type(plugin) == "string" then
|
||||||
@@ -74,7 +74,7 @@ function M.load(plugins)
|
|||||||
vim.notify("Plugin " .. plugin .. " not found", vim.log.levels.ERROR, { title = "lz.n" })
|
vim.notify("Plugin " .. plugin .. " not found", vim.log.levels.ERROR, { title = "lz.n" })
|
||||||
loadable = false
|
loadable = false
|
||||||
end
|
end
|
||||||
---@cast plugin LzPlugin
|
---@cast plugin lz.n.Plugin
|
||||||
end
|
end
|
||||||
if loadable then
|
if loadable then
|
||||||
M._load(plugin)
|
M._load(plugin)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@param spec LzSpecImport
|
---@param spec lz.n.SpecImport
|
||||||
---@param result table<string, LzPlugin>
|
---@param result table<string, lz.n.Plugin>
|
||||||
local function import_spec(spec, result)
|
local function import_spec(spec, result)
|
||||||
if spec.import == "lz.n" then
|
if spec.import == "lz.n" then
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
@@ -45,10 +45,10 @@ local function import_spec(spec, result)
|
|||||||
M._normalize(mod, result)
|
M._normalize(mod, result)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param spec LzPluginSpec
|
---@param spec lz.n.PluginSpec
|
||||||
---@return LzPlugin
|
---@return lz.n.Plugin
|
||||||
local function parse(spec)
|
local function parse(spec)
|
||||||
---@type LzPlugin
|
---@type lz.n.Plugin
|
||||||
---@diagnostic disable-next-line: assign-type-mismatch
|
---@diagnostic disable-next-line: assign-type-mismatch
|
||||||
local result = vim.deepcopy(spec)
|
local result = vim.deepcopy(spec)
|
||||||
local event_spec = spec.event
|
local event_spec = spec.event
|
||||||
@@ -57,25 +57,25 @@ local function parse(spec)
|
|||||||
end
|
end
|
||||||
if type(event_spec) == "string" then
|
if type(event_spec) == "string" then
|
||||||
local event = require("lz.n.handler.event").parse(event_spec)
|
local event = require("lz.n.handler.event").parse(event_spec)
|
||||||
result.event[event.id] = event
|
table.insert(result.event, event)
|
||||||
elseif type(event_spec) == "table" then
|
elseif type(event_spec) == "table" then
|
||||||
---@cast event_spec LzEventSpec[]
|
---@cast event_spec lz.n.EventSpec[]
|
||||||
for _, _event_spec in pairs(event_spec) do
|
for _, _event_spec in pairs(event_spec) do
|
||||||
local event = require("lz.n.handler.event").parse(_event_spec)
|
local event = require("lz.n.handler.event").parse(_event_spec)
|
||||||
result.ft[event.id] = event
|
table.insert(result.event, event)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local ft_spec = spec.ft
|
local ft_spec = spec.ft
|
||||||
if ft_spec then
|
if ft_spec then
|
||||||
result.ft = {}
|
result.event = result.event or {}
|
||||||
end
|
end
|
||||||
if type(ft_spec) == "string" then
|
if type(ft_spec) == "string" then
|
||||||
local ft = require("lz.n.handler.ft").parse(ft_spec)
|
local ft = require("lz.n.handler.ft").parse(ft_spec)
|
||||||
result[ft.id] = ft
|
table.insert(result.event, ft)
|
||||||
elseif type(ft_spec) == "table" then
|
elseif type(ft_spec) == "table" then
|
||||||
for _, _ft_spec in pairs(ft_spec) do
|
for _, _ft_spec in pairs(ft_spec) do
|
||||||
local ft = require("lz.n.handler.ft").parse(_ft_spec)
|
local ft = require("lz.n.handler.ft").parse(_ft_spec)
|
||||||
result.ft[ft.id] = ft
|
table.insert(result.event, ft)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local keys_spec = spec.keys
|
local keys_spec = spec.keys
|
||||||
@@ -84,12 +84,12 @@ local function parse(spec)
|
|||||||
end
|
end
|
||||||
if type(keys_spec) == "string" then
|
if type(keys_spec) == "string" then
|
||||||
local keys = require("lz.n.handler.keys").parse(keys_spec)
|
local keys = require("lz.n.handler.keys").parse(keys_spec)
|
||||||
result.keys[keys.id] = keys
|
table.insert(result.keys, keys)
|
||||||
elseif type(keys_spec) == "table" then
|
elseif type(keys_spec) == "table" then
|
||||||
---@cast keys_spec string[] | LzKeysSpec[]
|
---@cast keys_spec string[] | lz.n.KeysSpec[]
|
||||||
for _, _keys_spec in pairs(keys_spec) do
|
for _, _keys_spec in pairs(keys_spec) do
|
||||||
local keys = require("lz.n.handler.keys").parse(_keys_spec)
|
local keys = require("lz.n.handler.keys").parse(_keys_spec)
|
||||||
result.keys[keys.id] = keys
|
table.insert(result.keys, keys)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local cmd_spec = spec.cmd
|
local cmd_spec = spec.cmd
|
||||||
@@ -97,33 +97,33 @@ local function parse(spec)
|
|||||||
result.cmd = {}
|
result.cmd = {}
|
||||||
end
|
end
|
||||||
if type(cmd_spec) == "string" then
|
if type(cmd_spec) == "string" then
|
||||||
result.cmd[cmd_spec] = cmd_spec
|
table.insert(result.cmd, cmd_spec)
|
||||||
elseif type(cmd_spec) == "table" then
|
elseif type(cmd_spec) == "table" then
|
||||||
for _, _cmd_spec in pairs(cmd_spec) do
|
for _, _cmd_spec in pairs(cmd_spec) do
|
||||||
result.cmd[_cmd_spec] = _cmd_spec
|
table.insert(result.cmd, _cmd_spec)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
---@param spec LzSpec
|
---@param spec lz.n.Spec
|
||||||
---@param result table<string, LzPlugin>
|
---@param result table<string, lz.n.Plugin>
|
||||||
function M._normalize(spec, result)
|
function M._normalize(spec, result)
|
||||||
if #spec > 1 or vim.islist(spec) then
|
if #spec > 1 or vim.islist(spec) then
|
||||||
for _, sp in ipairs(spec) do
|
for _, sp in ipairs(spec) do
|
||||||
M._normalize(sp, result)
|
M._normalize(sp, result)
|
||||||
end
|
end
|
||||||
elseif spec.name then
|
elseif spec.name then
|
||||||
---@cast spec LzPluginSpec
|
---@cast spec lz.n.PluginSpec
|
||||||
result[spec.name] = parse(spec)
|
result[spec.name] = parse(spec)
|
||||||
elseif spec.import then
|
elseif spec.import then
|
||||||
---@cast spec LzSpecImport
|
---@cast spec lz.n.SpecImport
|
||||||
import_spec(spec, result)
|
import_spec(spec, result)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param result table<string, LzPlugin>
|
---@param result table<string, lz.n.Plugin>
|
||||||
local function remove_disabled_plugins(result)
|
local function remove_disabled_plugins(result)
|
||||||
for _, plugin in ipairs(result) do
|
for _, plugin in ipairs(result) do
|
||||||
local disabled = plugin.enabled == false or (type(plugin.enabled) == "function" and not plugin.enabled())
|
local disabled = plugin.enabled == false or (type(plugin.enabled) == "function" and not plugin.enabled())
|
||||||
@@ -133,8 +133,8 @@ local function remove_disabled_plugins(result)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param spec LzSpec
|
---@param spec lz.n.Spec
|
||||||
---@return table<string, LzPlugin>
|
---@return table<string, lz.n.Plugin>
|
||||||
function M.parse(spec)
|
function M.parse(spec)
|
||||||
local result = {}
|
local result = {}
|
||||||
M._normalize(spec, result)
|
M._normalize(spec, result)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@type table<string, LzPlugin>
|
---@type table<string, lz.n.Plugin>
|
||||||
M.plugins = {}
|
M.plugins = {}
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -10,40 +10,40 @@ error("Cannot import a meta module")
|
|||||||
---@field name? string Name of the config function. Defaults to 'setup', the most common in the Neovim plugin community.
|
---@field name? string Name of the config function. Defaults to 'setup', the most common in the Neovim plugin community.
|
||||||
---@field type 'func'
|
---@field type 'func'
|
||||||
|
|
||||||
---@alias LzPluginOptsSpec VimGTable | ConfigFunction How a plugin accepts its options
|
---@alias lz.n.PluginOptsSpec VimGTable | ConfigFunction How a plugin accepts its options
|
||||||
|
|
||||||
---@class LzPluginBase
|
---@class lz.n.PluginBase
|
||||||
---@field name string Display name and name used for plugin config files, e.g. "neorg"
|
---@field name string Display name and name used for plugin config files, e.g. "neorg"
|
||||||
---@field optsSpec? LzPluginOptsSpec
|
---@field optsSpec? lz.n.PluginOptsSpec
|
||||||
---@field enabled? boolean|(fun():boolean)
|
---@field enabled? boolean|(fun():boolean)
|
||||||
---@field enable? boolean|(fun():boolean) Whether to enable this plugin. Useful to disable plugins under certain conditions.
|
---@field enable? boolean|(fun():boolean) Whether to enable this plugin. Useful to disable plugins under certain conditions.
|
||||||
---@field lazy? boolean
|
---@field lazy? boolean
|
||||||
---@field priority? number Only useful for lazy=false plugins to force loading certain plugins first. Default priority is 50
|
---@field priority? number Only useful for lazy=false plugins to force loading certain plugins first. Default priority is 50
|
||||||
|
|
||||||
---@alias LzEvent {id:string, event:string[]|string, pattern?:string[]|string}
|
---@alias lz.n.Event {id:string, event:string[]|string, pattern?:string[]|string}
|
||||||
---@alias LzEventSpec string|{event?:string|string[], pattern?:string|string[]}|string[]
|
---@alias lz.n.EventSpec string|{event?:string|string[], pattern?:string|string[]}|string[]
|
||||||
|
|
||||||
---@alias PluginOpts table|fun(self:LzPlugin, opts:table):table?
|
---@alias PluginOpts table|fun(self:lz.n.Plugin, opts:table):table?
|
||||||
|
|
||||||
---@class LzPluginHooks
|
---@class lz.n.PluginHooks
|
||||||
---@field beforeAll? fun(self:LzPlugin) Will be run before loading any plugins
|
---@field beforeAll? fun(self:lz.n.Plugin) Will be run before loading any plugins
|
||||||
---@field deactivate? fun(self:LzPlugin) Unload/Stop a plugin
|
---@field deactivate? fun(self:lz.n.Plugin) Unload/Stop a plugin
|
||||||
---@field after? fun(self:LzPlugin, opts:table)|true Will be executed when loading the plugin
|
---@field after? fun(self:lz.n.Plugin, opts:table)|true Will be executed when loading the plugin
|
||||||
---@field opts? PluginOpts
|
---@field opts? PluginOpts
|
||||||
|
|
||||||
---@class LzPluginHandlers
|
---@class lz.n.PluginHandlers
|
||||||
---@field event? LzEvent[]
|
---@field event? lz.n.Event[]
|
||||||
---@field keys? LzKeys[]
|
---@field keys? lz.n.Keys[]
|
||||||
---@field cmd? string[]
|
---@field cmd? string[]
|
||||||
|
|
||||||
---@class LzPluginSpecHandlers
|
---@class lz.n.PluginSpecHandlers
|
||||||
---@field event? string|LzEventSpec[]
|
---@field event? string|lz.n.EventSpec[]
|
||||||
---@field cmd? string[]|string
|
---@field cmd? string[]|string
|
||||||
---@field ft? string[]|string
|
---@field ft? string[]|string
|
||||||
---@field keys? string|string[]|LzKeysSpec[]
|
---@field keys? string|string[]|lz.n.KeysSpec[]
|
||||||
---@field module? false
|
---@field module? false
|
||||||
|
|
||||||
---@class LzKeysBase: vim.keymap.set.Opts
|
---@class lz.n.KeysBase: vim.keymap.set.Opts
|
||||||
---@field desc? string
|
---@field desc? string
|
||||||
---@field noremap? boolean
|
---@field noremap? boolean
|
||||||
---@field remap? boolean
|
---@field remap? boolean
|
||||||
@@ -51,12 +51,12 @@ error("Cannot import a meta module")
|
|||||||
---@field nowait? boolean
|
---@field nowait? boolean
|
||||||
---@field ft? string|string[]
|
---@field ft? string|string[]
|
||||||
|
|
||||||
---@class LzKeysSpec: LzKeysBase
|
---@class lz.n.KeysSpec: lz.n.KeysBase
|
||||||
---@field [1] string lhs
|
---@field [1] string lhs
|
||||||
---@field [2]? string|fun()|false rhs
|
---@field [2]? string|fun()|false rhs
|
||||||
---@field mode? string|string[]
|
---@field mode? string|string[]
|
||||||
|
|
||||||
---@class LzKeys: LzKeysBase
|
---@class lz.n.Keys: lz.n.KeysBase
|
||||||
---@field lhs string lhs
|
---@field lhs string lhs
|
||||||
---@field rhs? string|fun() rhs
|
---@field rhs? string|fun() rhs
|
||||||
---@field mode? string
|
---@field mode? string
|
||||||
@@ -64,13 +64,13 @@ error("Cannot import a meta module")
|
|||||||
---@field name string
|
---@field name string
|
||||||
|
|
||||||
---@package
|
---@package
|
||||||
---@class LzPlugin: LzPluginBase,LzPluginHandlers,LzPluginHooks
|
---@class lz.n.Plugin: lz.n.PluginBase,lz.n.PluginHandlers,lz.n.PluginHooks
|
||||||
|
|
||||||
---@class LzPluginSpec: LzPluginBase,LzPluginSpecHandlers,LzPluginHooks
|
---@class lz.n.PluginSpec: lz.n.PluginBase,lz.n.PluginSpecHandlers,lz.n.PluginHooks
|
||||||
|
|
||||||
---@alias LzSpec LzPluginSpec|LzSpecImport|LzSpec[]
|
---@alias lz.n.Spec lz.n.PluginSpec|lz.n.SpecImport|lz.n.Spec[]
|
||||||
|
|
||||||
---@class LzSpecImport
|
---@class lz.n.SpecImport
|
||||||
---@field import string spec module to import
|
---@field import string spec module to import
|
||||||
---@field enabled? boolean|(fun():boolean)
|
---@field enabled? boolean|(fun():boolean)
|
||||||
---@field cond? boolean|(fun():boolean)
|
---@field cond? boolean|(fun():boolean)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ local spy = require("luassert.spy")
|
|||||||
|
|
||||||
describe("handlers.cmd", function()
|
describe("handlers.cmd", function()
|
||||||
it("Command only loads plugin once and executes plugin command", function()
|
it("Command only loads plugin once and executes plugin command", function()
|
||||||
---@type LzPlugin
|
---@type lz.n.Plugin
|
||||||
local plugin = {
|
local plugin = {
|
||||||
name = "foo",
|
name = "foo",
|
||||||
cmd = { "Foo" },
|
cmd = { "Foo" },
|
||||||
@@ -40,7 +40,7 @@ describe("handlers.cmd", function()
|
|||||||
vim.api.nvim_create_user_command("Foo", function() end, {})
|
vim.api.nvim_create_user_command("Foo", function() end, {})
|
||||||
vim.api.nvim_create_user_command("Bar", function() end, {})
|
vim.api.nvim_create_user_command("Bar", function() end, {})
|
||||||
end
|
end
|
||||||
---@type LzPlugin
|
---@type lz.n.Plugin
|
||||||
local plugin = {
|
local plugin = {
|
||||||
name = "foo",
|
name = "foo",
|
||||||
cmd = commands,
|
cmd = commands,
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ describe("handlers.event", function()
|
|||||||
)
|
)
|
||||||
end)
|
end)
|
||||||
it("Event only loads plugin once", function()
|
it("Event only loads plugin once", function()
|
||||||
---@type LzPlugin
|
---@type lz.n.Plugin
|
||||||
local plugin = {
|
local plugin = {
|
||||||
name = "foo",
|
name = "foo",
|
||||||
event = { event.parse("BufEnter") },
|
event = { event.parse("BufEnter") },
|
||||||
@@ -56,9 +56,9 @@ describe("handlers.event", function()
|
|||||||
assert.spy(spy_load).called(1)
|
assert.spy(spy_load).called(1)
|
||||||
end)
|
end)
|
||||||
it("Multiple events only load plugin once", function()
|
it("Multiple events only load plugin once", function()
|
||||||
---@param events LzEvent[]
|
---@param events lz.n.Event[]
|
||||||
local function itt(events)
|
local function itt(events)
|
||||||
---@type LzPlugin
|
---@type lz.n.Plugin
|
||||||
local plugin = {
|
local plugin = {
|
||||||
name = "foo",
|
name = "foo",
|
||||||
event = events,
|
event = events,
|
||||||
@@ -78,7 +78,7 @@ describe("handlers.event", function()
|
|||||||
itt({ event.parse("WinEnter"), event.parse("BufEnter") })
|
itt({ event.parse("WinEnter"), event.parse("BufEnter") })
|
||||||
end)
|
end)
|
||||||
it("Plugins' event handlers are triggered", function()
|
it("Plugins' event handlers are triggered", function()
|
||||||
---@type LzPlugin
|
---@type lz.n.Plugin
|
||||||
local plugin = {
|
local plugin = {
|
||||||
name = "foo",
|
name = "foo",
|
||||||
event = { event.parse("BufEnter") },
|
event = { event.parse("BufEnter") },
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ describe("handlers.ft", function()
|
|||||||
}, ft.parse("rust"))
|
}, ft.parse("rust"))
|
||||||
end)
|
end)
|
||||||
it("filetype event loads plugins", function()
|
it("filetype event loads plugins", function()
|
||||||
---@type LzPlugin
|
---@type lz.n.Plugin
|
||||||
local plugin = {
|
local plugin = {
|
||||||
name = "Foo",
|
name = "Foo",
|
||||||
event = { ft.parse("rust") },
|
event = { ft.parse("rust") },
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ describe("handlers.keys", function()
|
|||||||
end)
|
end)
|
||||||
it("Key only loads plugin once", function()
|
it("Key only loads plugin once", function()
|
||||||
local lhs = "<leader>tt"
|
local lhs = "<leader>tt"
|
||||||
---@type LzPlugin
|
---@type lz.n.Plugin
|
||||||
local plugin = {
|
local plugin = {
|
||||||
name = "foo",
|
name = "foo",
|
||||||
keys = { keys.parse(lhs) },
|
keys = { keys.parse(lhs) },
|
||||||
@@ -36,9 +36,9 @@ describe("handlers.keys", function()
|
|||||||
--
|
--
|
||||||
end)
|
end)
|
||||||
it("Multiple keys only load plugin once", function()
|
it("Multiple keys only load plugin once", function()
|
||||||
---@param lzkeys LzKeys[]
|
---@param lzkeys lz.n.Keys[]
|
||||||
local function itt(lzkeys)
|
local function itt(lzkeys)
|
||||||
---@type LzPlugin
|
---@type lz.n.Plugin
|
||||||
local plugin = {
|
local plugin = {
|
||||||
name = "foo",
|
name = "foo",
|
||||||
keys = lzkeys,
|
keys = lzkeys,
|
||||||
@@ -57,7 +57,7 @@ describe("handlers.keys", function()
|
|||||||
end)
|
end)
|
||||||
it("Plugins' keymaps are triggered", function()
|
it("Plugins' keymaps are triggered", function()
|
||||||
local lhs = "<leader>xy"
|
local lhs = "<leader>xy"
|
||||||
---@type LzPlugin
|
---@type lz.n.Plugin
|
||||||
local plugin = {
|
local plugin = {
|
||||||
name = "baz",
|
name = "baz",
|
||||||
keys = { keys.parse(lhs) },
|
keys = { keys.parse(lhs) },
|
||||||
|
|||||||
Reference in New Issue
Block a user