mirror of
https://github.com/zoriya/lz.n.git
synced 2026-06-05 03:31:17 +00:00
reactor: use vim.iter
This commit is contained in:
@@ -69,9 +69,9 @@ end
|
||||
---@param plugin lz.n.Plugin
|
||||
function M.del(plugin)
|
||||
pcall(vim.api.nvim_del_user_command, plugin.cmd)
|
||||
for _, plugins in pairs(M.pending) do
|
||||
vim.iter(M.pending):each(function(_, plugins)
|
||||
plugins[plugin.name] = nil
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---@param plugin lz.n.Plugin
|
||||
@@ -79,11 +79,12 @@ function M.add(plugin)
|
||||
if not plugin.cmd then
|
||||
return
|
||||
end
|
||||
for _, cmd in pairs(plugin.cmd) do
|
||||
---@param cmd string
|
||||
vim.iter(plugin.cmd):each(function(cmd)
|
||||
M.pending[cmd] = M.pending[cmd] or {}
|
||||
M.pending[cmd][plugin.name] = plugin.name
|
||||
add_cmd(cmd)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -12,9 +12,9 @@ local M = {
|
||||
|
||||
---@param plugin lz.n.Plugin
|
||||
function M.del(plugin)
|
||||
for _, plugins in pairs(M.pending) do
|
||||
vim.iter(M.pending):each(function(_, plugins)
|
||||
plugins[plugin.name] = nil
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---@param name string
|
||||
@@ -44,10 +44,11 @@ function M.add(plugin)
|
||||
return
|
||||
end
|
||||
init()
|
||||
for _, colorscheme in pairs(plugin.colorscheme) do
|
||||
---@param colorscheme string
|
||||
vim.iter(plugin.colorscheme):each(function(colorscheme)
|
||||
M.pending[colorscheme] = M.pending[colorscheme] or {}
|
||||
M.pending[colorscheme][plugin.name] = plugin.name
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
+19
-16
@@ -58,15 +58,16 @@ local M = {
|
||||
|
||||
-- Get all augroups for an event
|
||||
---@param event string
|
||||
---@return string[]
|
||||
local function get_augroups(event)
|
||||
---@type string[]
|
||||
local groups = {}
|
||||
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = event })) do
|
||||
if autocmd.group_name then
|
||||
table.insert(groups, autocmd.group_name)
|
||||
end
|
||||
end
|
||||
return groups
|
||||
return vim.iter(vim.api.nvim_get_autocmds({ event = event }))
|
||||
:filter(function(autocmd)
|
||||
return autocmd.group_name ~= nil
|
||||
end)
|
||||
:map(function(autocmd)
|
||||
return autocmd.group_name
|
||||
end)
|
||||
:totable()
|
||||
end
|
||||
|
||||
local event_triggers = {
|
||||
@@ -123,7 +124,7 @@ local function trigger(opts)
|
||||
end
|
||||
---@type table<string,true>
|
||||
local done = {}
|
||||
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = opts.event })) do
|
||||
vim.iter(vim.api.nvim_get_autocmds({ event = opts.event })):each(function(autocmd)
|
||||
local id = autocmd.event .. ":" .. (autocmd.group or "") ---@type string
|
||||
local skip = done[id] or (opts.exclude and vim.tbl_contains(opts.exclude, autocmd.group_name))
|
||||
done[id] = true
|
||||
@@ -132,7 +133,7 @@ local function trigger(opts)
|
||||
opts.group = autocmd.group_name
|
||||
_trigger(opts)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---@param event lz.n.Event
|
||||
@@ -152,27 +153,29 @@ local function add_event(event)
|
||||
-- load the plugins
|
||||
loader.load(M.pending[event.id])
|
||||
-- check if any plugin created an event handler for this event and fire the group
|
||||
for _, s in ipairs(state) do
|
||||
---@param s lz.n.EventOpts
|
||||
vim.iter(state):each(function(s)
|
||||
trigger(s)
|
||||
end
|
||||
end)
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
---@param plugin lz.n.Plugin
|
||||
function M.add(plugin)
|
||||
for _, event in pairs(plugin.event or {}) do
|
||||
---@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.name
|
||||
add_event(event)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---@param plugin lz.n.Plugin
|
||||
function M.del(plugin)
|
||||
for _, plugins in pairs(M.pending) do
|
||||
vim.iter(M.pending):each(function(_, plugins)
|
||||
plugins[plugin.name] = nil
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -35,22 +35,25 @@ end
|
||||
|
||||
---@param plugin lz.n.Plugin
|
||||
local function enable(plugin)
|
||||
for _, handler in pairs(handlers) do
|
||||
---@param handler lz.n.Handler
|
||||
vim.iter(handlers):each(function(_, handler)
|
||||
handler.add(plugin)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function M.disable(plugin)
|
||||
for _, handler in pairs(handlers) do
|
||||
---@param handler lz.n.Handler
|
||||
vim.iter(handlers):each(function(_, handler)
|
||||
if handler.del then
|
||||
handler.del(plugin)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---@param plugins table<string, lz.n.Plugin>
|
||||
function M.init(plugins)
|
||||
for _, plugin in pairs(plugins) do
|
||||
---@param plugin lz.n.Plugin
|
||||
vim.iter(plugins):each(function(_, plugin)
|
||||
xpcall(
|
||||
enable,
|
||||
vim.schedule_wrap(function(err)
|
||||
@@ -58,7 +61,7 @@ function M.init(plugins)
|
||||
end),
|
||||
plugin
|
||||
)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -35,13 +35,12 @@ local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
|
||||
---@return lz.n.KeysBase
|
||||
local function get_opts(keys)
|
||||
---@type lz.n.KeysBase
|
||||
local opts = {}
|
||||
for k, v in pairs(keys) do
|
||||
return vim.iter(keys):fold({}, function(acc, k, v)
|
||||
if type(k) ~= "number" and not skip[k] then
|
||||
opts[k] = v
|
||||
acc[k] = v
|
||||
end
|
||||
end
|
||||
return opts
|
||||
return acc
|
||||
end)
|
||||
end
|
||||
|
||||
-- Create a mapping if it is managed by lz.n
|
||||
@@ -126,18 +125,19 @@ end
|
||||
|
||||
---@param plugin lz.n.Plugin
|
||||
function M.add(plugin)
|
||||
for _, key in pairs(plugin.keys or {}) do
|
||||
---@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.name
|
||||
add_keys(key)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---@param plugin lz.n.Plugin
|
||||
function M.del(plugin)
|
||||
for _, plugins in pairs(M.pending) do
|
||||
vim.iter(M.pending):each(function(_, plugins)
|
||||
plugins[plugin.name] = nil
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
+20
-13
@@ -26,7 +26,8 @@ end
|
||||
|
||||
---@param plugins table<string, lz.n.Plugin>
|
||||
local function run_before_all(plugins)
|
||||
for _, plugin in pairs(plugins) do
|
||||
---@param plugin lz.n.Plugin
|
||||
vim.iter(plugins):each(function(plugin)
|
||||
if plugin.beforeAll then
|
||||
xpcall(
|
||||
plugin.beforeAll,
|
||||
@@ -39,7 +40,7 @@ local function run_before_all(plugins)
|
||||
plugin
|
||||
)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---@param plugin lz.n.Plugin
|
||||
@@ -50,12 +51,17 @@ end
|
||||
---@param plugins table<string, lz.n.Plugin>
|
||||
---@return lz.n.Plugin[]
|
||||
local function get_eager_plugins(plugins)
|
||||
local result = {}
|
||||
for _, plugin in pairs(plugins) do
|
||||
if plugin.lazy == false then
|
||||
table.insert(result, plugin)
|
||||
end
|
||||
end
|
||||
---@type lz.n.Plugin[]
|
||||
local result = vim
|
||||
.iter(plugins)
|
||||
---@param plugin lz.n.Plugin
|
||||
:filter(function(_, plugin)
|
||||
return plugin.lazy ~= true
|
||||
end)
|
||||
:fold({}, function(acc, _, v)
|
||||
table.insert(acc, v)
|
||||
return acc
|
||||
end)
|
||||
table.sort(result, function(a, b)
|
||||
---@cast a lz.n.Plugin
|
||||
---@cast b lz.n.Plugin
|
||||
@@ -68,10 +74,11 @@ end
|
||||
---@param plugins table<string, lz.n.Plugin>
|
||||
function M.load_startup_plugins(plugins)
|
||||
run_before_all(plugins)
|
||||
for _, plugin in pairs(get_eager_plugins(plugins)) do
|
||||
---@param plugin lz.n.Plugin
|
||||
vim.iter(get_eager_plugins(plugins)):each(function(plugin)
|
||||
M.load(plugin)
|
||||
plugins[plugin.name] = nil
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---@alias hook_key "before" | "after"
|
||||
@@ -96,8 +103,8 @@ end
|
||||
---@param plugins string | lz.n.Plugin | string[] | lz.n.Plugin[]
|
||||
function M.load(plugins)
|
||||
plugins = (type(plugins) == "string" or plugins.name) and { plugins } or plugins
|
||||
---@cast plugins (string|lz.n.Plugin)[]
|
||||
for _, plugin in pairs(plugins) do
|
||||
---@param plugin string|lz.n.Plugin
|
||||
vim.iter(plugins):each(function(plugin)
|
||||
local loadable = true
|
||||
if type(plugin) == "string" then
|
||||
if state.plugins[plugin] then
|
||||
@@ -113,7 +120,7 @@ function M.load(plugins)
|
||||
M._load(plugin)
|
||||
hook("after", plugin)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
+26
-22
@@ -87,11 +87,11 @@ local function parse(spec)
|
||||
local event = require("lz.n.handler.event").parse(event_spec)
|
||||
table.insert(result.event, event)
|
||||
elseif type(event_spec) == "table" then
|
||||
---@cast event_spec lz.n.EventSpec[]
|
||||
for _, _event_spec in pairs(event_spec) do
|
||||
local event = require("lz.n.handler.event").parse(_event_spec)
|
||||
---@param ev lz.n.EventSpec[]
|
||||
vim.iter(event_spec):each(function(ev)
|
||||
local event = require("lz.n.handler.event").parse(ev)
|
||||
table.insert(result.event, event)
|
||||
end
|
||||
end)
|
||||
end
|
||||
local ft_spec = spec.ft
|
||||
if ft_spec then
|
||||
@@ -103,10 +103,11 @@ local function parse(spec)
|
||||
local ft = require("lz.n.handler.ft").parse(ft_spec)
|
||||
table.insert(result.event, ft)
|
||||
elseif type(ft_spec) == "table" then
|
||||
for _, _ft_spec in pairs(ft_spec) do
|
||||
local ft = require("lz.n.handler.ft").parse(_ft_spec)
|
||||
---@param ft_spec_ string
|
||||
vim.iter(ft_spec):each(function(ft_spec_)
|
||||
local ft = require("lz.n.handler.ft").parse(ft_spec_)
|
||||
table.insert(result.event, ft)
|
||||
end
|
||||
end)
|
||||
end
|
||||
local keys_spec = spec.keys
|
||||
if keys_spec then
|
||||
@@ -116,11 +117,11 @@ local function parse(spec)
|
||||
local keys = require("lz.n.handler.keys").parse(keys_spec)
|
||||
table.insert(result.keys, keys)
|
||||
elseif type(keys_spec) == "table" then
|
||||
---@cast keys_spec string[] | lz.n.KeysSpec[]
|
||||
for _, _keys_spec in pairs(keys_spec) do
|
||||
local keys = require("lz.n.handler.keys").parse(_keys_spec)
|
||||
---@param keys_spec_ string | lz.n.KeysSpec
|
||||
vim.iter(keys_spec):each(function(keys_spec_)
|
||||
local keys = require("lz.n.handler.keys").parse(keys_spec_)
|
||||
table.insert(result.keys, keys)
|
||||
end
|
||||
end)
|
||||
end
|
||||
local cmd_spec = spec.cmd
|
||||
if cmd_spec then
|
||||
@@ -129,9 +130,10 @@ local function parse(spec)
|
||||
if type(cmd_spec) == "string" then
|
||||
table.insert(result.cmd, cmd_spec)
|
||||
elseif type(cmd_spec) == "table" then
|
||||
for _, _cmd_spec in pairs(cmd_spec) do
|
||||
table.insert(result.cmd, _cmd_spec)
|
||||
end
|
||||
---@param cmd_spec_ string
|
||||
vim.iter(cmd_spec):each(function(cmd_spec_)
|
||||
table.insert(result.cmd, cmd_spec_)
|
||||
end)
|
||||
end
|
||||
local colorscheme_spec = spec.colorscheme
|
||||
if colorscheme_spec then
|
||||
@@ -140,9 +142,10 @@ local function parse(spec)
|
||||
if type(colorscheme_spec) == "string" then
|
||||
table.insert(result.colorscheme, colorscheme_spec)
|
||||
elseif type(colorscheme_spec) == "table" then
|
||||
for _, _colorscheme_spec in pairs(colorscheme_spec) do
|
||||
table.insert(result.colorscheme, _colorscheme_spec)
|
||||
end
|
||||
---@param colorscheme_spec_ string
|
||||
vim.iter(colorscheme_spec):each(function(colorscheme_spec_)
|
||||
table.insert(result.colorscheme, colorscheme_spec_)
|
||||
end)
|
||||
end
|
||||
result.lazy = require("lz.n.handler").is_lazy(spec)
|
||||
return result
|
||||
@@ -165,10 +168,10 @@ end
|
||||
---@param result table<string, lz.n.Plugin>
|
||||
function M._normalize(spec, result)
|
||||
if M.is_spec_list(spec) then
|
||||
---@cast spec lz.n.Spec[]
|
||||
for _, sp in ipairs(spec) do
|
||||
---@param sp lz.n.Spec
|
||||
vim.iter(spec):each(function(sp)
|
||||
M._normalize(sp, result)
|
||||
end
|
||||
end)
|
||||
elseif M.is_single_plugin_spec(spec) then
|
||||
---@cast spec lz.n.PluginSpec
|
||||
result[spec[1]] = parse(spec)
|
||||
@@ -180,12 +183,13 @@ end
|
||||
|
||||
---@param result table<string, lz.n.Plugin>
|
||||
local function remove_disabled_plugins(result)
|
||||
for _, plugin in ipairs(result) do
|
||||
---@param plugin lz.n.Plugin
|
||||
vim.iter(result):each(function(_, plugin)
|
||||
local disabled = plugin.enabled == false or (type(plugin.enabled) == "function" and not plugin.enabled())
|
||||
if disabled then
|
||||
result[plugin.name] = nil
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---@param spec lz.n.Spec
|
||||
|
||||
Reference in New Issue
Block a user