mirror of
https://github.com/folke/lazy.nvim.git
synced 2025-06-27 03:09:19 +02:00
feat: lazy caching now works with functions that have upvalues
This commit is contained in:
parent
48199f8031
commit
fe33e4e3dd
9 changed files with 161 additions and 187 deletions
|
@ -1,21 +1,17 @@
|
|||
local Config = require("lazy.core.config")
|
||||
local Util = require("lazy.util")
|
||||
local Util = require("lazy.core.util")
|
||||
local Module = require("lazy.core.module")
|
||||
local State = require("lazy.core.state")
|
||||
|
||||
local M = {}
|
||||
|
||||
M.funcs = { run = "run", init = "init", config = "config" }
|
||||
|
||||
---@class LazyPlugin
|
||||
---@field [1] string
|
||||
---@field name string display name and name used for plugin config files
|
||||
---@field pack string package name
|
||||
---@field uri string
|
||||
---@field modname? string
|
||||
---@field modpath? string
|
||||
---@field branch? string
|
||||
---@field dir string
|
||||
---@field enabled? boolean
|
||||
---@field opt? boolean
|
||||
---@field init? fun(LazyPlugin) Will always be run
|
||||
---@field config? fun(LazyPlugin) Will be executed when loading the plugin
|
||||
|
@ -32,120 +28,107 @@ M.funcs = { run = "run", init = "init", config = "config" }
|
|||
---@field dirty? boolean
|
||||
---@field updated? {from:string, to:string}
|
||||
|
||||
---@class LazySpec
|
||||
---@field modname string
|
||||
---@field modpath string
|
||||
---@field plugins table<string, LazyPlugin>
|
||||
local Spec = {}
|
||||
|
||||
---@param modname string
|
||||
---@param modpath string
|
||||
function Spec.load(modname, modpath)
|
||||
local self = setmetatable({}, { __index = Spec })
|
||||
self.plugins = {}
|
||||
self.modname = modname
|
||||
self.modpath = modpath
|
||||
self:normalize(assert(Module.load(modname, modpath)))
|
||||
if modname == Config.options.plugins and not self.plugins["lazy.nvim"] then
|
||||
self:add({ "folke/lazy.nvim", opt = false })
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
---@param plugin LazyPlugin
|
||||
function M.plugin(plugin)
|
||||
local pkg = plugin[1]
|
||||
if type(pkg) ~= "string" then
|
||||
function Spec:add(plugin)
|
||||
if type(plugin[1]) ~= "string" then
|
||||
Util.error("Invalid plugin spec " .. vim.inspect(plugin))
|
||||
end
|
||||
|
||||
plugin.uri = plugin.uri or ("https://github.com/" .. pkg .. ".git")
|
||||
plugin.pack = plugin.pack or plugin.name
|
||||
|
||||
plugin.uri = plugin.uri or ("https://github.com/" .. plugin[1] .. ".git")
|
||||
if not plugin.name then
|
||||
local name = plugin.uri:gsub("%.git$", ""):match("/([^/]+)$")
|
||||
plugin.pack = name
|
||||
if not name then
|
||||
name = pkg:gsub("%W+", "_")
|
||||
end
|
||||
name = name:gsub("[%.%-]n?vim$", "")
|
||||
name = name:gsub("^n?vim[%-%.]", "")
|
||||
name = name:gsub("%.lua$", "")
|
||||
name = name:gsub("%.", "_")
|
||||
plugin.name = name:lower()
|
||||
-- PERF: optimized code to get package name without using lua patterns
|
||||
local name = plugin[1]:sub(-4) == ".git" and plugin[1]:sub(1, -5) or plugin[1]
|
||||
local slash = name:reverse():find("/", 1, true) --[[@as number?]]
|
||||
plugin.name = slash and name:sub(#name - slash + 2) or plugin[1]:gsub("%W+", "_")
|
||||
end
|
||||
|
||||
if Config.plugins[plugin.name] and Config.plugins[plugin.name] ~= plugin then
|
||||
for k, v in pairs(plugin) do
|
||||
Config.plugins[plugin.name][k] = v
|
||||
M.process_local(plugin)
|
||||
local other = self.plugins[plugin.name]
|
||||
self.plugins[plugin.name] = other and vim.tbl_extend("force", self.plugins[plugin.name], plugin) or plugin
|
||||
return self.plugins[plugin.name]
|
||||
end
|
||||
|
||||
---@param spec table
|
||||
---@param results? string[]
|
||||
function Spec:normalize(spec, results)
|
||||
results = results or {}
|
||||
if type(spec) == "string" then
|
||||
table.insert(results, self:add({ spec }).name)
|
||||
elseif #spec > 1 or Util.is_list(spec) then
|
||||
---@cast spec table[]
|
||||
for _, s in ipairs(spec) do
|
||||
self:normalize(s, results)
|
||||
end
|
||||
return Config.plugins[plugin.name]
|
||||
else
|
||||
Config.plugins[plugin.name] = plugin
|
||||
elseif spec.enabled ~= false then
|
||||
local plugin = self:add(spec)
|
||||
plugin.requires = plugin.requires and self:normalize(plugin.requires, {}) or nil
|
||||
table.insert(results, plugin.name)
|
||||
end
|
||||
return plugin
|
||||
return results
|
||||
end
|
||||
|
||||
---@param plugin LazyPlugin
|
||||
function M.process_local(plugin)
|
||||
for _, pattern in ipairs(Config.options.plugins_local.patterns) do
|
||||
if plugin[1]:find(pattern) then
|
||||
plugin.uri = Config.options.plugins_local.path .. "/" .. plugin.pack
|
||||
if plugin[1]:find(pattern, 1, true) then
|
||||
plugin.uri = Config.options.plugins_local.path .. "/" .. plugin.name
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function M.process_config()
|
||||
---@alias LazySpecLoader fun(modname:string, modpath:string):LazySpec
|
||||
---@param loader? LazySpecLoader
|
||||
function M.specs(loader)
|
||||
loader = loader or Spec.load
|
||||
---@type LazySpec[]
|
||||
local specs = {}
|
||||
table.insert(specs, loader(Config.options.plugins, Config.paths.main))
|
||||
Util.lsmod(Config.paths.plugins, function(name, modpath)
|
||||
local plugin = Config.plugins[name]
|
||||
if plugin then
|
||||
local modname = Config.options.plugins .. "." .. name
|
||||
local ok, spec = pcall(Module.load, modname, modpath)
|
||||
if ok and spec then
|
||||
---@diagnostic disable-next-line: no-unknown
|
||||
for k, v in pairs(spec) do
|
||||
if k == "requires" then
|
||||
plugin.requires = M.normalize(v)
|
||||
elseif type(v) ~= "function" or M.funcs[k] then
|
||||
---@diagnostic disable-next-line: no-unknown
|
||||
plugin[k] = v
|
||||
end
|
||||
end
|
||||
plugin.modname = modname
|
||||
plugin.modpath = modpath
|
||||
M.plugin(plugin)
|
||||
else
|
||||
Util.error("Failed to load " .. modname .. "\n" .. spec)
|
||||
end
|
||||
end
|
||||
table.insert(specs, loader(Config.options.plugins .. "." .. name, modpath))
|
||||
end)
|
||||
return specs
|
||||
end
|
||||
|
||||
function M.reload()
|
||||
---@param loader? LazySpecLoader
|
||||
function M.load(loader)
|
||||
Util.track("specs")
|
||||
local specs = M.specs(loader)
|
||||
Util.track()
|
||||
|
||||
Config.plugins = {}
|
||||
M.normalize(assert(Module.load(Config.options.plugins, Config.paths.main)))
|
||||
|
||||
if not Config.plugins.lazy then
|
||||
M.plugin({
|
||||
"folke/lazy.nvim",
|
||||
opt = false,
|
||||
})
|
||||
end
|
||||
|
||||
M.process_config()
|
||||
for _, plugin in pairs(Config.plugins) do
|
||||
if plugin.opt == nil then
|
||||
plugin.opt = Config.options.opt
|
||||
for _, spec in ipairs(specs) do
|
||||
for _, plugin in pairs(spec.plugins) do
|
||||
local other = Config.plugins[plugin.name]
|
||||
Config.plugins[plugin.name] = other and vim.tbl_extend("force", other, plugin) or plugin
|
||||
end
|
||||
plugin.dir = Config.options.package_path .. "/" .. (plugin.opt and "opt" or "start") .. "/" .. plugin.pack
|
||||
M.process_local(plugin)
|
||||
end
|
||||
|
||||
Util.track("state")
|
||||
State.update_state()
|
||||
Util.track()
|
||||
end
|
||||
|
||||
---@param spec table
|
||||
---@param results? LazyPlugin[]
|
||||
function M.normalize(spec, results)
|
||||
results = results or {}
|
||||
if type(spec) == "string" then
|
||||
table.insert(results, M.plugin({ spec }).name)
|
||||
elseif #spec > 1 or vim.tbl_islist(spec) then
|
||||
---@cast spec LazyPlugin[]
|
||||
for _, s in ipairs(spec) do
|
||||
M.normalize(s, results)
|
||||
end
|
||||
else
|
||||
---@cast spec LazyPlugin
|
||||
spec = M.plugin(spec)
|
||||
if spec.requires then
|
||||
spec.requires = M.normalize(spec.requires)
|
||||
end
|
||||
table.insert(results, spec.name)
|
||||
end
|
||||
return results
|
||||
end
|
||||
|
||||
-- profile(M.rebuild, 1000, true)
|
||||
M.Spec = Spec
|
||||
|
||||
return M
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue