style: run stylua with rocks.nvim config

This commit is contained in:
Marc Jakobi
2024-06-05 16:34:59 +02:00
parent a3313d1e7f
commit 9acc3d1053
12 changed files with 498 additions and 493 deletions

View File

@@ -1,6 +1,6 @@
column_width = 120
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 2
quote_style = "AutoPreferSingle"
call_parentheses = "NoSingleTable"
collapse_simple_statement = "Never"
indent_width = 4
quote_style = "AutoPreferDouble"
no_call_parentheses = false

View File

@@ -1,86 +1,86 @@
local loader = require('lz.n.loader')
local loader = require("lz.n.loader")
---@class LzCmdHandler: LzHandler
---@type LzCmdHandler
local M = {
pending = {},
type = 'cmd',
pending = {},
type = "cmd",
}
---@param cmd string
local function load(cmd)
vim.api.nvim_del_user_command(cmd)
loader.load(vim.tbl_values(M.pending[cmd]))
vim.api.nvim_del_user_command(cmd)
loader.load(vim.tbl_values(M.pending[cmd]))
end
---@param cmd string
local function add_cmd(cmd)
vim.api.nvim_create_user_command(cmd, function(event)
---@cast event vim.api.keyset.user_command
local command = {
cmd = cmd,
bang = event.bang or nil,
---@diagnostic disable-next-line: undefined-field
mods = event.smods,
---@diagnostic disable-next-line: undefined-field
args = event.fargs,
count = event.count >= 0 and event.range == 0 and event.count or nil,
}
vim.api.nvim_create_user_command(cmd, function(event)
---@cast event vim.api.keyset.user_command
local command = {
cmd = cmd,
bang = event.bang or nil,
---@diagnostic disable-next-line: undefined-field
mods = event.smods,
---@diagnostic disable-next-line: undefined-field
args = event.fargs,
count = event.count >= 0 and event.range == 0 and event.count or nil,
}
if event.range == 1 then
---@diagnostic disable-next-line: undefined-field
command.range = { event.line1 }
elseif event.range == 2 then
---@diagnostic disable-next-line: undefined-field
command.range = { event.line1, event.line2 }
end
if event.range == 1 then
---@diagnostic disable-next-line: undefined-field
command.range = { event.line1 }
elseif event.range == 2 then
---@diagnostic disable-next-line: undefined-field
command.range = { event.line1, event.line2 }
end
load(cmd)
load(cmd)
local info = vim.api.nvim_get_commands({})[cmd] or vim.api.nvim_buf_get_commands(0, {})[cmd]
if not info then
vim.schedule(function()
---@type string
local plugins = '`' .. table.concat(vim.tbl_values(M.pending[cmd]), ', ') .. '`'
vim.notify('Command `' .. cmd .. '` not found after loading ' .. plugins, vim.log.levels.ERROR)
end)
return
end
local info = vim.api.nvim_get_commands({})[cmd] or vim.api.nvim_buf_get_commands(0, {})[cmd]
if not info then
vim.schedule(function()
---@type string
local plugins = "`" .. table.concat(vim.tbl_values(M.pending[cmd]), ", ") .. "`"
vim.notify("Command `" .. cmd .. "` not found after loading " .. plugins, vim.log.levels.ERROR)
end)
return
end
command.nargs = info.nargs
---@diagnostic disable-next-line: undefined-field
if event.args and event.args ~= '' and info.nargs and info.nargs:find('[1?]') then
---@diagnostic disable-next-line: undefined-field
command.args = { event.args }
end
vim.cmd(command)
end, {
bang = true,
range = true,
nargs = '*',
complete = function(_, line)
load(cmd)
return vim.fn.getcompletion(line, 'cmdline')
end,
})
command.nargs = info.nargs
---@diagnostic disable-next-line: undefined-field
if event.args and event.args ~= "" and info.nargs and info.nargs:find("[1?]") then
---@diagnostic disable-next-line: undefined-field
command.args = { event.args }
end
vim.cmd(command)
end, {
bang = true,
range = true,
nargs = "*",
complete = function(_, line)
load(cmd)
return vim.fn.getcompletion(line, "cmdline")
end,
})
end
---@param cmd string
function M.del(cmd)
pcall(vim.api.nvim_del_user_command, cmd)
pcall(vim.api.nvim_del_user_command, cmd)
end
---@param plugin LzPlugin
function M.add(plugin)
if not plugin.cmd then
return
end
for _, cmd in pairs(plugin.cmd) do
M.pending[cmd] = M.pending[cmd] or {}
M.pending[cmd][plugin.name] = plugin.name
add_cmd(cmd)
end
if not plugin.cmd then
return
end
for _, cmd in pairs(plugin.cmd) do
M.pending[cmd] = M.pending[cmd] or {}
M.pending[cmd][plugin.name] = plugin.name
add_cmd(cmd)
end
end
return M

View File

@@ -1,4 +1,4 @@
local loader = require('lz.n.loader')
local loader = require("lz.n.loader")
---@class LzEventOpts
---@field event string
@@ -14,49 +14,51 @@ local loader = require('lz.n.loader')
---@type LzEventHandler
local M = {
pending = {},
events = {},
group = vim.api.nvim_create_augroup('lz_n_handler_event', { clear = true }),
type = 'event',
parse = function(spec)
local ret
if type(spec) == 'string' then
local event, pattern = spec:match('^(%w+)%s+(.*)$')
event = event or spec
return { id = spec, event = event, pattern = pattern }
elseif vim.islist(spec) then
ret = { id = table.concat(spec, '|'), event = spec }
else
ret = spec --[[@as LzEvent]]
if not ret.id then
---@diagnostic disable-next-line: assign-type-mismatch, param-type-mismatch
ret.id = type(ret.event) == 'string' and ret.event or table.concat(ret.event, '|')
if ret.pattern then
---@diagnostic disable-next-line: assign-type-mismatch, param-type-mismatch
ret.id = ret.id .. ' ' .. (type(ret.pattern) == 'string' and ret.pattern or table.concat(ret.pattern, ', '))
pending = {},
events = {},
group = vim.api.nvim_create_augroup("lz_n_handler_event", { clear = true }),
type = "event",
parse = function(spec)
local ret
if type(spec) == "string" then
local event, pattern = spec:match("^(%w+)%s+(.*)$")
event = event or spec
return { id = spec, event = event, pattern = pattern }
elseif vim.islist(spec) then
ret = { id = table.concat(spec, "|"), event = spec }
else
ret = spec --[[@as LzEvent]]
if not ret.id then
---@diagnostic disable-next-line: assign-type-mismatch, param-type-mismatch
ret.id = type(ret.event) == "string" and ret.event or table.concat(ret.event, "|")
if ret.pattern then
---@diagnostic disable-next-line: assign-type-mismatch, param-type-mismatch
ret.id = ret.id
.. " "
.. (type(ret.pattern) == "string" and ret.pattern or table.concat(ret.pattern, ", "))
end
end
end
end
end
return ret
end,
return ret
end,
}
-- Get all augroups for an event
---@param event 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)
---@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
end
return groups
return groups
end
local event_triggers = {
FileType = 'BufReadPost',
BufReadPost = 'BufReadPre',
FileType = "BufReadPost",
BufReadPost = "BufReadPre",
}
-- Get the current state of the event and all the events that will be fired
---@param event string
@@ -64,92 +66,92 @@ local event_triggers = {
---@param data unknown
---@return LzEventOpts[]
local function get_state(event, buf, data)
---@type LzEventOpts[]
local state = {}
while event do
---@type LzEventOpts
local event_opts = {
event = event,
exclude = event ~= 'FileType' and get_augroups(event) or nil,
buffer = buf,
data = data,
}
table.insert(state, 1, event_opts)
data = nil -- only pass the data to the first event
event = event_triggers[event]
end
return state
---@type LzEventOpts[]
local state = {}
while event do
---@type LzEventOpts
local event_opts = {
event = event,
exclude = event ~= "FileType" and get_augroups(event) or nil,
buffer = buf,
data = data,
}
table.insert(state, 1, event_opts)
data = nil -- only pass the data to the first event
event = event_triggers[event]
end
return state
end
-- Trigger an event
---@param opts LzEventOpts
local function _trigger(opts)
xpcall(
function()
vim.api.nvim_exec_autocmds(opts.event, {
buffer = opts.buffer,
group = opts.group,
modeline = false,
data = opts.data,
})
end,
vim.schedule_wrap(function(err)
vim.notify(err, vim.log.levels.ERROR)
end)
)
xpcall(
function()
vim.api.nvim_exec_autocmds(opts.event, {
buffer = opts.buffer,
group = opts.group,
modeline = false,
data = opts.data,
})
end,
vim.schedule_wrap(function(err)
vim.notify(err, vim.log.levels.ERROR)
end)
)
end
-- 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.
---@param opts LzEventOpts
local function trigger(opts)
if opts.group or opts.exclude == nil then
return _trigger(opts)
end
---@type table<string,true>
local done = {}
for _, autocmd in ipairs(vim.api.nvim_get_autocmds { event = opts.event }) do
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
if autocmd.group and not skip then
---@diagnostic disable-next-line: assign-type-mismatch
opts.group = autocmd.group_name
_trigger(opts)
if opts.group or opts.exclude == nil then
return _trigger(opts)
end
---@type table<string,true>
local done = {}
for _, autocmd in ipairs(vim.api.nvim_get_autocmds({ event = opts.event })) do
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
if autocmd.group and not skip then
---@diagnostic disable-next-line: assign-type-mismatch
opts.group = autocmd.group_name
_trigger(opts)
end
end
end
end
---@param event LzEvent
local function add_event(event)
local done = false
vim.api.nvim_create_autocmd(event.event, {
group = M.group,
once = true,
pattern = event.pattern,
callback = function(ev)
if done or not M.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])
-- check if any plugin created an event handler for this event and fire the group
for _, s in ipairs(state) do
trigger(s)
end
end,
})
local done = false
vim.api.nvim_create_autocmd(event.event, {
group = M.group,
once = true,
pattern = event.pattern,
callback = function(ev)
if done or not M.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])
-- check if any plugin created an event handler for this event and fire the group
for _, s in ipairs(state) do
trigger(s)
end
end,
})
end
---@param plugin LzPlugin
function M.add(plugin)
-- TODO add plugin to M.pending
for _, event in pairs(plugin.event or {}) do
add_event(event)
end
-- TODO add plugin to M.pending
for _, event in pairs(plugin.event or {}) do
add_event(event)
end
end
return M

