feat(plugin): allow plugin files only without a main plugin module. Fixes #53

This commit is contained in:
Folke Lemaitre 2022-12-21 09:03:40 +01:00
parent f5734f512f
commit 44f80a7f5d
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
4 changed files with 23 additions and 16 deletions

View File

@ -493,7 +493,7 @@ Example:
require("lazy").setup("plugins") require("lazy").setup("plugins")
``` ```
- `~/.config/nvim/lua/plugins.lua` or `~/.config/nvim/lua/plugins/init.lua` - `~/.config/nvim/lua/plugins.lua` or `~/.config/nvim/lua/plugins/init.lua` **_(this file is optional)_**
```lua ```lua
return { return {

View File

@ -220,8 +220,7 @@ function M.spec()
if type(Config.spec) == "string" then if type(Config.spec) == "string" then
-- spec is a module -- spec is a module
local function _load(name) local function _load(modname)
local modname = name and (Config.spec .. "." .. name) or Config.spec
-- unload the module so we get a clean slate -- unload the module so we get a clean slate
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
package.loaded[modname] = nil package.loaded[modname] = nil
@ -229,10 +228,7 @@ function M.spec()
spec:normalize(Cache.require(modname)) spec:normalize(Cache.require(modname))
end, "Failed to load **" .. modname .. "**") end, "Failed to load **" .. modname .. "**")
end end
local path_plugins = vim.fn.stdpath("config") .. "/lua/" .. Config.spec:gsub("%.", "/") Util.lsmod(Config.spec --[[@as string]], _load)
_load()
Util.lsmod(path_plugins, _load)
else else
-- spec is a spec -- spec is a spec
spec:normalize(vim.deepcopy(Config.spec)) spec:normalize(vim.deepcopy(Config.spec))

View File

@ -158,14 +158,28 @@ function M.walk(path, fn)
end) end)
end end
---@param modname string
---@param root string ---@param root string
---@param fn fun(modname:string, modpath:string) ---@param fn fun(modname:string, modpath:string)
function M.lsmod(root, fn) ---@overload fun(modname:string, fn: fun(modname:string, modpath:string))
function M.lsmod(modname, root, fn)
if type(root) == "function" then
fn = root
root = vim.fn.stdpath("config") .. "/lua"
end
root = root .. "/" .. modname:gsub("%.", "/")
if vim.loop.fs_stat(root .. ".lua") then
fn(modname, root .. ".lua")
end
M.ls(root, function(path, name, type) M.ls(root, function(path, name, type)
if type == "file" and name:sub(-4) == ".lua" and name ~= "init.lua" then if type == "file" and name:sub(-4) == ".lua" then
fn(name:sub(1, -5), path) if name == "init.lua" then
fn(modname, path)
else
fn(modname .. "." .. name:sub(1, -5), path)
end
elseif type == "directory" and vim.loop.fs_stat(path .. "/init.lua") then elseif type == "directory" and vim.loop.fs_stat(path .. "/init.lua") then
fn(name, path .. "/init.lua") fn(modname .. "." .. name, path .. "/init.lua")
end end
end) end)
end end

View File

@ -10,7 +10,6 @@ M.files = {}
---@type vim.loop.Timer ---@type vim.loop.Timer
M.timer = nil M.timer = nil
M.main = nil
M.root = nil M.root = nil
function M.enable() function M.enable()
@ -19,8 +18,7 @@ function M.enable()
end end
if type(Config.spec) == "string" then if type(Config.spec) == "string" then
M.timer = vim.loop.new_timer() M.timer = vim.loop.new_timer()
M.root = vim.fn.stdpath("config") .. "/lua/" .. Config.spec:gsub("%.", "/") M.root = vim.fn.stdpath("config") .. "/lua/"
M.main = vim.loop.fs_stat(M.root .. ".lua") and (M.root .. ".lua") or (M.root .. "/init.lua")
M.check(true) M.check(true)
M.timer:start(2000, 2000, M.check) M.timer:start(2000, 2000, M.check)
end end
@ -56,8 +54,7 @@ function M.check(start)
end end
end end
check(nil, M.main) Util.lsmod(Config.spec --[[@as string]], M.root, check)
Util.lsmod(M.root, check)
for file in pairs(M.files) do for file in pairs(M.files) do
if not checked[file] then if not checked[file] then