Skip to content

Commit

Permalink
perf(plugin): minor optim to resolve imports a bit faster
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jul 1, 2024
1 parent 1fad617 commit a9d7ade
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
5 changes: 4 additions & 1 deletion lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,11 @@ function Spec:import(spec)
local modspecs = {}

if type(import) == "string" then
Util.lsmod(import, function(modname)
Util.lsmod(import, function(modname, modpath)
modspecs[#modspecs + 1] = modname
package.preload[modname] = function()
return loadfile(modpath)()
end
end)
table.sort(modspecs)
else
Expand Down
16 changes: 9 additions & 7 deletions lua/lazy/core/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -287,33 +287,35 @@ function M.find_root(modname)
local ret = require("lazy.core.cache").find(modname, {
rtp = true,
paths = paths,
patterns = { "", ".lua" },
patterns = { ".lua", "" },
})[1]

if not ret and cached then
paths = M.get_unloaded_rtp(modname)
ret = require("lazy.core.cache").find(modname, {
rtp = false,
paths = paths,
patterns = { "", ".lua" },
patterns = { ".lua", "" },
})[1]
end
if ret then
local root = ret.modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "")
return root
return ret.modpath:gsub("%.lua$", ""), ret.modpath
end
end

---@param modname string
---@param fn fun(modname:string, modpath:string)
function M.lsmod(modname, fn)
local root = M.find_root(modname)
local root, match = M.find_root(modname)
if not root then
return
end

if vim.uv.fs_stat(root .. ".lua") then
fn(modname, root .. ".lua")
if match:sub(-4) == ".lua" then
fn(modname, match)
if not vim.uv.fs_stat(root) then
return
end
end

M.ls(root, function(path, name, type)
Expand Down

0 comments on commit a9d7ade

Please sign in to comment.