View File

@@ -1,26 +1,26 @@
local event = require('lz.n.handler.event')
local event = require("lz.n.handler.event")
---@class LzFtHandler: LzHandler
---@field parse fun(spec: LzEventSpec): LzEvent
---@type LzFtHandler
local M = {
pending = {},
type = 'ft',
---@param value string
---@return LzEvent
parse = function(value)
return {
id = value,
event = 'FileType',
pattern = value,
}
end,
pending = {},
type = "ft",
---@param value string
---@return LzEvent
parse = function(value)
return {
id = value,
event = "FileType",
pattern = value,
}
end,
}
---@param plugin LzPlugin
function M.add(plugin)
event.add(plugin)
event.add(plugin)
end
return M

View File

@@ -8,47 +8,47 @@ local M = {}
---@enum LzHandlerTypes
M.types = {
cmd = 'cmd',
event = 'event',
ft = 'ft',
keys = 'keys',
cmd = "cmd",
event = "event",
ft = "ft",
keys = "keys",
}
local handlers = {
cmd = require('lz.n.handler.cmd'),
event = require('lz.n.handler.event'),
ft = require('lz.n.handler.ft'),
keys = require('lz.n.handler.keys'),
cmd = require("lz.n.handler.cmd"),
event = require("lz.n.handler.event"),
ft = require("lz.n.handler.ft"),
keys = require("lz.n.handler.keys"),
}
---@param plugin LzPlugin
local function enable(plugin)
for _, handler in pairs(handlers) do
handler.add(plugin)
end
-- TODO: Change handler add implementations to take a LzPlugin
for _, handler in pairs(handlers) do
handler.add(plugin)
end
-- TODO: Change handler add implementations to take a LzPlugin
end
function M.disable(plugin)
for _, handler in pairs(handlers) do
if type(handler.del) == 'function' then
-- TODO: Change handler del implementations to take a LzPlugin?
handler.del(plugin)
for _, handler in pairs(handlers) do
if type(handler.del) == "function" then
-- TODO: Change handler del implementations to take a LzPlugin?
handler.del(plugin)
end
end
end
end
---@param plugins table<string, LzPlugin>
function M.init(plugins)
for _, plugin in pairs(plugins) do
xpcall(
enable,
vim.schedule_wrap(function(err)
vim.notify(('Failed to enable handlers for %s: %s'):format(plugin.name, err), vim.log.levels.ERROR)
end),
plugin
)
end
for _, plugin in pairs(plugins) do
xpcall(
enable,
vim.schedule_wrap(function(err)
vim.notify(("Failed to enable handlers for %s: %s"):format(plugin.name, err), vim.log.levels.ERROR)
end),
plugin
)
end
end
return M

