From 44f80a7f5d80a56dbfcc5cda34cc805a78ac7189 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Wed, 21 Dec 2022 09:03:40 +0100 Subject: [PATCH] feat(plugin): allow plugin files only without a main plugin module. Fixes #53 --- README.md | 2 +- lua/lazy/core/plugin.lua | 8 ++------ lua/lazy/core/util.lua | 22 ++++++++++++++++++---- lua/lazy/manage/reloader.lua | 7 ++----- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index ad24d68..c271afc 100644 --- a/README.md +++ b/README.md @@ -493,7 +493,7 @@ Example: 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 return { diff --git a/lua/lazy/core/plugin.lua b/lua/lazy/core/plugin.lua index f510ee8..1a6d439 100644 --- a/lua/lazy/core/plugin.lua +++ b/lua/lazy/core/plugin.lua @@ -220,8 +220,7 @@ function M.spec() if type(Config.spec) == "string" then -- spec is a module - local function _load(name) - local modname = name and (Config.spec .. "." .. name) or Config.spec + local function _load(modname) -- unload the module so we get a clean slate ---@diagnostic disable-next-line: no-unknown package.loaded[modname] = nil @@ -229,10 +228,7 @@ function M.spec() spec:normalize(Cache.require(modname)) end, "Failed to load **" .. modname .. "**") end - local path_plugins = vim.fn.stdpath("config") .. "/lua/" .. Config.spec:gsub("%.", "/") - - _load() - Util.lsmod(path_plugins, _load) + Util.lsmod(Config.spec --[[@as string]], _load) else -- spec is a spec spec:normalize(vim.deepcopy(Config.spec)) diff --git a/lua/lazy/core/util.lua b/lua/lazy/core/util.lua index 4475d60..f63807b 100644 --- a/lua/lazy/core/util.lua +++ b/lua/lazy/core/util.lua @@ -158,14 +158,28 @@ function M.walk(path, fn) end) end +---@param modname string ---@param root 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) - if type == "file" and name:sub(-4) == ".lua" and name ~= "init.lua" then - fn(name:sub(1, -5), path) + if type == "file" and name:sub(-4) == ".lua" then + 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 - fn(name, path .. "/init.lua") + fn(modname .. "." .. name, path .. "/init.lua") end end) end diff --git a/lua/lazy/manage/reloader.lua b/lua/lazy/manage/reloader.lua index 3b0af3c..b3b7eeb 100644 --- a/lua/lazy/manage/reloader.lua +++ b/lua/lazy/manage/reloader.lua @@ -10,7 +10,6 @@ M.files = {} ---@type vim.loop.Timer M.timer = nil -M.main = nil M.root = nil function M.enable() @@ -19,8 +18,7 @@ function M.enable() end if type(Config.spec) == "string" then M.timer = vim.loop.new_timer() - M.root = vim.fn.stdpath("config") .. "/lua/" .. Config.spec:gsub("%.", "/") - M.main = vim.loop.fs_stat(M.root .. ".lua") and (M.root .. ".lua") or (M.root .. "/init.lua") + M.root = vim.fn.stdpath("config") .. "/lua/" M.check(true) M.timer:start(2000, 2000, M.check) end @@ -56,8 +54,7 @@ function M.check(start) end end - check(nil, M.main) - Util.lsmod(M.root, check) + Util.lsmod(Config.spec --[[@as string]], M.root, check) for file in pairs(M.files) do if not checked[file] then