From a9d7ade203b3f3ee3058c082c62afdf8e4bcb416 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Mon, 1 Jul 2024 07:07:49 +0200 Subject: [PATCH] perf(plugin): minor optim to resolve imports a bit faster --- lua/lazy/core/plugin.lua | 5 ++++- lua/lazy/core/util.lua | 16 +++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lua/lazy/core/plugin.lua b/lua/lazy/core/plugin.lua index 26b3f6e..f9bd04f 100644 --- a/lua/lazy/core/plugin.lua +++ b/lua/lazy/core/plugin.lua @@ -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 diff --git a/lua/lazy/core/util.lua b/lua/lazy/core/util.lua index 34ca1d6..185527b 100644 --- a/lua/lazy/core/util.lua +++ b/lua/lazy/core/util.lua @@ -287,7 +287,7 @@ 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 @@ -295,25 +295,27 @@ function M.find_root(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)