View File

@@ -1,32 +1,32 @@
local loader = require('lz.n.loader')
local loader = require("lz.n.loader")
---@class LzKeysHandler: LzHandler
---@type LzKeysHandler
local M = {
pending = {},
type = 'keys',
---@param value string|LzKeysSpec
---@param mode? string
---@return LzKeys
parse = function(value, mode)
value = type(value) == 'string' and { value } or value --[[@as LzKeysSpec]]
local ret = vim.deepcopy(value) --[[@as LzKeys]]
ret.lhs = ret[1] or ''
ret.rhs = ret[2]
ret[1] = nil
ret[2] = nil
ret.mode = mode or 'n'
ret.id = vim.api.nvim_replace_termcodes(ret.lhs, true, true, true)
if ret.ft then
local ft = type(ret.ft) == 'string' and { ret.ft } or ret.ft --[[@as string[] ]]
ret.id = ret.id .. ' (' .. table.concat(ft, ', ') .. ')'
end
if ret.mode ~= 'n' then
ret.id = ret.id .. ' (' .. ret.mode .. ')'
end
return ret
end,
pending = {},
type = "keys",
---@param value string|LzKeysSpec
---@param mode? string
---@return LzKeys
parse = function(value, mode)
value = type(value) == "string" and { value } or value --[[@as LzKeysSpec]]
local ret = vim.deepcopy(value) --[[@as LzKeys]]
ret.lhs = ret[1] or ""
ret.rhs = ret[2]
ret[1] = nil
ret[2] = nil
ret.mode = mode or "n"
ret.id = vim.api.nvim_replace_termcodes(ret.lhs, true, true, true)
if ret.ft then
local ft = type(ret.ft) == "string" and { ret.ft } or ret.ft --[[@as string[] ]]
ret.id = ret.id .. " (" .. table.concat(ft, ", ") .. ")"
end
if ret.mode ~= "n" then
ret.id = ret.id .. " (" .. ret.mode .. ")"
end
return ret
end,
}
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
@@ -34,102 +34,102 @@ local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
---@param keys LzKeys
---@return LzKeysBase
local function get_opts(keys)
---@type LzKeysBase
local opts = {}
for k, v in pairs(keys) do
if type(k) ~= 'number' and not skip[k] then
opts[k] = v
---@type LzKeysBase
local opts = {}
for k, v in pairs(keys) do
if type(k) ~= "number" and not skip[k] then
opts[k] = v
end
end
end
return opts
return opts
end
-- Create a mapping if it is managed by lz.n
---@param keys LzKeys
---@param buf integer?
local function set(keys, buf)
if keys.rhs then
local opts = get_opts(keys)
---@diagnostic disable-next-line: inject-field
opts.buffer = buf
vim.keymap.set(keys.mode, keys.lhs, keys.rhs, opts)
end
if keys.rhs then
local opts = get_opts(keys)
---@diagnostic disable-next-line: inject-field
opts.buffer = buf
vim.keymap.set(keys.mode, keys.lhs, keys.rhs, opts)
end
end
-- Delete a mapping and create the real global
-- mapping when needed
---@param keys LzKeys
local function del(keys)
pcall(vim.keymap.del, keys.mode, keys.lhs, {
-- NOTE: for buffer-local mappings, we only delete the mapping for the current buffer
-- So the mapping could still exist in other buffers
buffer = keys.ft and true or nil,
})
-- make sure to create global mappings when needed
-- buffer-local mappings are managed by lazy
if not keys.ft then
set(keys)
end
pcall(vim.keymap.del, keys.mode, keys.lhs, {
-- NOTE: for buffer-local mappings, we only delete the mapping for the current buffer
-- So the mapping could still exist in other buffers
buffer = keys.ft and true or nil,
})
-- make sure to create global mappings when needed
-- buffer-local mappings are managed by lazy
if not keys.ft then
set(keys)
end
end
---@param keys LzKeys
local function add_keys(keys)
local lhs = keys.lhs
local opts = get_opts(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.pending[keys.id]
-- always delete the mapping immediately to prevent recursive mappings
del(keys)
M.pending[keys.id] = nil
if plugins then
loader.load(plugins)
end
-- Create the real buffer-local mapping
if keys.ft then
set(keys, buf)
end
if keys.mode:sub(-1) == 'a' then
lhs = lhs .. '<C-]>'
end
local feed = vim.api.nvim_replace_termcodes('<Ignore>' .. lhs, true, true, true)
-- insert instead of append the lhs
vim.api.nvim_feedkeys(feed, 'i', false)
end, {
desc = opts.desc,
nowait = opts.nowait,
-- we do not return anything, but this is still needed to make operator pending mappings work
expr = true,
buffer = buf,
})
end
-- buffer-local mappings
if keys.ft then
vim.api.nvim_create_autocmd('FileType', {
pattern = keys.ft,
callback = function(event)
if M.pending[keys.id] then
add(event.buf)
else
-- Only create the mapping if its managed by lz.n
-- otherwise the plugin is supposed to manage it
set(keys, event.buf)
end
end,
})
else
add()
end
---@param buf? number
local function add(buf)
vim.keymap.set(keys.mode, lhs, function()
local plugins = M.pending[keys.id]
-- always delete the mapping immediately to prevent recursive mappings
del(keys)
M.pending[keys.id] = nil
if plugins then
loader.load(plugins)
end
-- Create the real buffer-local mapping
if keys.ft then
set(keys, buf)
end
if keys.mode:sub(-1) == "a" then
lhs = lhs .. "<C-]>"
end
local feed = vim.api.nvim_replace_termcodes("<Ignore>" .. lhs, true, true, true)
-- insert instead of append the lhs
vim.api.nvim_feedkeys(feed, "i", false)
end, {
desc = opts.desc,
nowait = opts.nowait,
-- we do not return anything, but this is still needed to make operator pending mappings work
expr = true,
buffer = buf,
})
end
-- buffer-local mappings
if keys.ft then
vim.api.nvim_create_autocmd("FileType", {
pattern = keys.ft,
callback = function(event)
if M.pending[keys.id] then
add(event.buf)
else
-- Only create the mapping if its managed by lz.n
-- otherwise the plugin is supposed to manage it
set(keys, event.buf)
end
end,
})
else
add()
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
-- TODO add plugin to M.pending
for _, key in pairs(plugin.keys or {}) do
add_keys(key)
end
end
return M

