mirror of
https://github.com/folke/lazy.nvim.git
synced 2025-04-18 20:36:45 +00:00
feat(spec): config
can be true
or a table
that will be passed to require("plugin").setup(config)
This commit is contained in:
parent
9e983898b1
commit
2a7b0047dd
4 changed files with 69 additions and 2 deletions
|
@ -142,7 +142,7 @@ function M.load(plugins, reason)
|
|||
|
||||
M.packadd(plugin.dir)
|
||||
if plugin.config then
|
||||
Util.try(plugin.config, "Failed to run `config` for " .. plugin.name)
|
||||
M.config(plugin)
|
||||
end
|
||||
|
||||
plugin._.loaded.time = Util.track().time
|
||||
|
@ -154,6 +154,40 @@ function M.load(plugins, reason)
|
|||
end
|
||||
end
|
||||
|
||||
--- runs plugin config
|
||||
---@param plugin LazyPlugin
|
||||
function M.config(plugin)
|
||||
local fn
|
||||
if type(plugin.config) == "function" then
|
||||
fn = plugin.config
|
||||
else
|
||||
local normname = Util.normname(plugin.name)
|
||||
---@type table<string, string>
|
||||
local mods = {}
|
||||
Util.ls(plugin.dir .. "/lua", function(_, modname)
|
||||
modname = modname:gsub("%.lua$", "")
|
||||
mods[modname] = modname
|
||||
local modnorm = Util.normname(modname)
|
||||
-- if we found an exact match, then use that
|
||||
if modnorm == normname then
|
||||
mods = { modname }
|
||||
return false
|
||||
end
|
||||
end)
|
||||
mods = vim.tbl_values(mods)
|
||||
if #mods == 1 then
|
||||
fn = function()
|
||||
require(mods[1]).setup(plugin.config == true and {} or plugin.config)
|
||||
end
|
||||
else
|
||||
return Util.error(
|
||||
"Lua module not found for config of " .. plugin.name .. ". Please use a `config()` function instead"
|
||||
)
|
||||
end
|
||||
end
|
||||
Util.try(fn, "Failed to run `config` for " .. plugin.name)
|
||||
end
|
||||
|
||||
---@param path string
|
||||
function M.packadd(path)
|
||||
M.source_runtime(path, "plugin")
|
||||
|
|
|
@ -27,6 +27,11 @@ function M.track(data, time)
|
|||
end
|
||||
end
|
||||
|
||||
---@param name string
|
||||
function M.normname(name)
|
||||
return name:lower():gsub("^n?vim%-", ""):gsub("%.n?vim$", ""):gsub("%.lua", ""):gsub("[^a-z]+", "")
|
||||
end
|
||||
|
||||
function M.norm(path)
|
||||
if path:sub(1, 1) == "~" then
|
||||
local home = vim.loop.os_homedir()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue