feat: find local_spec in parent directories as well. Closes #1519

This commit is contained in:
Folke Lemaitre 2024-06-15 09:28:56 +02:00
parent 6944b105cb
commit e2e10d9cbe
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 35 additions and 9 deletions

View File

@ -403,7 +403,7 @@ function Spec:import(spec)
---@type string[] ---@type string[]
local modnames = {} local modnames = {}
if spec.import == M.LOCAL_SPEC then if spec.import:find(M.LOCAL_SPEC, 1, true) then
modnames = { spec.import } modnames = { spec.import }
else else
Util.lsmod(spec.import, function(modname) Util.lsmod(spec.import, function(modname)
@ -414,15 +414,19 @@ function Spec:import(spec)
for _, modname in ipairs(modnames) do for _, modname in ipairs(modnames) do
imported = imported + 1 imported = imported + 1
Util.track({ import = modname }) local name = modname
if modname:find(M.LOCAL_SPEC, 1, true) then
name = vim.fn.fnamemodify(modname, ":~:.")
end
Util.track({ import = name })
self.importing = modname self.importing = modname
-- 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
Util.try(function() Util.try(function()
local mod = nil local mod = nil
if modname == M.LOCAL_SPEC then if modname:find(M.LOCAL_SPEC, 1, true) then
mod = M.local_spec() mod = M.local_spec(modname)
else else
mod = require(modname) mod = require(modname)
end end
@ -546,26 +550,48 @@ function M.update_state()
end end
end end
function M.local_spec() ---@param path string
local filepath = vim.fn.fnamemodify(".lazy.lua", ":p") function M.local_spec(path)
local file = vim.secure.read(filepath) local file = vim.secure.read(path)
if file then if file then
return loadstring(file)() return loadstring(file)()
end end
return {} return {}
end end
---@return string?
function M.find_local_spec()
if not Config.options.local_spec then
return
end
local path = vim.uv.cwd()
while path ~= "" do
local file = path .. "/" .. M.LOCAL_SPEC
if vim.fn.filereadable(file) == 1 then
return file
end
local p = vim.fn.fnamemodify(path, ":h")
if p == path then
break
end
path = p
end
end
function M.load() function M.load()
M.loading = true M.loading = true
-- load specs -- load specs
Util.track("spec") Util.track("spec")
Config.spec = Spec.new() Config.spec = Spec.new()
local local_spec = M.find_local_spec()
Config.spec:parse({ Config.spec:parse({
vim.deepcopy(Config.options.spec), vim.deepcopy(Config.options.spec),
{ {
import = ".lazy.lua", import = local_spec or M.LOCAL_SPEC,
cond = function() cond = function()
return Config.options.local_spec and vim.fn.filereadable(M.LOCAL_SPEC) == 1 return local_spec ~= nil
end, end,
}, },
{ "folke/lazy.nvim" }, { "folke/lazy.nvim" },