View File

@@ -3,25 +3,25 @@
local M = {}
-- TODO: Is this necessary?
if not vim.loader or vim.fn.has('nvim-0.9.1') ~= 1 then
error('lz.n requires Neovim >= 0.9.1')
if not vim.loader or vim.fn.has("nvim-0.9.1") ~= 1 then
error("lz.n requires Neovim >= 0.9.1")
end
---@param spec string | LzSpec
function M.load(spec)
if vim.g.lzn_did_load then
return vim.notify('lz.n has already loaded your plugins.', vim.log.levels.WARN, { title = 'lz.n' })
end
vim.g.lzn_did_load = true
if vim.g.lzn_did_load then
return vim.notify("lz.n has already loaded your plugins.", vim.log.levels.WARN, { title = "lz.n" })
end
vim.g.lzn_did_load = true
if type(spec) == 'string' then
spec = { import = spec }
end
---@cast spec LzSpec
local plugins = require('lz.n.spec').parse(spec)
require('lz.n.loader').load_startup_plugins(plugins)
require('lz.n.state').plugins = plugins
require('lz.n.handler').init(plugins)
if type(spec) == "string" then
spec = { import = spec }
end
---@cast spec LzSpec
local plugins = require("lz.n.spec").parse(spec)
require("lz.n.loader").load_startup_plugins(plugins)
require("lz.n.state").plugins = plugins
require("lz.n.handler").init(plugins)
end
return M

View File

