2022-11-25 21:48:17 +00:00
|
|
|
local Config = require("lazy.core.config")
|
2022-11-29 18:58:23 +00:00
|
|
|
local Handler = require("lazy.core.handler")
|
2023-10-09 10:25:42 +01:00
|
|
|
local Util = require("lazy.core.util")
|
2022-11-25 21:48:17 +00:00
|
|
|
|
2022-12-28 16:57:59 +00:00
|
|
|
---@class LazyCorePlugin
|
2022-11-25 21:48:17 +00:00
|
|
|
local M = {}
|
2023-01-11 22:07:58 +00:00
|
|
|
M.loading = false
|
2022-11-25 21:48:17 +00:00
|
|
|
|
2022-11-28 06:36:32 +00:00
|
|
|
---@class LazySpecLoader
|
2022-11-25 21:48:17 +00:00
|
|
|
---@field plugins table<string, LazyPlugin>
|
2023-09-29 15:11:56 +01:00
|
|
|
---@field fragments table<number, LazyPlugin>
|
2023-01-03 09:34:53 +00:00
|
|
|
---@field disabled table<string, LazyPlugin>
|
2023-09-29 15:11:56 +01:00
|
|
|
---@field dirty table<string, true>
|
|
|
|
---@field ignore_installed table<string, true>
|
2023-01-01 20:07:05 +00:00
|
|
|
---@field modules string[]
|
2023-01-02 08:44:09 +00:00
|
|
|
---@field notifs {msg:string, level:number, file?:string}[]
|
|
|
|
---@field importing? string
|
2023-05-23 07:57:53 +01:00
|
|
|
---@field optional? boolean
|
2022-11-25 21:48:17 +00:00
|
|
|
local Spec = {}
|
2022-11-29 13:27:58 +00:00
|
|
|
M.Spec = Spec
|
2023-09-29 15:11:56 +01:00
|
|
|
M.last_fid = 0
|
|
|
|
M.fid_stack = {} ---@type number[]
|
2022-11-29 13:27:58 +00:00
|
|
|
|
|
|
|
---@param spec? LazySpec
|
2023-05-23 07:57:53 +01:00
|
|
|
---@param opts? {optional?:boolean}
|
|
|
|
function Spec.new(spec, opts)
|
2022-11-29 13:27:58 +00:00
|
|
|
local self = setmetatable({}, { __index = Spec })
|
|
|
|
self.plugins = {}
|
2023-09-29 15:11:56 +01:00
|
|
|
self.fragments = {}
|
2023-01-03 09:34:53 +00:00
|
|
|
self.disabled = {}
|
2023-01-01 20:07:05 +00:00
|
|
|
self.modules = {}
|
2023-09-29 15:11:56 +01:00
|
|
|
self.dirty = {}
|
2023-01-02 08:44:09 +00:00
|
|
|
self.notifs = {}
|
2023-09-29 15:11:56 +01:00
|
|
|
self.ignore_installed = {}
|
2023-05-23 07:57:53 +01:00
|
|
|
self.optional = opts and opts.optional
|
2022-11-29 13:27:58 +00:00
|
|
|
if spec then
|
2023-01-12 12:07:51 +00:00
|
|
|
self:parse(spec)
|
2022-11-29 13:27:58 +00:00
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
2022-11-25 21:48:17 +00:00
|
|
|
|
2023-01-12 12:07:51 +00:00
|
|
|
function Spec:parse(spec)
|
|
|
|
self:normalize(spec)
|
2023-09-29 15:11:56 +01:00
|
|
|
self:fix_disabled()
|
2023-01-12 12:07:51 +00:00
|
|
|
end
|
|
|
|
|
2022-12-13 09:09:33 +00:00
|
|
|
-- PERF: optimized code to get package name without using lua patterns
|
|
|
|
function Spec.get_name(pkg)
|
|
|
|
local name = pkg:sub(-4) == ".git" and pkg:sub(1, -5) or pkg
|
2023-05-25 07:10:08 +01:00
|
|
|
name = name:sub(-1) == "/" and name:sub(1, -2) or name
|
2022-12-13 09:09:33 +00:00
|
|
|
local slash = name:reverse():find("/", 1, true) --[[@as number?]]
|
|
|
|
return slash and name:sub(#name - slash + 2) or pkg:gsub("%W+", "_")
|
|
|
|
end
|
|
|
|
|
2022-11-25 21:48:17 +00:00
|
|
|
---@param plugin LazyPlugin
|
2023-01-03 09:34:53 +00:00
|
|
|
---@param results? string[]
|
2023-09-29 15:11:56 +01:00
|
|
|
function Spec:add(plugin, results)
|
2023-01-03 09:34:53 +00:00
|
|
|
-- check if we already processed this spec. Can happen when a user uses the same instance of a spec in multiple specs
|
|
|
|
-- see https://github.com/folke/lazy.nvim/issues/45
|
2023-01-17 12:54:34 +00:00
|
|
|
if rawget(plugin, "_") then
|
2023-01-04 16:50:57 +00:00
|
|
|
if results then
|
|
|
|
table.insert(results, plugin.name)
|
|
|
|
end
|
|
|
|
return plugin
|
2023-01-03 09:34:53 +00:00
|
|
|
end
|
|
|
|
|
2023-01-16 16:03:43 +00:00
|
|
|
local is_ref = plugin[1] and not plugin[1]:find("/", 1, true)
|
|
|
|
|
|
|
|
if not plugin.url and not is_ref and plugin[1] then
|
2023-01-09 12:25:50 +00:00
|
|
|
local prefix = plugin[1]:sub(1, 4)
|
|
|
|
if prefix == "http" or prefix == "git@" then
|
|
|
|
plugin.url = plugin[1]
|
|
|
|
else
|
|
|
|
plugin.url = Config.options.git.url_format:format(plugin[1])
|
|
|
|
end
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
2022-11-29 13:27:58 +00:00
|
|
|
|
2023-10-15 07:36:15 +01:00
|
|
|
---@type string?
|
|
|
|
local dir
|
|
|
|
|
2022-12-13 09:09:33 +00:00
|
|
|
if plugin.dir then
|
2023-10-15 07:36:15 +01:00
|
|
|
dir = Util.norm(plugin.dir)
|
2022-12-13 09:09:33 +00:00
|
|
|
-- local plugin
|
|
|
|
plugin.name = plugin.name or Spec.get_name(plugin.dir)
|
|
|
|
elseif plugin.url then
|
|
|
|
plugin.name = plugin.name or Spec.get_name(plugin.url)
|
|
|
|
-- check for dev plugins
|
|
|
|
if plugin.dev == nil then
|
|
|
|
for _, pattern in ipairs(Config.options.dev.patterns) do
|
|
|
|
if plugin.url:find(pattern, 1, true) then
|
|
|
|
plugin.dev = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-01-16 16:03:43 +00:00
|
|
|
elseif is_ref then
|
|
|
|
plugin.name = plugin[1]
|
2022-12-13 09:09:33 +00:00
|
|
|
else
|
2022-12-22 15:36:07 +00:00
|
|
|
self:error("Invalid plugin spec " .. vim.inspect(plugin))
|
2023-01-04 16:50:57 +00:00
|
|
|
return
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
|
2023-05-25 07:10:08 +01:00
|
|
|
if not plugin.name or plugin.name == "" then
|
|
|
|
self:error("Plugin spec " .. vim.inspect(plugin) .. " has no name")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-10-15 07:36:15 +01:00
|
|
|
-- dev plugins
|
|
|
|
if
|
|
|
|
plugin.dev
|
|
|
|
and (not Config.options.dev.fallback or vim.fn.isdirectory(Config.options.dev.path .. "/" .. plugin.name) == 1)
|
|
|
|
then
|
|
|
|
dir = Config.options.dev.path .. "/" .. plugin.name
|
|
|
|
elseif plugin.dev == false then
|
|
|
|
-- explicitely select the default path
|
|
|
|
dir = Config.options.root .. "/" .. plugin.name
|
|
|
|
end
|
|
|
|
|
2023-01-08 14:01:49 +00:00
|
|
|
if type(plugin.config) == "table" then
|
|
|
|
self:warn(
|
|
|
|
"{" .. plugin.name .. "}: setting a table to `Plugin.config` is deprecated. Please use `Plugin.opts` instead"
|
|
|
|
)
|
|
|
|
---@diagnostic disable-next-line: assign-type-mismatch
|
|
|
|
plugin.opts = plugin.config
|
|
|
|
plugin.config = nil
|
|
|
|
end
|
|
|
|
|
2023-09-29 15:11:56 +01:00
|
|
|
local fpid = M.fid_stack[#M.fid_stack]
|
|
|
|
|
|
|
|
M.last_fid = M.last_fid + 1
|
|
|
|
plugin._ = {
|
2023-10-15 07:36:15 +01:00
|
|
|
dir = dir,
|
2023-09-29 15:11:56 +01:00
|
|
|
fid = M.last_fid,
|
|
|
|
fpid = fpid,
|
|
|
|
dep = fpid ~= nil,
|
2023-10-03 23:20:08 +01:00
|
|
|
module = self.importing,
|
2023-09-29 15:11:56 +01:00
|
|
|
}
|
|
|
|
self.fragments[plugin._.fid] = plugin
|
2023-10-15 07:36:15 +01:00
|
|
|
-- remote plugin
|
|
|
|
plugin.dir = plugin._.dir or (plugin.name and (Config.options.root .. "/" .. plugin.name)) or nil
|
2023-09-29 15:11:56 +01:00
|
|
|
|
|
|
|
if fpid then
|
|
|
|
local parent = self.fragments[fpid]
|
|
|
|
parent._.fdeps = parent._.fdeps or {}
|
|
|
|
table.insert(parent._.fdeps, plugin._.fid)
|
|
|
|
end
|
|
|
|
|
|
|
|
if plugin.dependencies then
|
|
|
|
table.insert(M.fid_stack, plugin._.fid)
|
|
|
|
plugin.dependencies = self:normalize(plugin.dependencies, {})
|
|
|
|
table.remove(M.fid_stack)
|
|
|
|
end
|
2022-11-29 18:51:37 +00:00
|
|
|
|
2023-01-04 09:36:51 +00:00
|
|
|
if self.plugins[plugin.name] then
|
|
|
|
plugin = self:merge(self.plugins[plugin.name], plugin)
|
2023-01-03 09:34:53 +00:00
|
|
|
end
|
2023-01-04 09:36:51 +00:00
|
|
|
self.plugins[plugin.name] = plugin
|
2023-01-04 16:50:57 +00:00
|
|
|
if results then
|
|
|
|
table.insert(results, plugin.name)
|
|
|
|
end
|
|
|
|
return plugin
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
|
2023-01-02 08:44:09 +00:00
|
|
|
function Spec:error(msg)
|
2023-01-02 09:08:45 +00:00
|
|
|
self:log(msg, vim.log.levels.ERROR)
|
2023-01-02 08:44:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Spec:warn(msg)
|
2023-01-02 09:08:45 +00:00
|
|
|
self:log(msg, vim.log.levels.WARN)
|
2023-01-02 08:44:09 +00:00
|
|
|
end
|
|
|
|
|
2023-09-29 15:11:56 +01:00
|
|
|
--- Rebuilds a plugin spec excluding any removed fragments
|
2023-10-04 11:50:46 +01:00
|
|
|
---@param name? string
|
2023-09-29 15:11:56 +01:00
|
|
|
function Spec:rebuild(name)
|
2023-10-04 11:50:46 +01:00
|
|
|
if not name then
|
|
|
|
for n, _ in pairs(self.dirty) do
|
|
|
|
self:rebuild(n)
|
|
|
|
end
|
|
|
|
self.dirty = {}
|
|
|
|
end
|
2023-09-29 15:11:56 +01:00
|
|
|
local plugin = self.plugins[name]
|
|
|
|
if not plugin then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local fragments = {} ---@type LazyPlugin[]
|
|
|
|
|
|
|
|
repeat
|
|
|
|
local super = plugin._.super
|
|
|
|
if self.fragments[plugin._.fid] then
|
|
|
|
plugin._.dep = plugin._.fpid ~= nil
|
|
|
|
plugin._.super = nil
|
|
|
|
if plugin._.fdeps then
|
|
|
|
plugin.dependencies = {}
|
|
|
|
for _, cid in ipairs(plugin._.fdeps) do
|
|
|
|
if self.fragments[cid] then
|
|
|
|
table.insert(plugin.dependencies, self.fragments[cid].name)
|
|
|
|
end
|
|
|
|
end
|
2023-07-22 09:20:52 +01:00
|
|
|
end
|
2023-09-29 15:11:56 +01:00
|
|
|
setmetatable(plugin, nil)
|
|
|
|
table.insert(fragments, 1, plugin)
|
2023-07-22 09:20:52 +01:00
|
|
|
end
|
2023-09-29 15:11:56 +01:00
|
|
|
plugin = super
|
|
|
|
until not plugin
|
|
|
|
|
|
|
|
if #fragments == 0 then
|
|
|
|
self.plugins[name] = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
plugin = fragments[1]
|
|
|
|
for i = 2, #fragments do
|
|
|
|
plugin = self:merge(plugin, fragments[i])
|
|
|
|
end
|
|
|
|
self.plugins[name] = plugin
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Recursively removes all fragments from a plugin spec or a given fragment
|
|
|
|
---@param id string|number Plugin name or fragment id
|
|
|
|
---@param opts {self: boolean}
|
|
|
|
function Spec:remove_fragments(id, opts)
|
|
|
|
local fids = {} ---@type number[]
|
|
|
|
|
|
|
|
if type(id) == "number" then
|
|
|
|
fids[1] = id
|
|
|
|
else
|
|
|
|
local plugin = self.plugins[id]
|
|
|
|
repeat
|
|
|
|
fids[#fids + 1] = plugin._.fid
|
|
|
|
plugin = plugin._.super
|
|
|
|
until not plugin
|
2023-07-22 09:20:52 +01:00
|
|
|
end
|
|
|
|
|
2023-09-29 15:11:56 +01:00
|
|
|
for _, fid in ipairs(fids) do
|
|
|
|
local fragment = self.fragments[fid]
|
|
|
|
if fragment then
|
|
|
|
for _, cid in ipairs(fragment._.fdeps or {}) do
|
|
|
|
self:remove_fragments(cid, { self = true })
|
|
|
|
end
|
|
|
|
if opts.self then
|
|
|
|
self.fragments[fid] = nil
|
2023-07-22 09:20:52 +01:00
|
|
|
end
|
2023-09-29 15:11:56 +01:00
|
|
|
self.dirty[fragment.name] = true
|
2023-07-22 09:20:52 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-06 14:30:01 +01:00
|
|
|
function Spec:fix_cond()
|
2023-05-27 07:45:14 +01:00
|
|
|
for _, plugin in pairs(self.plugins) do
|
2023-07-06 14:30:01 +01:00
|
|
|
local cond = plugin.cond
|
|
|
|
if cond == nil then
|
|
|
|
cond = Config.options.defaults.cond
|
|
|
|
end
|
|
|
|
if cond == false or (type(cond) == "function" and not cond(plugin)) then
|
|
|
|
plugin._.cond = false
|
2023-09-29 15:11:56 +01:00
|
|
|
local stack = { plugin }
|
|
|
|
while #stack > 0 do
|
|
|
|
local p = table.remove(stack)
|
2023-09-30 17:02:07 +01:00
|
|
|
if not self.ignore_installed[p.name] then
|
|
|
|
for _, dep in ipairs(p.dependencies or {}) do
|
|
|
|
table.insert(stack, self.plugins[dep])
|
|
|
|
end
|
|
|
|
self.ignore_installed[p.name] = true
|
2023-09-29 15:11:56 +01:00
|
|
|
end
|
|
|
|
end
|
2023-07-06 14:30:01 +01:00
|
|
|
plugin.enabled = false
|
2023-05-27 07:45:14 +01:00
|
|
|
end
|
|
|
|
end
|
2023-07-06 14:30:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Spec:fix_optional()
|
2023-05-23 07:57:53 +01:00
|
|
|
if not self.optional then
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
local function all_optional(plugin)
|
|
|
|
return (not plugin) or (rawget(plugin, "optional") and all_optional(plugin._.super))
|
|
|
|
end
|
2023-05-22 13:31:23 +01:00
|
|
|
|
2023-05-23 07:57:53 +01:00
|
|
|
-- handle optional plugins
|
|
|
|
for _, plugin in pairs(self.plugins) do
|
|
|
|
if plugin.optional and all_optional(plugin) then
|
2023-09-29 15:11:56 +01:00
|
|
|
-- remove all optional fragments
|
|
|
|
self:remove_fragments(plugin.name, { self = true })
|
2023-05-23 07:57:53 +01:00
|
|
|
self.plugins[plugin.name] = nil
|
|
|
|
end
|
2023-05-22 13:24:18 +01:00
|
|
|
end
|
|
|
|
end
|
2023-07-06 14:30:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Spec:fix_disabled()
|
|
|
|
for _, plugin in pairs(self.plugins) do
|
|
|
|
if not plugin.name or not plugin.dir then
|
|
|
|
self:error("Plugin spec for **" .. plugin.name .. "** not found.\n```lua\n" .. vim.inspect(plugin) .. "\n```")
|
|
|
|
self.plugins[plugin.name] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-29 15:11:56 +01:00
|
|
|
self:fix_optional()
|
2023-10-04 11:50:46 +01:00
|
|
|
self:rebuild()
|
|
|
|
|
2023-07-22 09:20:52 +01:00
|
|
|
self:fix_cond()
|
2023-10-04 11:50:46 +01:00
|
|
|
self:rebuild()
|
|
|
|
|
2023-10-04 10:48:14 +01:00
|
|
|
self.dirty = {}
|
2023-07-22 09:20:52 +01:00
|
|
|
|
2023-01-04 09:36:51 +00:00
|
|
|
for _, plugin in pairs(self.plugins) do
|
2023-09-29 15:11:56 +01:00
|
|
|
local disabled = plugin.enabled == false or (type(plugin.enabled) == "function" and not plugin.enabled())
|
|
|
|
if disabled then
|
2023-01-04 09:36:51 +00:00
|
|
|
plugin._.kind = "disabled"
|
2023-09-29 15:11:56 +01:00
|
|
|
-- remove all child fragments
|
|
|
|
self:remove_fragments(plugin.name, { self = false })
|
2023-01-04 09:36:51 +00:00
|
|
|
self.plugins[plugin.name] = nil
|
|
|
|
self.disabled[plugin.name] = plugin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-29 15:11:56 +01:00
|
|
|
-- rebuild any plugin specs that were modified
|
2023-10-04 11:50:46 +01:00
|
|
|
self:rebuild()
|
2023-01-04 09:36:51 +00:00
|
|
|
end
|
|
|
|
|
2023-01-02 08:44:09 +00:00
|
|
|
---@param msg string
|
|
|
|
---@param level number
|
2023-01-02 09:08:45 +00:00
|
|
|
function Spec:log(msg, level)
|
2023-01-02 08:44:09 +00:00
|
|
|
self.notifs[#self.notifs + 1] = { msg = msg, level = level, file = self.importing }
|
2023-01-02 09:08:45 +00:00
|
|
|
end
|
|
|
|
|
2023-01-02 11:53:35 +00:00
|
|
|
function Spec:report(level)
|
|
|
|
level = level or vim.log.levels.ERROR
|
2023-10-15 07:36:15 +01:00
|
|
|
local count = 0
|
2023-01-02 09:08:45 +00:00
|
|
|
for _, notif in ipairs(self.notifs) do
|
2023-01-02 11:53:35 +00:00
|
|
|
if notif.level >= level then
|
2023-01-10 07:42:20 +00:00
|
|
|
Util.notify(notif.msg, { level = notif.level })
|
2023-10-15 07:36:15 +01:00
|
|
|
count = count + 1
|
2023-01-02 11:53:35 +00:00
|
|
|
end
|
2023-01-02 09:08:45 +00:00
|
|
|
end
|
2023-10-15 07:36:15 +01:00
|
|
|
return count
|
2022-12-22 15:36:07 +00:00
|
|
|
end
|
|
|
|
|
2023-01-01 20:07:05 +00:00
|
|
|
---@param spec LazySpec|LazySpecImport
|
2022-11-25 21:48:17 +00:00
|
|
|
---@param results? string[]
|
2023-09-29 15:11:56 +01:00
|
|
|
function Spec:normalize(spec, results)
|
2022-11-25 21:48:17 +00:00
|
|
|
if type(spec) == "string" then
|
2023-09-29 15:11:56 +01:00
|
|
|
if not spec:find("/", 1, true) then
|
2022-12-22 15:36:07 +00:00
|
|
|
-- spec is a plugin name
|
2023-01-03 09:34:53 +00:00
|
|
|
if results then
|
|
|
|
table.insert(results, spec)
|
|
|
|
end
|
2022-12-22 15:36:07 +00:00
|
|
|
else
|
2023-09-29 15:11:56 +01:00
|
|
|
self:add({ spec }, results)
|
2022-12-22 15:36:07 +00:00
|
|
|
end
|
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
|
2023-09-29 15:11:56 +01:00
|
|
|
self:normalize(s, results)
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
2023-01-04 16:50:57 +00:00
|
|
|
elseif spec[1] or spec.dir or spec.url then
|
|
|
|
---@cast spec LazyPlugin
|
2023-09-29 15:11:56 +01:00
|
|
|
local plugin = self:add(spec, results)
|
2023-01-04 16:50:57 +00:00
|
|
|
---@diagnostic disable-next-line: cast-type-mismatch
|
|
|
|
---@cast plugin LazySpecImport
|
|
|
|
if plugin and plugin.import then
|
|
|
|
self:import(plugin)
|
|
|
|
end
|
2023-01-01 20:07:05 +00:00
|
|
|
elseif spec.import then
|
|
|
|
---@cast spec LazySpecImport
|
|
|
|
self:import(spec)
|
|
|
|
else
|
2023-01-04 16:50:57 +00:00
|
|
|
self:error("Invalid plugin spec " .. vim.inspect(spec))
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
return results
|
|
|
|
end
|
|
|
|
|
2023-01-01 20:07:05 +00:00
|
|
|
---@param spec LazySpecImport
|
|
|
|
function Spec:import(spec)
|
2023-01-03 21:49:04 +00:00
|
|
|
if spec.import == "lazy" then
|
|
|
|
return self:error("You can't name your plugins module `lazy`.")
|
|
|
|
end
|
2023-05-25 07:25:34 +01:00
|
|
|
if type(spec.import) ~= "string" then
|
|
|
|
return self:error("Invalid import spec. `import` should be a string: " .. vim.inspect(spec))
|
|
|
|
end
|
2023-01-10 08:20:39 +00:00
|
|
|
if vim.tbl_contains(self.modules, spec.import) then
|
|
|
|
return
|
|
|
|
end
|
2023-10-06 14:46:46 +01:00
|
|
|
if spec.cond == false or (type(spec.cond) == "function" and not spec.cond()) then
|
|
|
|
return
|
|
|
|
end
|
2023-01-01 20:07:05 +00:00
|
|
|
if spec.enabled == false or (type(spec.enabled) == "function" and not spec.enabled()) then
|
|
|
|
return
|
|
|
|
end
|
2023-01-02 08:44:09 +00:00
|
|
|
|
2023-01-02 11:53:35 +00:00
|
|
|
self.modules[#self.modules + 1] = spec.import
|
2023-01-02 08:44:09 +00:00
|
|
|
|
|
|
|
local imported = 0
|
2023-02-17 12:58:16 +00:00
|
|
|
|
|
|
|
---@type string[]
|
|
|
|
local modnames = {}
|
2023-01-01 20:07:05 +00:00
|
|
|
Util.lsmod(spec.import, function(modname)
|
2023-02-17 12:58:16 +00:00
|
|
|
modnames[#modnames + 1] = modname
|
|
|
|
end)
|
|
|
|
table.sort(modnames)
|
|
|
|
|
|
|
|
for _, modname in ipairs(modnames) do
|
2023-01-02 08:44:09 +00:00
|
|
|
imported = imported + 1
|
|
|
|
Util.track({ import = modname })
|
|
|
|
self.importing = modname
|
2023-01-01 20:07:05 +00:00
|
|
|
-- unload the module so we get a clean slate
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
package.loaded[modname] = nil
|
|
|
|
Util.try(function()
|
2023-04-22 13:10:06 +01:00
|
|
|
local mod = require(modname)
|
|
|
|
if type(mod) ~= "table" then
|
|
|
|
self.importing = nil
|
|
|
|
return self:error(
|
|
|
|
"Invalid spec module: `"
|
|
|
|
.. modname
|
|
|
|
.. "`\nExpected a `table` of specs, but a `"
|
|
|
|
.. type(mod)
|
|
|
|
.. "` was returned instead"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
self:normalize(mod)
|
2023-01-02 08:44:09 +00:00
|
|
|
self.importing = nil
|
|
|
|
Util.track()
|
2023-01-01 20:07:05 +00:00
|
|
|
end, {
|
|
|
|
msg = "Failed to load `" .. modname .. "`",
|
|
|
|
on_error = function(msg)
|
|
|
|
self:error(msg)
|
2023-01-02 08:44:09 +00:00
|
|
|
self.importing = nil
|
|
|
|
Util.track()
|
2023-01-01 20:07:05 +00:00
|
|
|
end,
|
|
|
|
})
|
2023-02-17 12:58:16 +00:00
|
|
|
end
|
2023-01-02 08:44:09 +00:00
|
|
|
if imported == 0 then
|
|
|
|
self:error("No specs found for module " .. spec.import)
|
|
|
|
end
|
2023-01-01 20:07:05 +00:00
|
|
|
end
|
|
|
|
|
2022-11-29 18:51:37 +00:00
|
|
|
---@param old LazyPlugin
|
|
|
|
---@param new LazyPlugin
|
|
|
|
---@return LazyPlugin
|
2022-11-29 23:18:59 +00:00
|
|
|
function Spec:merge(old, new)
|
2023-01-04 08:35:00 +00:00
|
|
|
new._.dep = old._.dep and new._.dep
|
|
|
|
|
2023-01-07 08:37:03 +00:00
|
|
|
if new.url and old.url and new.url ~= old.url then
|
2023-10-14 22:07:01 +01:00
|
|
|
self:warn("Two plugins with the same name and different url:\n" .. vim.inspect({ old = old, new = new }))
|
2023-01-07 08:37:03 +00:00
|
|
|
end
|
|
|
|
|
2023-01-12 12:07:51 +00:00
|
|
|
if new.dependencies and old.dependencies then
|
2023-01-17 13:00:33 +00:00
|
|
|
Util.extend(new.dependencies, old.dependencies)
|
2022-11-29 18:51:37 +00:00
|
|
|
end
|
2023-01-12 12:07:51 +00:00
|
|
|
|
2023-10-15 07:36:15 +01:00
|
|
|
local new_dir = new._.dir or old._.dir or (new.name and (Config.options.root .. "/" .. new.name)) or nil
|
2023-10-15 07:51:54 +01:00
|
|
|
if new_dir ~= old.dir then
|
|
|
|
local msg = "Plugin `" .. new.name .. "` changed `dir`:\n- from: `" .. old.dir .. "`\n- to: `" .. new_dir .. "`"
|
|
|
|
if new._.rtp_loaded or old._.rtp_loaded then
|
|
|
|
msg = msg
|
|
|
|
.. "\n\nThis plugin was already partially loaded, so we did not change it's `dir`.\nPlease fix your config."
|
|
|
|
self:error(msg)
|
|
|
|
new_dir = old.dir
|
|
|
|
else
|
|
|
|
self:warn(msg)
|
|
|
|
end
|
2023-10-15 07:36:15 +01:00
|
|
|
end
|
|
|
|
new.dir = new_dir
|
2023-10-15 07:51:54 +01:00
|
|
|
new._.rtp_loaded = new._.rtp_loaded or old._.rtp_loaded
|
2023-10-15 07:36:15 +01:00
|
|
|
|
2023-01-04 08:35:00 +00:00
|
|
|
new._.super = old
|
|
|
|
setmetatable(new, { __index = old })
|
2023-01-10 08:20:39 +00:00
|
|
|
|
2023-01-04 08:35:00 +00:00
|
|
|
return new
|
2022-11-29 18:51:37 +00:00
|
|
|
end
|
|
|
|
|
2022-11-29 23:18:59 +00:00
|
|
|
function M.update_state()
|
2023-02-28 10:51:16 +00:00
|
|
|
---@type string[]
|
|
|
|
local cloning = {}
|
|
|
|
|
2022-12-01 10:06:44 +00:00
|
|
|
---@type table<string,FileType>
|
|
|
|
local installed = {}
|
2022-12-03 14:48:06 +00:00
|
|
|
Util.ls(Config.options.root, function(_, name, type)
|
2022-12-20 06:19:55 +00:00
|
|
|
if type == "directory" and name ~= "readme" then
|
2022-12-01 10:06:44 +00:00
|
|
|
installed[name] = type
|
2023-02-28 10:51:16 +00:00
|
|
|
elseif type == "file" and name:sub(-8) == ".cloning" then
|
|
|
|
name = name:sub(1, -9)
|
|
|
|
cloning[#cloning + 1] = name
|
2022-12-01 10:06:44 +00:00
|
|
|
end
|
|
|
|
end)
|
2022-11-25 21:48:17 +00:00
|
|
|
|
2023-02-28 10:51:16 +00:00
|
|
|
for _, failed in ipairs(cloning) do
|
|
|
|
installed[failed] = nil
|
|
|
|
end
|
|
|
|
|
2022-11-29 23:18:59 +00:00
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2022-11-28 10:19:50 +00:00
|
|
|
plugin._ = plugin._ or {}
|
2022-12-01 10:06:44 +00:00
|
|
|
if plugin.lazy == nil then
|
2022-12-02 16:09:40 +00:00
|
|
|
local lazy = plugin._.dep
|
|
|
|
or Config.options.defaults.lazy
|
|
|
|
or plugin.event
|
|
|
|
or plugin.keys
|
|
|
|
or plugin.ft
|
|
|
|
or plugin.cmd
|
2022-12-01 10:06:44 +00:00
|
|
|
plugin.lazy = lazy and true or false
|
2022-11-28 21:03:44 +00:00
|
|
|
end
|
2022-12-20 06:15:26 +00:00
|
|
|
if plugin.dir:find(Config.options.root, 1, true) == 1 then
|
2022-12-03 14:31:21 +00:00
|
|
|
plugin._.installed = installed[plugin.name] ~= nil
|
2022-12-01 10:06:44 +00:00
|
|
|
installed[plugin.name] = nil
|
2022-12-13 09:09:33 +00:00
|
|
|
else
|
|
|
|
plugin._.is_local = true
|
|
|
|
plugin._.installed = true -- local plugins are managed by the user
|
2022-11-28 12:18:31 +00:00
|
|
|
end
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
|
2023-09-29 15:11:56 +01:00
|
|
|
for name in pairs(Config.spec.ignore_installed) do
|
|
|
|
installed[name] = nil
|
2023-07-06 14:30:01 +01:00
|
|
|
end
|
|
|
|
|
2022-11-29 23:18:59 +00:00
|
|
|
Config.to_clean = {}
|
2022-12-01 10:06:44 +00:00
|
|
|
for pack, dir_type in pairs(installed) do
|
|
|
|
table.insert(Config.to_clean, {
|
|
|
|
name = pack,
|
2022-12-03 14:48:06 +00:00
|
|
|
dir = Config.options.root .. "/" .. pack,
|
2022-12-01 10:06:44 +00:00
|
|
|
_ = {
|
2022-12-25 20:07:30 +00:00
|
|
|
kind = "clean",
|
2022-12-01 10:06:44 +00:00
|
|
|
installed = true,
|
|
|
|
is_symlink = dir_type == "link",
|
|
|
|
is_local = dir_type == "link",
|
|
|
|
},
|
|
|
|
})
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.load()
|
2023-01-11 22:07:58 +00:00
|
|
|
M.loading = true
|
2022-11-25 21:48:17 +00:00
|
|
|
-- load specs
|
2022-11-29 23:18:59 +00:00
|
|
|
Util.track("spec")
|
2023-01-02 08:44:09 +00:00
|
|
|
Config.spec = Spec.new()
|
2023-05-10 16:20:06 +01:00
|
|
|
Config.spec:parse({ vim.deepcopy(Config.options.spec), { "folke/lazy.nvim" } })
|
2022-12-01 15:28:25 +00:00
|
|
|
|
2022-12-28 23:49:38 +00:00
|
|
|
-- override some lazy props
|
2023-01-02 08:44:09 +00:00
|
|
|
local lazy = Config.spec.plugins["lazy.nvim"]
|
2023-01-03 09:34:53 +00:00
|
|
|
if lazy then
|
|
|
|
lazy.lazy = true
|
|
|
|
lazy.dir = Config.me
|
|
|
|
lazy.config = function()
|
|
|
|
error("lazy config should not be called")
|
|
|
|
end
|
|
|
|
lazy._.loaded = {}
|
2022-12-28 23:49:38 +00:00
|
|
|
end
|
2022-12-01 15:28:25 +00:00
|
|
|
|
2022-12-02 16:02:25 +00:00
|
|
|
local existing = Config.plugins
|
2023-01-02 08:44:09 +00:00
|
|
|
Config.plugins = Config.spec.plugins
|
2022-12-02 16:02:25 +00:00
|
|
|
-- copy state. This wont do anything during startup
|
|
|
|
for name, plugin in pairs(existing) do
|
|
|
|
if Config.plugins[name] then
|
2023-01-17 12:55:04 +00:00
|
|
|
local dep = Config.plugins[name]._.dep
|
|
|
|
local super = Config.plugins[name]._.super
|
2022-12-02 16:02:25 +00:00
|
|
|
Config.plugins[name]._ = plugin._
|
2023-01-17 12:55:04 +00:00
|
|
|
Config.plugins[name]._.dep = dep
|
|
|
|
Config.plugins[name]._.super = super
|
2022-12-02 16:02:25 +00:00
|
|
|
end
|
|
|
|
end
|
2022-11-29 23:18:59 +00:00
|
|
|
Util.track()
|
2022-11-25 21:48:17 +00:00
|
|
|
|
|
|
|
Util.track("state")
|
|
|
|
M.update_state()
|
|
|
|
Util.track()
|
2023-01-11 22:07:58 +00:00
|
|
|
M.loading = false
|
2023-05-25 07:09:19 +01:00
|
|
|
vim.api.nvim_exec_autocmds("User", { pattern = "LazyPlugins", modeline = false })
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
|
2022-12-02 08:22:15 +00:00
|
|
|
-- Finds the plugin that has this path
|
|
|
|
---@param path string
|
|
|
|
function M.find(path)
|
2023-01-02 13:35:36 +00:00
|
|
|
if not Config.spec then
|
|
|
|
return
|
|
|
|
end
|
2022-12-26 22:41:19 +00:00
|
|
|
local lua = path:find("/lua/", 1, true)
|
2022-12-16 12:06:30 +00:00
|
|
|
if lua then
|
|
|
|
local name = path:sub(1, lua - 1)
|
|
|
|
local slash = name:reverse():find("/", 1, true)
|
|
|
|
if slash then
|
|
|
|
name = name:sub(#name - slash + 2)
|
2023-01-02 14:27:01 +00:00
|
|
|
return name and Config.plugins[name] or Config.spec.plugins[name] or nil
|
2022-12-16 12:06:30 +00:00
|
|
|
end
|
|
|
|
end
|
2022-12-02 08:22:15 +00:00
|
|
|
end
|
|
|
|
|
2023-01-03 21:50:14 +00:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M.has_errors(plugin)
|
|
|
|
for _, task in ipairs(plugin._.tasks or {}) do
|
|
|
|
if task.error then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-01-12 12:07:51 +00:00
|
|
|
-- Merges super values or runs the values function to override values or return new ones
|
|
|
|
-- Used for opts, cmd, event, ft and keys
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
---@param prop string
|
|
|
|
---@param is_list? boolean
|
|
|
|
function M.values(plugin, prop, is_list)
|
2023-10-16 14:05:16 +01:00
|
|
|
if not plugin[prop] then
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
plugin._.values = plugin._.values or {}
|
|
|
|
local key = prop .. (is_list and "_list" or "")
|
|
|
|
if plugin._.values[key] == nil then
|
|
|
|
plugin[prop] = M._values(plugin, prop, is_list)
|
|
|
|
plugin._.values[key] = true
|
|
|
|
end
|
|
|
|
return plugin[prop] or {}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Merges super values or runs the values function to override values or return new ones
|
|
|
|
-- Used for opts, cmd, event, ft and keys
|
|
|
|
---@param plugin LazyPlugin
|
|
|
|
---@param prop string
|
|
|
|
---@param is_list? boolean
|
|
|
|
function M._values(plugin, prop, is_list)
|
2023-01-12 12:07:51 +00:00
|
|
|
---@type table
|
2023-10-16 14:05:16 +01:00
|
|
|
local ret = plugin._.super and M._values(plugin._.super, prop, is_list) or {}
|
2023-01-12 12:07:51 +00:00
|
|
|
local values = rawget(plugin, prop)
|
|
|
|
|
|
|
|
if not values then
|
|
|
|
return ret
|
|
|
|
elseif type(values) == "function" then
|
|
|
|
ret = values(plugin, ret) or ret
|
|
|
|
return type(ret) == "table" and ret or { ret }
|
|
|
|
end
|
|
|
|
|
|
|
|
values = type(values) == "table" and values or { values }
|
2023-01-17 13:00:33 +00:00
|
|
|
return is_list and Util.extend(ret, values) or Util.merge(ret, values)
|
2023-01-12 12:07:51 +00:00
|
|
|
end
|
|
|
|
|
2022-11-25 21:48:17 +00:00
|
|
|
return M
|