fix: actually support importing plugin specs from files

This commit is contained in:
Marc Jakobi
2024-06-17 19:26:19 +02:00
parent 2989fd3fe2
commit 5553dc52fa
3 changed files with 120 additions and 17 deletions
+42 -17
View File
@@ -1,5 +1,28 @@
local M = {}
---@param modname string
---@param result table<string, lz.n.Plugin>
local function import_modname(modname, result)
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 lz.n.SpecImport
---@param result table<string, lz.n.Plugin>
local function import_spec(spec, result)
@@ -21,25 +44,27 @@ local function import_spec(spec, result)
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
local modname = spec.import
local import_root = vim.api.nvim_get_runtime_file(vim.fs.joinpath("lua", modname .. ".lua"), true)
if #import_root == 1 then
import_modname(modname, result)
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
local import_dir = vim.api.nvim_get_runtime_file(vim.fs.joinpath("lua", modname), true)
if #import_dir == 1 then
local dir = import_dir[1]
local handle = vim.uv.fs_scandir(dir)
while handle do
local name, ty = vim.uv.fs_scandir_next(handle)
local path = vim.fs.joinpath(dir, name)
ty = ty or vim.uv.fs_stat(path).type
if not name then
break
elseif ty == "file" then
local submodname = vim.fn.fnamemodify(name, ":r")
import_modname(modname .. "." .. submodname, result)
end
end
end
M._normalize(mod, result)
end
---@param spec lz.n.PluginSpec
+77
View File
@@ -0,0 +1,77 @@
local lz = require("lz.n")
vim.g.lz_n = {
load = function() end,
}
local tempdir = vim.fn.tempname()
vim.system({ "rm", "-r", tempdir }):wait()
vim.system({ "mkdir", "-p", tempdir .. "/lua/plugins" }):wait()
local loader = require("lz.n.loader")
local spy = require("luassert.spy")
describe("lz.n", function()
describe("load", function()
it("import", function()
vim.g.lz_n_did_load = false
local plugin_config_content = [[
return {
"telescope.nvim",
cmd = "Telescope",
}
]]
local spec_file = vim.fs.joinpath(tempdir, "lua", "plugins", "telescope.lua")
local fh = assert(io.open(spec_file, "w"), "Could not open config file for writing")
fh:write(plugin_config_content)
fh:close()
vim.opt.runtimepath:append(tempdir)
local spy_load = spy.on(loader, "_load")
lz.load("plugins")
vim.cmd.Telescope()
assert.spy(spy_load).called(1)
vim.system({ "rm", spec_file }):wait()
end)
it("import root file", function()
vim.g.lz_n_did_load = false
local plugin_config_content = [[
return {
{ "sweetie.nvim" },
{ "telescope.nvim", cmd = "Telescope" },
}
]]
local spec_file = vim.fs.joinpath(tempdir, "lua", "plugins.lua")
local fh = assert(io.open(spec_file, "w"), "Could not open config file for writing")
fh:write(plugin_config_content)
fh:close()
vim.opt.runtimepath:append(tempdir)
local spy_load = spy.on(loader, "_load")
lz.load("plugins")
assert.spy(spy_load).called(1)
vim.cmd.Telescope()
assert.spy(spy_load).called(2)
vim.system({ "rm", spec_file }):wait()
end)
it("import plugin specs and spec file", function()
vim.g.lz_n_did_load = false
local plugin_config_content = [[
return {
"telescope.nvim",
cmd = "Telescope",
}
]]
local spec_file = vim.fs.joinpath(tempdir, "lua", "plugins", "telescope.lua")
local fh = assert(io.open(spec_file, "w"), "Could not open config file for writing")
fh:write(plugin_config_content)
fh:close()
vim.opt.runtimepath:append(tempdir)
local spy_load = spy.on(loader, "_load")
lz.load({
{ import = "plugins" },
{ "sweetie.nvim" },
})
assert.spy(spy_load).called(1)
vim.cmd.Telescope()
assert.spy(spy_load).called(2)
vim.system({ "rm", spec_file }):wait()
end)
end)
end)
+1
View File
@@ -4,6 +4,7 @@ vim.g.lz_n = {
}
local loader = require("lz.n.loader")
local spy = require("luassert.spy")
vim.g.lz_n_did_load = false
describe("lz.n", function()
describe("load", function()