@@ -1,6 +1,6 @@
---@mod lz.n.loader
local state = require('lz.n.state')
local state = require("lz.n.state")
local M = {}
@@ -9,77 +9,77 @@ local DEFAULT_PRIORITY = 50
---@package
---@param plugin LzPlugin
function M._load(plugin)
if plugin.enable == false or (type(plugin.enable) == 'function' and not plugin.enable()) then
return
end
require('lz.n.handler').disable(plugin)
-- TODO: Load plugin
if plugin.enable == false or (type(plugin.enable) == "function" and not plugin.enable()) then
return
end
require("lz.n.handler").disable(plugin)
-- TODO: Load plugin
end
---@param plugins table<string, LzPlugin>
local function run_before_all(plugins)
for _, plugin in pairs(plugins) do
if plugin.beforeAll then
xpcall(
plugin.beforeAll,
vim.schedule_wrap(function(err)
vim.notify(
"Failed to run 'beforeAll' for " .. plugin.name .. ': ' .. tostring(err or ''),
vim.log.levels.ERROR
)
end),
plugin
)
for _, plugin in pairs(plugins) do
if plugin.beforeAll then
xpcall(
plugin.beforeAll,
vim.schedule_wrap(function(err)
vim.notify(
"Failed to run 'beforeAll' for " .. plugin.name .. ": " .. tostring(err or ""),
vim.log.levels.ERROR
)
end),
plugin
)
end
end
end
end
---@param plugins table<string, LzPlugin>
---@return LzPlugin[]
local function get_eager_plugins(plugins)
local result = {}
for _, plugin in pairs(plugins) do
if plugin.lazy == false then
table.insert(result, plugin)
local result = {}
for _, plugin in pairs(plugins) do
if plugin.lazy == false then
table.insert(result, plugin)
end
end
end
table.sort(result, function(a, b)
---@cast a LzPlugin
---@cast b LzPlugin
return (a.priority or DEFAULT_PRIORITY) > (b.priority or DEFAULT_PRIORITY)
end)
return result
table.sort(result, function(a, b)
---@cast a LzPlugin
---@cast b LzPlugin
return (a.priority or DEFAULT_PRIORITY) > (b.priority or DEFAULT_PRIORITY)
end)
return result
end
--- Loads startup plugins, removing loaded plugins from the table
---@param plugins table<string, LzPlugin>
function M.load_startup_plugins(plugins)
run_before_all(plugins)
for _, plugin in pairs(get_eager_plugins(plugins)) do
M.load(plugin)
plugins[plugin.name] = nil
end
run_before_all(plugins)
for _, plugin in pairs(get_eager_plugins(plugins)) do
M.load(plugin)
plugins[plugin.name] = nil
end
end
---@param plugins string | LzPlugin | string[] | LzPlugin[]
function M.load(plugins)
plugins = (type(plugins) == 'string' or plugins.name) and { plugins } or plugins
---@cast plugins (string|LzPlugin)[]
for _, plugin in pairs(plugins) do
local loadable = true
if type(plugin) == 'string' then
if state.plugins[plugin] then
plugin = state.plugins[plugin]
else
vim.notify('Plugin ' .. plugin .. ' not found', vim.log.levels.ERROR, { title = 'lz.n' })
loadable = false
end
---@cast plugin LzPlugin
plugins = (type(plugins) == "string" or plugins.name) and { plugins } or plugins
---@cast plugins (string|LzPlugin)[]
for _, plugin in pairs(plugins) do
local loadable = true
if type(plugin) == "string" then
if state.plugins[plugin] then
plugin = state.plugins[plugin]
else
vim.notify("Plugin " .. plugin .. " not found", vim.log.levels.ERROR, { title = "lz.n" })
loadable = false
end
---@cast plugin LzPlugin
end
if loadable then
M._load(plugin)
end
end
if loadable then
M._load(plugin)
end
end
end
return M

View File

