2022-11-25 21:48:17 +00:00
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
local Util = require("lazy.core.util")
|
|
|
|
local Module = require("lazy.core.module")
|
|
|
|
local Cache = require("lazy.core.cache")
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
---@alias CachedPlugin LazyPlugin | {_funs: string[]}
|
2022-11-28 10:19:50 +00:00
|
|
|
local skip = { _ = true, dir = true }
|
2022-11-25 21:48:17 +00:00
|
|
|
local funs = { config = true, init = true, run = true }
|
|
|
|
|
2022-11-26 18:33:38 +00:00
|
|
|
M.dirty = false
|
|
|
|
|
2022-11-28 06:36:32 +00:00
|
|
|
---@class LazyPluginHooks
|
2022-11-25 21:48:17 +00:00
|
|
|
---@field init? fun(LazyPlugin) Will always be run
|
|
|
|
---@field config? fun(LazyPlugin) Will be executed when loading the plugin
|
2022-11-28 06:36:32 +00:00
|
|
|
---@field run? string|fun()
|
|
|
|
|
|
|
|
---@class LazyPluginState
|
2022-11-25 21:48:17 +00:00
|
|
|
---@field loaded? {[string]:string, time:number}
|
2022-11-28 23:15:13 +00:00
|
|
|
---@field installed boolean
|
2022-11-25 21:48:17 +00:00
|
|
|
---@field tasks? LazyTask[]
|
|
|
|
---@field dirty? boolean
|
|
|
|
---@field updated? {from:string, to:string}
|
2022-11-28 23:15:13 +00:00
|
|
|
---@field is_local boolean
|
2022-11-28 12:18:31 +00:00
|
|
|
---@field is_symlink? boolean
|
2022-11-28 21:03:44 +00:00
|
|
|
---@field cloned? boolean
|
2022-11-25 21:48:17 +00:00
|
|
|
|
2022-11-28 06:36:32 +00:00
|
|
|
---@class LazyPluginRef
|
|
|
|
---@field branch? string
|
|
|
|
---@field tag? string
|
|
|
|
---@field commit? string
|
|
|
|
---@field version? string
|
2022-11-29 11:36:07 +00:00
|
|
|
---@field lock? boolean
|
2022-11-28 06:36:32 +00:00
|
|
|
|
2022-11-28 10:19:50 +00:00
|
|
|
---@class LazyPlugin: LazyPluginHandlers,LazyPluginHooks,LazyPluginRef
|
2022-11-28 06:36:32 +00:00
|
|
|
---@field [1] string
|
|
|
|
---@field name string display name and name used for plugin config files
|
|
|
|
---@field uri string
|
|
|
|
---@field dir string
|
2022-11-29 18:51:37 +00:00
|
|
|
---@field dep? boolean True if this plugin is only in the spec as a dependency
|
2022-11-28 06:36:32 +00:00
|
|
|
---@field enabled? boolean|(fun():boolean)
|
|
|
|
---@field opt? boolean
|
2022-11-29 13:27:04 +00:00
|
|
|
---@field dependencies? string[]
|
2022-11-28 10:19:50 +00:00
|
|
|
---@field _ LazyPluginState
|
2022-11-28 06:36:32 +00:00
|
|
|
|
2022-11-29 13:27:04 +00:00
|
|
|
---@alias LazySpec string|LazyPlugin|LazySpec[]|{dependencies:LazySpec}
|
2022-11-28 06:36:32 +00:00
|
|
|
|
|
|
|
---@class LazySpecLoader
|
2022-11-25 21:48:17 +00:00
|
|
|
---@field modname string
|
|
|
|
---@field modpath string
|
|
|
|
---@field plugins table<string, LazyPlugin>
|
2022-11-26 12:58:01 +00:00
|
|
|
---@field funs? table<string, string[]>
|
2022-11-25 21:48:17 +00:00
|
|
|
local Spec = {}
|
2022-11-29 13:27:58 +00:00
|
|
|
M.Spec = Spec
|
|
|
|
|
|
|
|
---@param spec? LazySpec
|
|
|
|
function Spec.new(spec)
|
|
|
|
local self = setmetatable({}, { __index = Spec })
|
|
|
|
self.plugins = {}
|
|
|
|
self.modname = nil
|
|
|
|
self.modpath = nil
|
|
|
|
if spec then
|
|
|
|
self:normalize(spec)
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
2022-11-25 21:48:17 +00:00
|
|
|
|
|
|
|
---@param modname string
|
|
|
|
---@param modpath string
|
|
|
|
function Spec.load(modname, modpath)
|
|
|
|
local self = setmetatable({}, { __index = Spec })
|
|
|
|
self.plugins = {}
|
|
|
|
self.modname = modname
|
|
|
|
self.modpath = modpath
|
2022-11-26 18:33:38 +00:00
|
|
|
local mod, cached = Module.load(modname, modpath)
|
|
|
|
M.dirty = M.dirty or not cached
|
|
|
|
self:normalize(assert(mod))
|
2022-11-25 21:48:17 +00:00
|
|
|
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
|
2022-11-29 18:51:37 +00:00
|
|
|
---@param is_dep? boolean
|
|
|
|
function Spec:add(plugin, is_dep)
|
2022-11-29 13:27:58 +00:00
|
|
|
local pkg = plugin[1]
|
|
|
|
if type(pkg) ~= "string" then
|
2022-11-25 21:48:17 +00:00
|
|
|
Util.error("Invalid plugin spec " .. vim.inspect(plugin))
|
|
|
|
end
|
2022-11-29 13:27:58 +00:00
|
|
|
|
|
|
|
if not plugin.uri then
|
|
|
|
local c = pkg:sub(1, 1)
|
|
|
|
if c == "~" then
|
|
|
|
plugin.uri = vim.loop.os_getenv("HOME") .. pkg:sub(2)
|
|
|
|
elseif c == "/" then
|
|
|
|
plugin.uri = pkg
|
|
|
|
elseif pkg:sub(1, 4) == "http" or pkg:sub(1, 3) == "ssh" then
|
|
|
|
plugin.uri = pkg
|
|
|
|
else
|
|
|
|
plugin.uri = ("https://github.com/" .. pkg .. ".git")
|
|
|
|
end
|
|
|
|
end
|
2022-11-26 12:58:01 +00:00
|
|
|
|
|
|
|
-- PERF: optimized code to get package name without using lua patterns
|
2022-11-25 21:48:17 +00:00
|
|
|
if not plugin.name then
|
2022-11-29 13:27:58 +00:00
|
|
|
local name = pkg:sub(-4) == ".git" and pkg:sub(1, -5) or pkg
|
2022-11-25 21:48:17 +00:00
|
|
|
local slash = name:reverse():find("/", 1, true) --[[@as number?]]
|
2022-11-29 13:27:58 +00:00
|
|
|
plugin.name = slash and name:sub(#name - slash + 2) or pkg:gsub("%W+", "_")
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
|
2022-11-29 18:51:37 +00:00
|
|
|
plugin.dep = is_dep
|
|
|
|
|
2022-11-25 21:48:17 +00:00
|
|
|
M.process_local(plugin)
|
|
|
|
local other = self.plugins[plugin.name]
|
2022-11-29 18:51:37 +00:00
|
|
|
self.plugins[plugin.name] = other and M.merge(other, plugin) or plugin
|
2022-11-25 21:48:17 +00:00
|
|
|
return self.plugins[plugin.name]
|
|
|
|
end
|
|
|
|
|
2022-11-28 06:36:32 +00:00
|
|
|
---@param spec LazySpec
|
2022-11-25 21:48:17 +00:00
|
|
|
---@param results? string[]
|
2022-11-29 18:51:37 +00:00
|
|
|
---@param is_dep? boolean
|
|
|
|
function Spec:normalize(spec, results, is_dep)
|
2022-11-25 21:48:17 +00:00
|
|
|
results = results or {}
|
|
|
|
if type(spec) == "string" then
|
2022-11-29 18:51:37 +00:00
|
|
|
table.insert(results, self:add({ spec }, is_dep).name)
|
2022-11-25 21:48:17 +00:00
|
|
|
elseif #spec > 1 or Util.is_list(spec) then
|
2022-11-28 06:36:32 +00:00
|
|
|
---@cast spec LazySpec[]
|
2022-11-25 21:48:17 +00:00
|
|
|
for _, s in ipairs(spec) do
|
2022-11-29 18:51:37 +00:00
|
|
|
self:normalize(s, results, is_dep)
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
2022-11-26 12:58:01 +00:00
|
|
|
elseif spec.enabled == nil or spec.enabled == true or (type(spec.enabled) == "function" and spec.enabled()) then
|
2022-11-28 06:36:32 +00:00
|
|
|
---@cast spec LazyPlugin
|
2022-11-29 18:51:37 +00:00
|
|
|
local plugin = self:add(spec, is_dep)
|
|
|
|
plugin.dependencies = plugin.dependencies and self:normalize(plugin.dependencies, {}, true) or nil
|
2022-11-25 21:48:17 +00:00
|
|
|
table.insert(results, plugin.name)
|
|
|
|
end
|
|
|
|
return results
|
|
|
|
end
|
|
|
|
|
2022-11-28 06:36:32 +00:00
|
|
|
---@param spec LazySpecLoader
|
2022-11-25 21:48:17 +00:00
|
|
|
function Spec.revive(spec)
|
|
|
|
if spec.funs then
|
2022-11-28 06:36:32 +00:00
|
|
|
---@type LazySpecLoader
|
2022-11-25 21:48:17 +00:00
|
|
|
local loaded = nil
|
|
|
|
for fun, plugins in pairs(spec.funs) do
|
|
|
|
for _, name in pairs(plugins) do
|
2022-11-26 12:58:01 +00:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2022-11-25 21:48:17 +00:00
|
|
|
spec.plugins[name][fun] = function(...)
|
|
|
|
loaded = loaded or Spec.load(spec.modname, spec.modpath)
|
|
|
|
return loaded.plugins[name][fun](...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return spec
|
|
|
|
end
|
|
|
|
|
2022-11-29 18:51:37 +00:00
|
|
|
---@param old LazyPlugin
|
|
|
|
---@param new LazyPlugin
|
|
|
|
---@return LazyPlugin
|
|
|
|
function M.merge(old, new)
|
|
|
|
local is_dep = old.dep and new.dep
|
|
|
|
|
|
|
|
local Handler = require("lazy.core.handler")
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
for k, v in pairs(new) do
|
|
|
|
if k == "dep" then
|
|
|
|
elseif old[k] ~= nil and old[k] ~= v then
|
|
|
|
if Handler.handlers[k] then
|
|
|
|
local values = type(v) == "string" and { v } or v
|
|
|
|
vim.list_extend(values, type(old[k]) == "string" and { old[k] } or old[k])
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
old[k] = values
|
|
|
|
else
|
|
|
|
error("Merging plugins is not supported for key `" .. k .. "`")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
old[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
old.dep = is_dep
|
|
|
|
return old
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param opts? {clean:boolean, installed:boolean, plugins?: LazyPlugin[]}
|
|
|
|
function M.update_state(opts)
|
|
|
|
opts = opts or {}
|
|
|
|
|
2022-11-28 12:18:31 +00:00
|
|
|
---@type table<"opt"|"start", table<string,FileType>>
|
2022-11-25 21:48:17 +00:00
|
|
|
local installed = { opt = {}, start = {} }
|
2022-11-29 18:51:37 +00:00
|
|
|
if opts.installed ~= false then
|
|
|
|
for opt, packs in pairs(installed) do
|
|
|
|
Util.ls(Config.options.packpath .. "/" .. opt, function(_, name, type)
|
|
|
|
if type == "directory" or type == "link" then
|
|
|
|
packs[name] = type
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
|
2022-11-29 18:51:37 +00:00
|
|
|
for _, plugin in pairs(opts.plugins or Config.plugins) do
|
2022-11-28 10:19:50 +00:00
|
|
|
plugin._ = plugin._ or {}
|
2022-11-25 21:48:17 +00:00
|
|
|
plugin[1] = plugin["1"] or plugin[1]
|
2022-11-28 21:03:44 +00:00
|
|
|
if plugin.opt == nil then
|
2022-11-29 18:51:37 +00:00
|
|
|
plugin.opt = plugin.dep or Config.options.opt
|
2022-11-28 21:03:44 +00:00
|
|
|
end
|
2022-11-25 21:48:17 +00:00
|
|
|
local opt = plugin.opt and "opt" or "start"
|
2022-11-28 23:14:23 +00:00
|
|
|
plugin.dir = Config.options.packpath .. "/" .. opt .. "/" .. plugin.name
|
2022-11-28 12:18:31 +00:00
|
|
|
plugin._.is_local = plugin.uri:sub(1, 4) ~= "http" and plugin.uri:sub(1, 3) ~= "git"
|
|
|
|
plugin._.is_symlink = installed[opt][plugin.name] == "link"
|
|
|
|
plugin._.installed = installed[opt][plugin.name] ~= nil
|
|
|
|
if plugin._.is_local == plugin._.is_symlink then
|
|
|
|
installed[opt][plugin.name] = nil
|
|
|
|
end
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
|
2022-11-29 18:51:37 +00:00
|
|
|
if opts.clean then
|
2022-11-25 21:48:17 +00:00
|
|
|
Config.to_clean = {}
|
|
|
|
for opt, packs in pairs(installed) do
|
|
|
|
for pack in pairs(packs) do
|
|
|
|
table.insert(Config.to_clean, {
|
|
|
|
name = pack,
|
|
|
|
pack = pack,
|
2022-11-28 23:14:23 +00:00
|
|
|
dir = Config.options.packpath .. "/" .. opt .. "/" .. pack,
|
2022-11-25 21:48:17 +00:00
|
|
|
opt = opt == "opt",
|
2022-11-28 12:18:31 +00:00
|
|
|
_ = {
|
|
|
|
installed = true,
|
|
|
|
},
|
2022-11-25 21:48:17 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.process_local(plugin)
|
|
|
|
for _, pattern in ipairs(Config.options.plugins_local.patterns) do
|
|
|
|
if plugin[1]:find(pattern, 1, true) then
|
|
|
|
plugin.uri = Config.options.plugins_local.path .. "/" .. plugin.name
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-28 06:36:32 +00:00
|
|
|
---@param cache? table<string,LazySpecLoader>
|
2022-11-26 12:58:01 +00:00
|
|
|
function M.specs(cache)
|
2022-11-28 06:36:32 +00:00
|
|
|
---@type LazySpecLoader[]
|
2022-11-25 21:48:17 +00:00
|
|
|
local specs = {}
|
|
|
|
|
2022-11-26 12:58:01 +00:00
|
|
|
local function _load(name, modpath)
|
|
|
|
local modname = Config.options.plugins .. (name and ("." .. name) or "")
|
|
|
|
Util.try(function()
|
|
|
|
local spec = cache and cache[modname]
|
|
|
|
spec = spec and not Module.is_dirty(modname, modpath) and Spec.revive(spec) or Spec.load(modname, modpath)
|
2022-11-25 21:48:17 +00:00
|
|
|
table.insert(specs, spec)
|
2022-11-26 12:58:01 +00:00
|
|
|
end, "Failed to load **" .. modname .. "**")
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
|
2022-11-26 12:58:01 +00:00
|
|
|
_load(nil, Config.paths.main)
|
|
|
|
Util.lsmod(Config.paths.plugins, _load)
|
2022-11-25 21:48:17 +00:00
|
|
|
return specs
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.load()
|
|
|
|
---@type boolean, LazyState?
|
|
|
|
local ok, state = pcall(vim.json.decode, Cache.get("cache.state"))
|
|
|
|
if not (ok and state and vim.deep_equal(Config.options, state.config)) then
|
2022-11-26 18:33:38 +00:00
|
|
|
M.dirty = true
|
2022-11-25 21:48:17 +00:00
|
|
|
state = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- load specs
|
|
|
|
Util.track("specs")
|
2022-11-26 12:58:01 +00:00
|
|
|
local specs = M.specs(state and state.specs)
|
2022-11-25 21:48:17 +00:00
|
|
|
Util.track()
|
|
|
|
|
|
|
|
-- merge
|
|
|
|
Config.plugins = {}
|
|
|
|
for _, spec in ipairs(specs) do
|
|
|
|
for _, plugin in pairs(spec.plugins) do
|
|
|
|
local other = Config.plugins[plugin.name]
|
2022-11-29 18:51:37 +00:00
|
|
|
Config.plugins[plugin.name] = other and M.merge(other, plugin) or plugin
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Util.track("state")
|
|
|
|
M.update_state()
|
|
|
|
Util.track()
|
|
|
|
|
2022-11-26 18:33:38 +00:00
|
|
|
if M.dirty then
|
2022-11-25 21:48:17 +00:00
|
|
|
Cache.dirty = true
|
2022-11-26 21:04:32 +00:00
|
|
|
elseif state then
|
|
|
|
require("lazy.core.handler")._groups = state.handlers
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.save()
|
|
|
|
---@class LazyState
|
|
|
|
local state = {
|
2022-11-28 06:36:32 +00:00
|
|
|
---@type table<string, LazySpecLoader>
|
2022-11-25 21:48:17 +00:00
|
|
|
specs = {},
|
2022-11-26 21:04:32 +00:00
|
|
|
handlers = require("lazy.core.handler").group(Config.plugins, true),
|
2022-11-25 21:48:17 +00:00
|
|
|
config = Config.options,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, spec in ipairs(M.specs()) do
|
|
|
|
spec.funs = {}
|
|
|
|
state.specs[spec.modname] = spec
|
|
|
|
for _, plugin in pairs(spec.plugins) do
|
2022-11-26 12:58:01 +00:00
|
|
|
if plugin.init or (plugin.opt == false and plugin.config) then
|
|
|
|
-- no use in caching specs that need init,
|
|
|
|
-- or specs that are in start and have a config,
|
|
|
|
-- since we'll load the real spec during startup anyway
|
|
|
|
state.specs[spec.modname] = nil
|
|
|
|
break
|
|
|
|
end
|
2022-11-25 21:48:17 +00:00
|
|
|
---@cast plugin CachedPlugin
|
|
|
|
for k, v in pairs(plugin) do
|
|
|
|
if type(v) == "function" then
|
|
|
|
if funs[k] then
|
|
|
|
spec.funs[k] = spec.funs[k] or {}
|
|
|
|
table.insert(spec.funs[k], plugin.name)
|
|
|
|
end
|
|
|
|
plugin[k] = nil
|
|
|
|
elseif skip[k] then
|
|
|
|
plugin[k] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Cache.set("cache.state", vim.json.encode(state))
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|