2022-11-25 21:48:17 +00:00
|
|
|
local Config = require("lazy.core.config")
|
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[]
|
2024-05-26 15:43:52 +01:00
|
|
|
M.LOCAL_SPEC = ".lazy.lua"
|
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
|
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 13:19:09 +00: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 07:36:15 +01:00
|
|
|
elseif plugin.dev == false then
|
2024-06-03 08:18:43 +01:00
|
|
|
-- explicitly select the default path
|
2023-10-15 07:36:15 +01:00
|
|
|
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
|
2024-06-16 14:52:21 +01:00
|
|
|
if plugin._.fpid then
|
|
|
|
local parent = self.fragments[plugin._.fpid]
|
|
|
|
if parent then
|
|
|
|
parent._.fdeps = vim.tbl_filter(function(fid)
|
|
|
|
return fid ~= plugin._.fid
|
|
|
|
end, parent._.fdeps)
|
|
|
|
self.dirty[parent.name] = true
|
|
|
|
end
|
|
|
|
end
|
2023-09-29 15:11:56 +01:00
|
|
|
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
|
2024-06-16 14:52:21 +01:00
|
|
|
self:rebuild()
|
2023-01-04 09:36:51 +00:00
|
|
|
|
2024-06-15 08:11:36 +01:00
|
|
|
-- check optional plugins again
|
|
|
|
self:fix_optional()
|
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
|
2024-06-16 06:09:33 +01:00
|
|
|
if type(spec.import) == "function" then
|
|
|
|
if not spec.name then
|
|
|
|
return self:error("Invalid import spec. Missing name: " .. vim.inspect(spec))
|
|
|
|
end
|
|
|
|
elseif type(spec.import) ~= "string" then
|
2023-05-25 07:25:34 +01:00
|
|
|
return self:error("Invalid import spec. `import` should be a string: " .. vim.inspect(spec))
|
|
|
|
end
|
2024-06-16 06:09:33 +01:00
|
|
|
|
|
|
|
local import_name = spec.name or spec.import
|
|
|
|
---@cast import_name string
|
|
|
|
|
|
|
|
if vim.tbl_contains(self.modules, import_name) then
|
2023-01-10 08:20:39 +00:00
|
|
|
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
|
|
|
|
2024-06-16 06:09:33 +01:00
|
|
|
self.modules[#self.modules + 1] = import_name
|
|
|
|
|
|
|
|
local import = spec.import
|
2023-01-02 08:44:09 +00:00
|
|
|
|
|
|
|
local imported = 0
|
2023-02-17 12:58:16 +00:00
|
|
|
|
2024-06-16 06:09:33 +01:00
|
|
|
---@type (string|(fun():LazyPluginSpec))[]
|
|
|
|
local modspecs = {}
|
2024-05-26 15:43:52 +01:00
|
|
|
|
2024-06-16 06:09:33 +01:00
|
|
|
if type(import) == "string" then
|
|
|
|
Util.lsmod(import, function(modname)
|
|
|
|
modspecs[#modspecs + 1] = modname
|
2024-05-26 15:43:52 +01:00
|
|
|
end)
|
2024-06-16 06:09:33 +01:00
|
|
|
table.sort(modspecs)
|
|
|
|
else
|
|
|
|
modspecs = { spec.import }
|
2024-05-26 15:43:52 +01:00
|
|
|
end
|
2023-02-17 12:58:16 +00:00
|
|
|
|
2024-06-16 06:09:33 +01:00
|
|
|
for _, modspec in ipairs(modspecs) do
|
2023-01-02 08:44:09 +00:00
|
|
|
imported = imported + 1
|
2024-06-16 06:09:33 +01:00
|
|
|
local modname = type(modspec) == "string" and modspec or import_name
|
|
|
|
Util.track({ import = modname })
|
2023-01-02 08:44:09 +00:00
|
|
|
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()
|
2024-06-16 06:09:33 +01:00
|
|
|
local mod = type(modspec) == "function" and modspec() or require(modspec)
|
2023-04-22 13:10:06 +01: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 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
|
|
|
|
|
2024-06-18 18:39:47 +01:00
|
|
|
require("lazy.manage.rocks").update_state()
|
|
|
|
|
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
|
|
|
|
|
2024-06-15 08:28:56 +01:00
|
|
|
---@param path string
|
|
|
|
function M.local_spec(path)
|
|
|
|
local file = vim.secure.read(path)
|
2024-05-26 15:43:52 +01:00
|
|
|
if file then
|
|
|
|
return loadstring(file)()
|
|
|
|
end
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
2024-06-16 06:09:33 +01:00
|
|
|
---@return LazySpecImport?
|
2024-06-15 08:28:56 +01:00
|
|
|
function M.find_local_spec()
|
|
|
|
if not Config.options.local_spec then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local path = vim.uv.cwd()
|
|
|
|
while path ~= "" do
|
|
|
|
local file = path .. "/" .. M.LOCAL_SPEC
|
|
|
|
if vim.fn.filereadable(file) == 1 then
|
2024-06-16 06:09:33 +01:00
|
|
|
return {
|
|
|
|
name = vim.fn.fnamemodify(file, ":~:."),
|
|
|
|
import = function()
|
|
|
|
local data = vim.secure.read(file)
|
|
|
|
if data then
|
|
|
|
return loadstring(data)()
|
|
|
|
end
|
|
|
|
return {}
|
|
|
|
end,
|
|
|
|
}
|
2024-06-15 08:28:56 +01:00
|
|
|
end
|
|
|
|
local p = vim.fn.fnamemodify(path, ":h")
|
|
|
|
if p == path then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
path = p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 21:48:17 +00:00
|
|
|
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()
|
2024-06-15 08:28:56 +01:00
|
|
|
|
2024-06-16 06:09:33 +01:00
|
|
|
local specs = {
|
2024-05-26 15:43:52 +01:00
|
|
|
vim.deepcopy(Config.options.spec),
|
2024-06-16 06:09:33 +01:00
|
|
|
}
|
|
|
|
specs[#specs + 1] = M.find_local_spec()
|
|
|
|
specs[#specs + 1] = { "folke/lazy.nvim" }
|
|
|
|
|
|
|
|
Config.spec:parse(specs)
|
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-10-16 22:04:57 +01:00
|
|
|
-- Merges super values or runs the values function to override values or return new ones.
|
|
|
|
-- Values are cached for performance.
|
2023-01-12 12:07:51 +00: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 14:05:16 +01:00
|
|
|
if not plugin[prop] then
|
|
|
|
return {}
|
|
|
|
end
|
2023-10-16 21:34:44 +01:00
|
|
|
plugin._.cache = plugin._.cache or {}
|
2023-10-16 14:05:16 +01:00
|
|
|
local key = prop .. (is_list and "_list" or "")
|
2023-10-16 21:34:44 +01:00
|
|
|
if plugin._.cache[key] == nil then
|
|
|
|
plugin._.cache[key] = M._values(plugin, plugin, prop, is_list)
|
2023-10-16 14:05:16 +01:00
|
|
|
end
|
2023-10-16 21:34:44 +01:00
|
|
|
return plugin._.cache[key]
|
2023-10-16 14:05:16 +01: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-16 21:34:44 +01:00
|
|
|
---@param root LazyPlugin
|
2023-10-16 14:05:16 +01:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
---@param prop string
|
|
|
|
---@param is_list? boolean
|
2023-10-16 21:34:44 +01:00
|
|
|
function M._values(root, plugin, prop, is_list)
|
|
|
|
if not plugin[prop] then
|
|
|
|
return {}
|
|
|
|
end
|
2023-01-12 12:07:51 +00:00
|
|
|
---@type table
|
2023-10-16 21:34:44 +01:00
|
|
|
local ret = plugin._.super and M._values(root, 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
|
2023-10-16 21:34:44 +01:00
|
|
|
ret = values(root, ret) or ret
|
2023-01-12 12:07:51 +00:00
|
|
|
return type(ret) == "table" and ret or { ret }
|
|
|
|
end
|
|
|
|
|
|
|
|
values = type(values) == "table" and values or { values }
|
2024-06-07 08:02:52 +01:00
|
|
|
if is_list then
|
|
|
|
return Util.extend(ret, values)
|
|
|
|
else
|
|
|
|
---@type {path:string[], list:any[]}[]
|
|
|
|
local lists = {}
|
|
|
|
for _, key in ipairs(plugin[prop .. "_extend"] or {}) do
|
|
|
|
local path = vim.split(key, ".", { plain = true })
|
|
|
|
local r = Util.key_get(ret, path)
|
|
|
|
local v = Util.key_get(values, path)
|
|
|
|
if type(r) == "table" and type(v) == "table" then
|
|
|
|
lists[key] = { path = path, list = {} }
|
|
|
|
vim.list_extend(lists[key].list, r)
|
|
|
|
vim.list_extend(lists[key].list, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local t = Util.merge(ret, values)
|
|
|
|
for _, list in pairs(lists) do
|
|
|
|
Util.key_set(t, list.path, list.list)
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
2023-01-12 12:07:51 +00:00
|
|
|
end
|
|
|
|
|
2022-11-25 21:48:17 +00:00
|
|
|
return M
|