@@ -3,140 +3,143 @@ local M = {}
---@param spec LzSpecImport
---@param result table<string, LzPlugin>
local function import_spec(spec, result)
if spec.import == 'lz.n' then
vim.schedule(function()
vim.notify("Plugins modules cannot be called 'lz.n'", vim.log.levels.ERROR)
end)
return
end
if type(spec.import) ~= 'string' then
vim.schedule(function()
vim.notify(
"Invalid import spec. The 'import' field should be a module name: " .. vim.inspect(spec),
vim.log.levels.ERROR
)
end)
return
end
if spec.cond == false or (type(spec.cond) == 'function' and not spec.cond()) then
return
end
if spec.enabled == false or (type(spec.enabled) == 'function' and not spec.enabled()) then
return
end
local modname = 'plugin.' .. spec.import
local ok, mod = pcall(require, modname)
if not ok then
vim.schedule(function()
local err = type(mod) == 'string' and ': ' .. mod or ''
vim.notify("Failed to load module '" .. modname .. err, vim.log.levels.ERROR)
end)
return
end
if type(mod) ~= table then
vim.schedule(function()
vim.notify("Invalid plugin spec module '" .. modname .. "' of type '" .. type(mod) .. "'", vim.log.levels.ERROR)
end)
return
end
M._normalize(mod, result)
if spec.import == "lz.n" then
vim.schedule(function()
vim.notify("Plugins modules cannot be called 'lz.n'", vim.log.levels.ERROR)
end)
return
end
if type(spec.import) ~= "string" then
vim.schedule(function()
vim.notify(
"Invalid import spec. The 'import' field should be a module name: " .. vim.inspect(spec),
vim.log.levels.ERROR
)
end)
return
end
if spec.cond == false or (type(spec.cond) == "function" and not spec.cond()) then
return
end
if spec.enabled == false or (type(spec.enabled) == "function" and not spec.enabled()) then
return
end
local modname = "plugin." .. spec.import
local ok, mod = pcall(require, modname)
if not ok then
vim.schedule(function()
local err = type(mod) == "string" and ": " .. mod or ""
vim.notify("Failed to load module '" .. modname .. err, vim.log.levels.ERROR)
end)
return
end
if type(mod) ~= table then
vim.schedule(function()
vim.notify(
"Invalid plugin spec module '" .. modname .. "' of type '" .. type(mod) .. "'",
vim.log.levels.ERROR
)
end)
return
end
M._normalize(mod, result)
end
---@param spec LzPluginSpec
---@return LzPlugin
local function parse(spec)
---@type LzPlugin
---@diagnostic disable-next-line: assign-type-mismatch
local result = vim.deepcopy(spec)
local event_spec = spec.event
if event_spec then
result.event = {}
end
if type(event_spec) == 'string' then
local event = require('lz.n.handler.event').parse(event_spec)
result.event[event.id] = event
elseif type(event_spec) == 'table' then
---@cast event_spec LzEventSpec[]
for _, _event_spec in pairs(event_spec) do
local event = require('lz.n.handler.event').parse(_event_spec)
result.ft[event.id] = event
---@type LzPlugin
---@diagnostic disable-next-line: assign-type-mismatch
local result = vim.deepcopy(spec)
local event_spec = spec.event
if event_spec then
result.event = {}
end
end
local ft_spec = spec.ft
if ft_spec then
result.ft = {}
end
if type(ft_spec) == 'string' then
local ft = require('lz.n.handler.ft').parse(ft_spec)
result[ft.id] = 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)
result.ft[ft.id] = ft
if type(event_spec) == "string" then
local event = require("lz.n.handler.event").parse(event_spec)
result.event[event.id] = event
elseif type(event_spec) == "table" then
---@cast event_spec LzEventSpec[]
for _, _event_spec in pairs(event_spec) do
local event = require("lz.n.handler.event").parse(_event_spec)
result.ft[event.id] = event
end
end
end
local keys_spec = spec.keys
if keys_spec then
result.keys = {}
end
if type(keys_spec) == 'string' then
local keys = require('lz.n.handler.keys').parse(keys_spec)
result.keys[keys.id] = keys
elseif type(keys_spec) == 'table' then
---@cast keys_spec string[] | LzKeysSpec[]
for _, _keys_spec in pairs(keys_spec) do
local keys = require('lz.n.handler.keys').parse(_keys_spec)
result.keys[keys.id] = keys
local ft_spec = spec.ft
if ft_spec then
result.ft = {}
end
end
local cmd_spec = spec.cmd
if cmd_spec then
result.cmd = {}
end
if type(cmd_spec) == 'string' then
result.cmd[cmd_spec] = cmd_spec
elseif type(cmd_spec) == 'table' then
for _, _cmd_spec in pairs(cmd_spec) do
result.cmd[_cmd_spec] = _cmd_spec
if type(ft_spec) == "string" then
local ft = require("lz.n.handler.ft").parse(ft_spec)
result[ft.id] = 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)
result.ft[ft.id] = ft
end
end
end
return result
local keys_spec = spec.keys
if keys_spec then
result.keys = {}
end
if type(keys_spec) == "string" then
local keys = require("lz.n.handler.keys").parse(keys_spec)
result.keys[keys.id] = keys
elseif type(keys_spec) == "table" then
---@cast keys_spec string[] | LzKeysSpec[]
for _, _keys_spec in pairs(keys_spec) do
local keys = require("lz.n.handler.keys").parse(_keys_spec)
result.keys[keys.id] = keys
end
end
local cmd_spec = spec.cmd
if cmd_spec then
result.cmd = {}
end
if type(cmd_spec) == "string" then
result.cmd[cmd_spec] = cmd_spec
elseif type(cmd_spec) == "table" then
for _, _cmd_spec in pairs(cmd_spec) do
result.cmd[_cmd_spec] = _cmd_spec
end
end
return result
end
---@private
---@param spec LzSpec
---@param result table<string, LzPlugin>
function M._normalize(spec, result)
if #spec > 1 or vim.islist(spec) then
for _, sp in ipairs(spec) do
M._normalize(sp, result)
if #spec > 1 or vim.islist(spec) then
for _, sp in ipairs(spec) do
M._normalize(sp, result)
end
elseif spec.name then
---@cast spec LzPluginSpec
result[spec.name] = parse(spec)
elseif spec.import then
---@cast spec LzSpecImport
import_spec(spec, result)
end
elseif spec.name then
---@cast spec LzPluginSpec
result[spec.name] = parse(spec)
elseif spec.import then
---@cast spec LzSpecImport
import_spec(spec, result)
end
end
---@param result table<string, LzPlugin>
local function remove_disabled_plugins(result)
for _, plugin in ipairs(result) do
local disabled = plugin.enabled == false or (type(plugin.enabled) == 'function' and not plugin.enabled())
if disabled then
result[plugin.name] = nil
for _, plugin in ipairs(result) do
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 LzSpec
---@return table<string, LzPlugin>
function M.parse(spec)
local result = {}
M._normalize(spec, result)
remove_disabled_plugins(result)
return result
local result = {}
M._normalize(spec, result)
remove_disabled_plugins(result)
return result
end
return M

