2022-11-26 05:48:17 +08:00
|
|
|
local Config = require("lazy.core.config")
|
2023-10-09 17:25:42 +08:00
|
|
|
local Util = require("lazy.core.util")
|
2022-11-26 05:48:17 +08:00
|
|
|
|
2022-12-29 00:57:59 +08:00
|
|
|
---@class LazyCorePlugin
|
2022-11-26 05:48:17 +08:00
|
|
|
local M = {}
|
2023-01-12 06:07:58 +08:00
|
|
|
M.loading = false
|
2022-11-26 05:48:17 +08:00
|
|
|
|
2022-11-28 14:36:32 +08:00
|
|
|
---@class LazySpecLoader
|
2022-11-26 05:48:17 +08:00
|
|
|
---@field plugins table<string, LazyPlugin>
|
2023-09-29 22:11:56 +08:00
|
|
|
---@field fragments table<number, LazyPlugin>
|
2023-01-03 17:34:53 +08:00
|
|
|
---@field disabled table<string, LazyPlugin>
|
2023-09-29 22:11:56 +08:00
|
|
|
---@field dirty table<string, true>
|
|
|
|
---@field ignore_installed table<string, true>
|
2023-01-02 04:07:05 +08:00
|
|
|
---@field modules string[]
|
2023-01-02 16:44:09 +08:00
|
|
|
---@field notifs {msg:string, level:number, file?:string}[]
|
|
|
|
---@field importing? string
|
2023-05-23 14:57:53 +08:00
|
|
|
---@field optional? boolean
|
2022-11-26 05:48:17 +08:00
|
|
|
local Spec = {}
|
2022-11-29 21:27:58 +08:00
|
|
|
M.Spec = Spec
|
2023-09-29 22:11:56 +08:00
|
|
|
M.last_fid = 0
|
|
|
|
M.fid_stack = {} ---@type number[]
|
2024-05-26 22:43:52 +08:00
|
|
|
M.LOCAL_SPEC = ".lazy.lua"
|
2022-11-29 21:27:58 +08:00
|
|
|
|
|
|
|
---@param spec? LazySpec
|
2023-05-23 14:57:53 +08:00
|
|
|
---@param opts? {optional?:boolean}
|
|
|
|
function Spec.new(spec, opts)
|
2022-11-29 21:27:58 +08:00
|
|
|
local self = setmetatable({}, { __index = Spec })
|
|
|
|
self.plugins = {}
|
2023-09-29 22:11:56 +08:00
|
|
|
self.fragments = {}
|
2023-01-03 17:34:53 +08:00
|
|
|
self.disabled = {}
|
2023-01-02 04:07:05 +08:00
|
|
|
self.modules = {}
|
2023-09-29 22:11:56 +08:00
|
|
|
self.dirty = {}
|
2023-01-02 16:44:09 +08:00
|
|
|
self.notifs = {}
|
2023-09-29 22:11:56 +08:00
|
|
|
self.ignore_installed = {}
|
2023-05-23 14:57:53 +08:00
|
|
|
self.optional = opts and opts.optional
|
2022-11-29 21:27:58 +08:00
|
|
|
if spec then
|
2023-01-12 20:07:51 +08:00
|
|
|
self:parse(spec)
|
2022-11-29 21:27:58 +08:00
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
2022-11-26 05:48:17 +08:00
|
|
|
|
2023-01-12 20:07:51 +08:00
|
|
|
function Spec:parse(spec)
|
|
|
|
self:normalize(spec)
|
2023-09-29 22:11:56 +08:00
|
|
|
self:fix_disabled()
|
2023-01-12 20:07:51 +08:00
|
|
|
end
|
|
|
|
|
2022-12-13 17:09:33 +08: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 14:10:08 +08:00
|
|
|
name = name:sub(-1) == "/" and name:sub(1, -2) or name
|
2022-12-13 17:09:33 +08: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-26 05:48:17 +08:00
|
|
|
---@param plugin LazyPlugin
|
2023-01-03 17:34:53 +08:00
|
|
|
---@param results? string[]
|
2023-09-29 22:11:56 +08:00
|
|
|
function Spec:add(plugin, results)
|
2023-01-03 17:34:53 +08: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 20:54:34 +08:00
|
|
|
if rawget(plugin, "_") then
|
2023-01-05 00:50:57 +08:00
|
|
|
if results then
|
|
|
|
table.insert(results, plugin.name)
|
|
|
|
end
|
|
|
|
return plugin
|
2023-01-03 17:34:53 +08:00
|
|
|
end
|
|
|
|
|
2023-01-17 00:03:43 +08: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 20:25:50 +08: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-26 05:48:17 +08:00
|
|
|
end
|
2022-11-29 21:27:58 +08:00
|
|
|
|
2023-10-15 14:36:15 +08:00
|
|
|
---@type string?
|
|
|
|
local dir
|
|
|
|
|
2022-12-13 17:09:33 +08:00
|
|
|
if plugin.dir then
|
2023-10-15 14:36:15 +08:00
|
|
|
dir = Util.norm(plugin.dir)
|
2022-12-13 17:09:33 +08: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-17 00:03:43 +08:00
|
|
|
elseif is_ref then
|
|
|
|
plugin.name = plugin[1]
|
2022-12-13 17:09:33 +08:00
|
|
|
else
|
2022-12-22 23:36:07 +08:00
|
|
|
self:error("Invalid plugin spec " .. vim.inspect(plugin))
|
2023-01-05 00:50:57 +08:00
|
|
|
return
|
2022-11-26 05:48:17 +08:00
|
|
|
end
|
|
|
|
|
2023-05-25 14:10:08 +08:00
|
|
|
if not plugin.name or plugin.name == "" then
|
|
|
|
self:error("Plugin spec " .. vim.inspect(plugin) .. " has no name")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-10-15 14:36:15 +08:00
|
|
|
-- dev plugins
|
feat(plugin): dev.path can now be a function (#1157)
In some case, `dev.path .. plugin.name` is not enoguh.
For example, when using `ghq` to manage projects, plugin directories may
vary by onewrs of the plugins.
With this change, users can do something like below
``` lua
require("lazy").setup("plugins", {
dev = {
path = function(p)
-- ghq
local path, cnt = string.gsub(p.url, "^https://(.*)%.git$", "~/ghq/%1")
if cnt == 1 then
return path
end
-- fallback to default
return "~/projects/" .. plugin.name
end,
},
})
```
2024-01-20 21:19:09 +08:00
|
|
|
if plugin.dev then
|
|
|
|
local dir_dev
|
|
|
|
if type(Config.options.dev.path) == "string" then
|
|
|
|
dir_dev = Config.options.dev.path .. "/" .. plugin.name
|
|
|
|
else
|
|
|
|
dir_dev = Util.norm(Config.options.dev.path(plugin))
|
|
|
|
end
|
|
|
|
if not Config.options.dev.fallback or vim.fn.isdirectory(dir_dev) == 1 then
|
|
|
|
dir = dir_dev
|
|
|
|
end
|
2023-10-15 14:36:15 +08:00
|
|
|
elseif plugin.dev == false then
|
2024-06-03 15:18:43 +08:00
|
|
|
-- explicitly select the default path
|
2023-10-15 14:36:15 +08:00
|
|
|
dir = Config.options.root .. "/" .. plugin.name
|
|
|
|
end
|
|
|
|
|
2023-01-08 22:01:49 +08: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 22:11:56 +08:00
|
|
|
local fpid = M.fid_stack[#M.fid_stack]
|
|
|
|
|
|
|
|
M.last_fid = M.last_fid + 1
|
|
|
|
plugin._ = {
|
2023-10-15 14:36:15 +08:00
|
|
|
dir = dir,
|
2023-09-29 22:11:56 +08:00
|
|
|
fid = M.last_fid,
|
|
|
|
fpid = fpid,
|
|
|
|
dep = fpid ~= nil,
|
2023-10-04 06:20:08 +08:00
|
|
|
module = self.importing,
|
2023-09-29 22:11:56 +08:00
|
|
|
}
|
|
|
|
self.fragments[plugin._.fid] = plugin
|
2023-10-15 14:36:15 +08:00
|
|
|
-- remote plugin
|
|
|
|
plugin.dir = plugin._.dir or (plugin.name and (Config.options.root .. "/" .. plugin.name)) or nil
|
2023-09-29 22:11:56 +08: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-30 02:51:37 +08:00
|
|
|
|
2023-01-04 17:36:51 +08:00
|
|
|
if self.plugins[plugin.name] then
|
|
|
|
plugin = self:merge(self.plugins[plugin.name], plugin)
|
2023-01-03 17:34:53 +08:00
|
|
|
end
|
2023-01-04 17:36:51 +08:00
|
|
|
self.plugins[plugin.name] = plugin
|
2023-01-05 00:50:57 +08:00
|
|
|
if results then
|
|
|
|
table.insert(results, plugin.name)
|
|
|
|
end
|
|
|
|
return plugin
|
2022-11-26 05:48:17 +08:00
|
|
|
end
|
|
|
|
|
2023-01-02 16:44:09 +08:00
|
|
|
function Spec:error(msg)
|
2023-01-02 17:08:45 +08:00
|
|
|
self:log(msg, vim.log.levels.ERROR)
|
2023-01-02 16:44:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function Spec:warn(msg)
|
2023-01-02 17:08:45 +08:00
|
|
|
self:log(msg, vim.log.levels.WARN)
|
2023-01-02 16:44:09 +08:00
|
|
|
end
|
|
|
|
|
2023-09-29 22:11:56 +08:00
|
|
|
--- Rebuilds a plugin spec excluding any removed fragments
|
2023-10-04 18:50:46 +08:00
|
|
|
---@param name? string
|
2023-09-29 22:11:56 +08:00
|
|
|
function Spec:rebuild(name)
|
2023-10-04 18:50:46 +08:00
|
|
|
if not name then
|
|
|
|
for n, _ in pairs(self.dirty) do
|
|
|
|
self:rebuild(n)
|
|
|
|
end
|
|
|
|
self.dirty = {}
|
|
|
|
end
|
2023-09-29 22:11:56 +08: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 16:20:52 +08:00
|
|
|
end
|
2023-09-29 22:11:56 +08:00
|
|
|
setmetatable(plugin, nil)
|
|
|
|
table.insert(fragments, 1, plugin)
|
2023-07-22 16:20:52 +08:00
|
|
|
end
|
2023-09-29 22:11:56 +08: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 16:20:52 +08:00
|
|
|
end
|
|
|
|
|
2023-09-29 22:11:56 +08: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 16:20:52 +08:00
|
|
|
end
|
2023-09-29 22:11:56 +08:00
|
|
|
self.dirty[fragment.name] = true
|
2023-07-22 16:20:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-06 21:30:01 +08:00
|
|
|
function Spec:fix_cond()
|
2023-05-27 14:45:14 +08:00
|
|
|
for _, plugin in pairs(self.plugins) do
|
2023-07-06 21:30:01 +08: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 22:11:56 +08:00
|
|
|
local stack = { plugin }
|
|
|
|
while #stack > 0 do
|
|
|
|
local p = table.remove(stack)
|
2023-10-01 00:02:07 +08: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 22:11:56 +08:00
|
|
|
end
|
|
|
|
end
|
2023-07-06 21:30:01 +08:00
|
|
|
plugin.enabled = false
|
2023-05-27 14:45:14 +08:00
|
|
|
end
|
|
|
|
end
|
2023-07-06 21:30:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function Spec:fix_optional()
|
2023-05-23 14:57:53 +08: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 20:31:23 +08:00
|
|
|
|
2023-05-23 14:57:53 +08:00
|
|
|
-- handle optional plugins
|
|
|
|
for _, plugin in pairs(self.plugins) do
|
|
|
|
if plugin.optional and all_optional(plugin) then
|
2023-09-29 22:11:56 +08:00
|
|
|
-- remove all optional fragments
|
|
|
|
self:remove_fragments(plugin.name, { self = true })
|
2023-05-23 14:57:53 +08:00
|
|
|
self.plugins[plugin.name] = nil
|
|
|
|
end
|
2023-05-22 20:24:18 +08:00
|
|
|
end
|
|
|
|
end
|
2023-07-06 21:30:01 +08: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 22:11:56 +08:00
|
|
|
self:fix_optional()
|
2023-10-04 18:50:46 +08:00
|
|
|
self:rebuild()
|
|
|
|
|
2023-07-22 16:20:52 +08:00
|
|
|
self:fix_cond()
|
2023-10-04 18:50:46 +08:00
|
|
|
self:rebuild()
|
|
|
|
|
2023-10-04 17:48:14 +08:00
|
|
|
self.dirty = {}
|
2023-07-22 16:20:52 +08:00
|
|
|
|
2023-01-04 17:36:51 +08:00
|
|
|
for _, plugin in pairs(self.plugins) do
|
2023-09-29 22:11:56 +08:00
|
|
|
local disabled = plugin.enabled == false or (type(plugin.enabled) == "function" and not plugin.enabled())
|
|
|
|
if disabled then
|
2023-01-04 17:36:51 +08:00
|
|
|
plugin._.kind = "disabled"
|
2023-09-29 22:11:56 +08:00
|
|
|
-- remove all child fragments
|
|
|
|
self:remove_fragments(plugin.name, { self = false })
|
2023-01-04 17:36:51 +08:00
|
|
|
self.plugins[plugin.name] = nil
|
|
|
|
self.disabled[plugin.name] = plugin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-29 22:11:56 +08:00
|
|
|
-- rebuild any plugin specs that were modified
|
2023-10-04 18:50:46 +08:00
|
|
|
self:rebuild()
|
2023-01-04 17:36:51 +08:00
|
|
|
end
|
|
|
|
|
2023-01-02 16:44:09 +08:00
|
|
|
---@param msg string
|
|
|
|
---@param level number
|
2023-01-02 17:08:45 +08:00
|
|
|
function Spec:log(msg, level)
|
2023-01-02 16:44:09 +08:00
|
|
|
self.notifs[#self.notifs + 1] = { msg = msg, level = level, file = self.importing }
|
2023-01-02 17:08:45 +08:00
|
|
|
end
|
|
|
|
|
2023-01-02 19:53:35 +08:00
|
|
|
function Spec:report(level)
|
|
|
|
level = level or vim.log.levels.ERROR
|
2023-10-15 14:36:15 +08:00
|
|
|
local count = 0
|
2023-01-02 17:08:45 +08:00
|
|
|
for _, notif in ipairs(self.notifs) do
|
2023-01-02 19:53:35 +08:00
|
|
|
if notif.level >= level then
|
2023-01-10 15:42:20 +08:00
|
|
|
Util.notify(notif.msg, { level = notif.level })
|
2023-10-15 14:36:15 +08:00
|
|
|
count = count + 1
|
2023-01-02 19:53:35 +08:00
|
|
|
end
|
2023-01-02 17:08:45 +08:00
|
|
|
end
|
2023-10-15 14:36:15 +08:00
|
|
|
return count
|
2022-12-22 23:36:07 +08:00
|
|
|
end
|
|
|
|
|
2023-01-02 04:07:05 +08:00
|
|
|
---@param spec LazySpec|LazySpecImport
|
2022-11-26 05:48:17 +08:00
|
|
|
---@param results? string[]
|
2023-09-29 22:11:56 +08:00
|
|
|
function Spec:normalize(spec, results)
|
2022-11-26 05:48:17 +08:00
|
|
|
if type(spec) == "string" then
|
2023-09-29 22:11:56 +08:00
|
|
|
if not spec:find("/", 1, true) then
|
2022-12-22 23:36:07 +08:00
|
|
|
-- spec is a plugin name
|
2023-01-03 17:34:53 +08:00
|
|
|
if results then
|
|
|
|
table.insert(results, spec)
|
|
|
|
end
|
2022-12-22 23:36:07 +08:00
|
|
|
else
|
2023-09-29 22:11:56 +08:00
|
|
|
self:add({ spec }, results)
|
2022-12-22 23:36:07 +08:00
|
|
|
end
|
2022-11-26 05:48:17 +08:00
|
|
|
elseif #spec > 1 or Util.is_list(spec) then
|
2022-11-28 14:36:32 +08:00
|
|
|
---@cast spec LazySpec[]
|
2022-11-26 05:48:17 +08:00
|
|
|
for _, s in ipairs(spec) do
|
2023-09-29 22:11:56 +08:00
|
|
|
self:normalize(s, results)
|
2022-11-26 05:48:17 +08:00
|
|
|
end
|
2023-01-05 00:50:57 +08:00
|
|
|
elseif spec[1] or spec.dir or spec.url then
|
|
|
|
---@cast spec LazyPlugin
|
2023-09-29 22:11:56 +08:00
|
|
|
local plugin = self:add(spec, results)
|
2023-01-05 00:50:57 +08:00
|
|
|
---@diagnostic disable-next-line: cast-type-mismatch
|
|
|
|
---@cast plugin LazySpecImport
|
|
|
|
if plugin and plugin.import then
|
|
|
|
self:import(plugin)
|
|
|
|
end
|
2023-01-02 04:07:05 +08:00
|
|
|
elseif spec.import then
|
|
|
|
---@cast spec LazySpecImport
|
|
|
|
self:import(spec)
|
|
|
|
else
|
2023-01-05 00:50:57 +08:00
|
|
|
self:error("Invalid plugin spec " .. vim.inspect(spec))
|
2022-11-26 05:48:17 +08:00
|
|
|
end
|
|
|
|
return results
|
|
|
|
end
|
|
|
|
|
2023-01-02 04:07:05 +08:00
|
|
|
---@param spec LazySpecImport
|
|
|
|
function Spec:import(spec)
|
2023-01-04 05:49:04 +08:00
|
|
|
if spec.import == "lazy" then
|
|
|
|
return self:error("You can't name your plugins module `lazy`.")
|
|
|
|
end
|
2023-05-25 14:25:34 +08: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 16:20:39 +08:00
|
|
|
if vim.tbl_contains(self.modules, spec.import) then
|
|
|
|
return
|
|
|
|
end
|
2023-10-06 21:46:46 +08:00
|
|
|
if spec.cond == false or (type(spec.cond) == "function" and not spec.cond()) then
|
|
|
|
return
|
|
|
|
end
|
2023-01-02 04:07:05 +08:00
|
|
|
if spec.enabled == false or (type(spec.enabled) == "function" and not spec.enabled()) then
|
|
|
|
return
|
|
|
|
end
|
2023-01-02 16:44:09 +08:00
|
|
|
|
2023-01-02 19:53:35 +08:00
|
|
|
self.modules[#self.modules + 1] = spec.import
|
2023-01-02 16:44:09 +08:00
|
|
|
|
|
|
|
local imported = 0
|
2023-02-17 20:58:16 +08:00
|
|
|
|
|
|
|
---@type string[]
|
|
|
|
local modnames = {}
|
2024-05-26 22:43:52 +08:00
|
|
|
|
|
|
|
if spec.import == M.LOCAL_SPEC then
|
|
|
|
modnames = { spec.import }
|
|
|
|
else
|
|
|
|
Util.lsmod(spec.import, function(modname)
|
|
|
|
modnames[#modnames + 1] = modname
|
|
|
|
end)
|
|
|
|
table.sort(modnames)
|
|
|
|
end
|
2023-02-17 20:58:16 +08:00
|
|
|
|
|
|
|
for _, modname in ipairs(modnames) do
|
2023-01-02 16:44:09 +08:00
|
|
|
imported = imported + 1
|
|
|
|
Util.track({ import = modname })
|
|
|
|
self.importing = modname
|
2023-01-02 04:07:05 +08:00
|
|
|
-- unload the module so we get a clean slate
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
package.loaded[modname] = nil
|
|
|
|
Util.try(function()
|
2024-05-26 22:43:52 +08:00
|
|
|
local mod = nil
|
|
|
|
if modname == M.LOCAL_SPEC then
|
|
|
|
mod = M.local_spec()
|
|
|
|
else
|
|
|
|
mod = require(modname)
|
|
|
|
end
|
2023-04-22 20:10:06 +08:00
|
|
|
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 16:44:09 +08:00
|
|
|
self.importing = nil
|
|
|
|
Util.track()
|
2023-01-02 04:07:05 +08:00
|
|
|
end, {
|
|
|
|
msg = "Failed to load `" .. modname .. "`",
|
|
|
|
on_error = function(msg)
|
|
|
|
self:error(msg)
|
2023-01-02 16:44:09 +08:00
|
|
|
self.importing = nil
|
|
|
|
Util.track()
|
2023-01-02 04:07:05 +08:00
|
|
|
end,
|
|
|
|
})
|
2023-02-17 20:58:16 +08:00
|
|
|
end
|
2023-01-02 16:44:09 +08:00
|
|
|
if imported == 0 then
|
|
|
|
self:error("No specs found for module " .. spec.import)
|
|
|
|
end
|
2023-01-02 04:07:05 +08:00
|
|
|
end
|
|
|
|
|
2022-11-30 02:51:37 +08:00
|
|
|
---@param old LazyPlugin
|
|
|
|
---@param new LazyPlugin
|
|
|
|
---@return LazyPlugin
|
2022-11-30 07:18:59 +08:00
|
|
|
function Spec:merge(old, new)
|
2023-01-04 16:35:00 +08:00
|
|
|
new._.dep = old._.dep and new._.dep
|
|
|
|
|
2023-01-07 16:37:03 +08:00
|
|
|
if new.url and old.url and new.url ~= old.url then
|
2023-10-15 05:07:01 +08:00
|
|
|
self:warn("Two plugins with the same name and different url:\n" .. vim.inspect({ old = old, new = new }))
|
2023-01-07 16:37:03 +08:00
|
|
|
end
|
|
|
|
|
2023-01-12 20:07:51 +08:00
|
|
|
if new.dependencies and old.dependencies then
|
2023-01-17 21:00:33 +08:00
|
|
|
Util.extend(new.dependencies, old.dependencies)
|
2022-11-30 02:51:37 +08:00
|
|
|
end
|
2023-01-12 20:07:51 +08:00
|
|
|
|
2023-10-15 14:36:15 +08:00
|
|
|
local new_dir = new._.dir or old._.dir or (new.name and (Config.options.root .. "/" .. new.name)) or nil
|
2023-10-15 14:51:54 +08: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 14:36:15 +08:00
|
|
|
end
|
|
|
|
new.dir = new_dir
|
2023-10-15 14:51:54 +08:00
|
|
|
new._.rtp_loaded = new._.rtp_loaded or old._.rtp_loaded
|
2023-10-15 14:36:15 +08:00
|
|
|
|
2023-01-04 16:35:00 +08:00
|
|
|
new._.super = old
|
|
|
|
setmetatable(new, { __index = old })
|
2023-01-10 16:20:39 +08:00
|
|
|
|
2023-01-04 16:35:00 +08:00
|
|
|
return new
|
2022-11-30 02:51:37 +08:00
|
|
|
end
|
|
|
|
|
2022-11-30 07:18:59 +08:00
|
|
|
function M.update_state()
|
2023-02-28 18:51:16 +08:00
|
|
|
---@type string[]
|
|
|
|
local cloning = {}
|
|
|
|
|
2022-12-01 18:06:44 +08:00
|
|
|
---@type table<string,FileType>
|
|
|
|
local installed = {}
|
2022-12-03 22:48:06 +08:00
|
|
|
Util.ls(Config.options.root, function(_, name, type)
|
2022-12-20 14:19:55 +08:00
|
|
|
if type == "directory" and name ~= "readme" then
|
2022-12-01 18:06:44 +08:00
|
|
|
installed[name] = type
|
2023-02-28 18:51:16 +08:00
|
|
|
elseif type == "file" and name:sub(-8) == ".cloning" then
|
|
|
|
name = name:sub(1, -9)
|
|
|
|
cloning[#cloning + 1] = name
|
2022-12-01 18:06:44 +08:00
|
|
|
end
|
|
|
|
end)
|
2022-11-26 05:48:17 +08:00
|
|
|
|
2023-02-28 18:51:16 +08:00
|
|
|
for _, failed in ipairs(cloning) do
|
|
|
|
installed[failed] = nil
|
|
|
|
end
|
|
|
|
|
2022-11-30 07:18:59 +08:00
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2022-11-28 18:19:50 +08:00
|
|
|
plugin._ = plugin._ or {}
|
2022-12-01 18:06:44 +08:00
|
|
|
if plugin.lazy == nil then
|
2022-12-03 00:09:40 +08: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 18:06:44 +08:00
|
|
|
plugin.lazy = lazy and true or false
|
2022-11-29 05:03:44 +08:00
|
|
|
end
|
2022-12-20 14:15:26 +08:00
|
|
|
if plugin.dir:find(Config.options.root, 1, true) == 1 then
|
2022-12-03 22:31:21 +08:00
|
|
|
plugin._.installed = installed[plugin.name] ~= nil
|
2022-12-01 18:06:44 +08:00
|
|
|
installed[plugin.name] = nil
|
2022-12-13 17:09:33 +08:00
|
|
|
else
|
|
|
|
plugin._.is_local = true
|
|
|
|
plugin._.installed = true -- local plugins are managed by the user
|
2022-11-28 20:18:31 +08:00
|
|
|
end
|
2022-11-26 05:48:17 +08:00
|
|
|
end
|
|
|
|
|
2023-09-29 22:11:56 +08:00
|
|
|
for name in pairs(Config.spec.ignore_installed) do
|
|
|
|
installed[name] = nil
|
2023-07-06 21:30:01 +08:00
|
|
|
end
|
|
|
|
|
2022-11-30 07:18:59 +08:00
|
|
|
Config.to_clean = {}
|
2022-12-01 18:06:44 +08:00
|
|
|
for pack, dir_type in pairs(installed) do
|
|
|
|
table.insert(Config.to_clean, {
|
|
|
|
name = pack,
|
2022-12-03 22:48:06 +08:00
|
|
|
dir = Config.options.root .. "/" .. pack,
|
2022-12-01 18:06:44 +08:00
|
|
|
_ = {
|
2022-12-26 04:07:30 +08:00
|
|
|
kind = "clean",
|
2022-12-01 18:06:44 +08:00
|
|
|
installed = true,
|
|
|
|
is_symlink = dir_type == "link",
|
|
|
|
is_local = dir_type == "link",
|
|
|
|
},
|
|
|
|
})
|
2022-11-26 05:48:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-26 22:43:52 +08:00
|
|
|
function M.local_spec()
|
|
|
|
local filepath = vim.fn.fnamemodify(".lazy.lua", ":p")
|
|
|
|
local file = vim.secure.read(filepath)
|
|
|
|
if file then
|
|
|
|
return loadstring(file)()
|
|
|
|
end
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
2022-11-26 05:48:17 +08:00
|
|
|
function M.load()
|
2023-01-12 06:07:58 +08:00
|
|
|
M.loading = true
|
2022-11-26 05:48:17 +08:00
|
|
|
-- load specs
|
2022-11-30 07:18:59 +08:00
|
|
|
Util.track("spec")
|
2023-01-02 16:44:09 +08:00
|
|
|
Config.spec = Spec.new()
|
2024-05-26 22:43:52 +08:00
|
|
|
Config.spec:parse({
|
|
|
|
vim.deepcopy(Config.options.spec),
|
|
|
|
{
|
|
|
|
import = ".lazy.lua",
|
|
|
|
cond = function()
|
|
|
|
return Config.options.local_spec and vim.fn.filereadable(M.LOCAL_SPEC) == 1
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{ "folke/lazy.nvim" },
|
|
|
|
})
|
2022-12-01 23:28:25 +08:00
|
|
|
|
2022-12-29 07:49:38 +08:00
|
|
|
-- override some lazy props
|
2023-01-02 16:44:09 +08:00
|
|
|
local lazy = Config.spec.plugins["lazy.nvim"]
|
2023-01-03 17:34:53 +08: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-29 07:49:38 +08:00
|
|
|
end
|
2022-12-01 23:28:25 +08:00
|
|
|
|
2022-12-03 00:02:25 +08:00
|
|
|
local existing = Config.plugins
|
2023-01-02 16:44:09 +08:00
|
|
|
Config.plugins = Config.spec.plugins
|
2022-12-03 00:02:25 +08:00
|
|
|
-- copy state. This wont do anything during startup
|
|
|
|
for name, plugin in pairs(existing) do
|
|
|
|
if Config.plugins[name] then
|
2023-01-17 20:55:04 +08:00
|
|
|
local dep = Config.plugins[name]._.dep
|
|
|
|
local super = Config.plugins[name]._.super
|
2022-12-03 00:02:25 +08:00
|
|
|
Config.plugins[name]._ = plugin._
|
2023-01-17 20:55:04 +08:00
|
|
|
Config.plugins[name]._.dep = dep
|
|
|
|
Config.plugins[name]._.super = super
|
2022-12-03 00:02:25 +08:00
|
|
|
end
|
|
|
|
end
|
2022-11-30 07:18:59 +08:00
|
|
|
Util.track()
|
2022-11-26 05:48:17 +08:00
|
|
|
|
|
|
|
Util.track("state")
|
|
|
|
M.update_state()
|
|
|
|
Util.track()
|
2023-01-12 06:07:58 +08:00
|
|
|
M.loading = false
|
2023-05-25 14:09:19 +08:00
|
|
|
vim.api.nvim_exec_autocmds("User", { pattern = "LazyPlugins", modeline = false })
|
2022-11-26 05:48:17 +08:00
|
|
|
end
|
|
|
|
|
2022-12-02 16:22:15 +08:00
|
|
|
-- Finds the plugin that has this path
|
|
|
|
---@param path string
|
|
|
|
function M.find(path)
|
2023-01-02 21:35:36 +08:00
|
|
|
if not Config.spec then
|
|
|
|
return
|
|
|
|
end
|
2022-12-27 06:41:19 +08:00
|
|
|
local lua = path:find("/lua/", 1, true)
|
2022-12-16 20:06:30 +08: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 22:27:01 +08:00
|
|
|
return name and Config.plugins[name] or Config.spec.plugins[name] or nil
|
2022-12-16 20:06:30 +08:00
|
|
|
end
|
|
|
|
end
|
2022-12-02 16:22:15 +08:00
|
|
|
end
|
|
|
|
|
2023-01-04 05:50:14 +08: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-10-17 05:04:57 +08:00
|
|
|
-- Merges super values or runs the values function to override values or return new ones.
|
|
|
|
-- Values are cached for performance.
|
2023-01-12 20:07:51 +08:00
|
|
|
-- 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 21:05:16 +08:00
|
|
|
if not plugin[prop] then
|
|
|
|
return {}
|
|
|
|
end
|
2023-10-17 04:34:44 +08:00
|
|
|
plugin._.cache = plugin._.cache or {}
|
2023-10-16 21:05:16 +08:00
|
|
|
local key = prop .. (is_list and "_list" or "")
|
2023-10-17 04:34:44 +08:00
|
|
|
if plugin._.cache[key] == nil then
|
|
|
|
plugin._.cache[key] = M._values(plugin, plugin, prop, is_list)
|
2023-10-16 21:05:16 +08:00
|
|
|
end
|
2023-10-17 04:34:44 +08:00
|
|
|
return plugin._.cache[key]
|
2023-10-16 21:05:16 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Merges super values or runs the values function to override values or return new ones
|
|
|
|
-- Used for opts, cmd, event, ft and keys
|
2023-10-17 04:34:44 +08:00
|
|
|
---@param root LazyPlugin
|
2023-10-16 21:05:16 +08:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
---@param prop string
|
|
|
|
---@param is_list? boolean
|
2023-10-17 04:34:44 +08:00
|
|
|
function M._values(root, plugin, prop, is_list)
|
|
|
|
if not plugin[prop] then
|
|
|
|
return {}
|
|
|
|
end
|
2023-01-12 20:07:51 +08:00
|
|
|
---@type table
|
2023-10-17 04:34:44 +08:00
|
|
|
local ret = plugin._.super and M._values(root, plugin._.super, prop, is_list) or {}
|
2023-01-12 20:07:51 +08:00
|
|
|
local values = rawget(plugin, prop)
|
|
|
|
|
|
|
|
if not values then
|
|
|
|
return ret
|
|
|
|
elseif type(values) == "function" then
|
2023-10-17 04:34:44 +08:00
|
|
|
ret = values(root, ret) or ret
|
2023-01-12 20:07:51 +08:00
|
|
|
return type(ret) == "table" and ret or { ret }
|
|
|
|
end
|
|
|
|
|
|
|
|
values = type(values) == "table" and values or { values }
|
2023-01-17 21:00:33 +08:00
|
|
|
return is_list and Util.extend(ret, values) or Util.merge(ret, values)
|
2023-01-12 20:07:51 +08:00
|
|
|
end
|
|
|
|
|
2022-11-26 05:48:17 +08:00
|
|
|
return M
|