From 5553dc52fa696f1e8162329a91e9055ff71020d6 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Mon, 17 Jun 2024 19:26:19 +0200 Subject: [PATCH] fix: actually support importing plugin specs from files --- lua/lz/n/spec.lua | 59 +++++++++++++++++++++++---------- spec/import_spec.lua | 77 ++++++++++++++++++++++++++++++++++++++++++++ spec/lz_n_spec.lua | 1 + 3 files changed, 120 insertions(+), 17 deletions(-) create mode 100644 spec/import_spec.lua diff --git a/lua/lz/n/spec.lua b/lua/lz/n/spec.lua index 355da30..8373165 100644 --- a/lua/lz/n/spec.lua +++ b/lua/lz/n/spec.lua @@ -1,5 +1,28 @@ local M = {} +---@param modname string +---@param result table +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 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 diff --git a/spec/import_spec.lua b/spec/import_spec.lua new file mode 100644 index 0000000..e68627d --- /dev/null +++ b/spec/import_spec.lua @@ -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) diff --git a/spec/lz_n_spec.lua b/spec/lz_n_spec.lua index afcd03b..985f7cc 100644 --- a/spec/lz_n_spec.lua +++ b/spec/lz_n_spec.lua @@ -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()