View File

@@ -1,5 +1,5 @@
---@meta
error('Cannot import a meta module')
error("Cannot import a meta module")
---@class VimGTable vim.g config table
---@field name? string Name of the vim.g config table, e.g. "rustaceanvim" for "vim.g.rustaceanvim". Defaults to the plugin name.

View File

@@ -1,5 +1,5 @@
describe('Test example', function()
it('Test can access vim namespace', function()
assert.are.same(vim.trim(' a '), 'a')
end)
describe("Test example", function()
it("Test can access vim namespace", function()
assert.are.same(vim.trim(" a "), "a")
end)
end)

View File

@@ -1,18 +1,18 @@
local keys = require('lz.n.handler.keys')
local keys = require("lz.n.handler.keys")
describe('keys', function()
it('parses ids correctly', function()
local tests = {
{ '<C-/>', '<c-/>', true },
{ '<C-h>', '<c-H>', true },
{ '<C-h>k', '<c-H>K', false },
}
for _, test in ipairs(tests) do
if test[3] then
assert.same(keys.parse(test[1]).id, keys.parse(test[2]).id)
else
assert.is_not.same(keys.parse(test[1]).id, keys.parse(test[2]).id)
end
end
end)
describe("keys", function()
it("parses ids correctly", function()
local tests = {
{ "<C-/>", "<c-/>", true },
{ "<C-h>", "<c-H>", true },
{ "<C-h>k", "<c-H>K", false },
}
for _, test in ipairs(tests) do
if test[3] then
assert.same(keys.parse(test[1]).id, keys.parse(test[2]).id)
else
assert.is_not.same(keys.parse(test[1]).id, keys.parse(test[2]).id)
end
end
end)
end)