2022-11-25 21:48:17 +00:00
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
local Util = require("lazy.core.util")
|
2022-11-29 18:58:23 +00:00
|
|
|
local Handler = require("lazy.core.handler")
|
2022-11-25 21:48:17 +00:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
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-12-01 06:43:28 +00:00
|
|
|
---@field build? string|fun(LazyPlugin)
|
2022-11-28 06:36:32 +00:00
|
|
|
|
|
|
|
---@class LazyPluginState
|
2022-12-01 15:27:52 +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-12-02 16:09:40 +00:00
|
|
|
---@field dep? boolean True if this plugin is only in the spec as a dependency
|
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 19:19:07 +00:00
|
|
|
---@field pin? 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
|
|
|
|
---@field enabled? boolean|(fun():boolean)
|
2022-12-01 10:06:44 +00:00
|
|
|
---@field lazy? boolean
|
2022-12-02 18:17:38 +00:00
|
|
|
---@field local? 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-12-02 08:22:15 +00:00
|
|
|
---@alias LazySpec string|LazyPlugin|LazyPlugin[]|{dependencies:LazySpec}
|
2022-11-28 06:36:32 +00:00
|
|
|
|
|
|
|
---@class LazySpecLoader
|
2022-11-25 21:48:17 +00:00
|
|
|
---@field plugins table<string, LazyPlugin>
|
|
|
|
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 = {}
|
|
|
|
if spec then
|
|
|
|
self:normalize(spec)
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
2022-11-25 21:48:17 +00:00
|
|
|
|
|
|
|
---@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)
|
2022-12-01 07:40:17 +00:00
|
|
|
elseif c == "/" or pkg:sub(1, 4) == "http" or pkg:sub(1, 3) == "ssh" then
|
2022-11-29 13:27:58 +00:00
|
|
|
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-12-02 16:09:40 +00:00
|
|
|
plugin._ = {}
|
|
|
|
plugin._.dep = is_dep
|
2022-11-29 18:51:37 +00:00
|
|
|
|
2022-11-29 23:18:59 +00:00
|
|
|
-- check for plugins that should be local
|
2022-11-30 22:08:00 +00:00
|
|
|
for _, pattern in ipairs(Config.options.dev.patterns) do
|
2022-12-02 18:17:38 +00:00
|
|
|
if plugin["local"] or plugin[1]:find(pattern, 1, true) then
|
2022-11-30 22:08:00 +00:00
|
|
|
plugin.uri = Config.options.dev.path .. "/" .. plugin.name
|
2022-11-29 23:18:59 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 21:48:17 +00:00
|
|
|
local other = self.plugins[plugin.name]
|
2022-11-29 23:18:59 +00:00
|
|
|
self.plugins[plugin.name] = other and self: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-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)
|
2022-12-02 16:09:40 +00:00
|
|
|
local is_dep = old._.dep and new._.dep
|
2022-11-29 18:51:37 +00:00
|
|
|
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
for k, v in pairs(new) do
|
2022-12-02 16:09:40 +00:00
|
|
|
if k == "_" then
|
2022-11-29 18:51:37 +00:00
|
|
|
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
|
2022-12-02 16:09:40 +00:00
|
|
|
old._.dep = is_dep
|
2022-11-29 18:51:37 +00:00
|
|
|
return old
|
|
|
|
end
|
|
|
|
|
2022-11-29 23:18:59 +00:00
|
|
|
function M.update_state()
|
2022-12-01 10:06:44 +00:00
|
|
|
---@type table<string,FileType>
|
|
|
|
local installed = {}
|
2022-12-01 12:33:55 +00:00
|
|
|
Util.ls(Config.root, function(_, name, type)
|
2022-12-01 10:06:44 +00:00
|
|
|
if type == "directory" or type == "link" then
|
|
|
|
installed[name] = type
|
|
|
|
end
|
|
|
|
end)
|
2022-11-25 21:48:17 +00:00
|
|
|
|
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-01 12:33:55 +00:00
|
|
|
plugin.dir = Config.root .. "/" .. 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"
|
2022-12-01 10:06:44 +00:00
|
|
|
plugin._.is_symlink = installed[plugin.name] == "link"
|
|
|
|
plugin._.installed = installed[plugin.name] ~= nil
|
2022-11-28 12:18:31 +00:00
|
|
|
if plugin._.is_local == plugin._.is_symlink then
|
2022-12-01 10:06:44 +00:00
|
|
|
installed[plugin.name] = nil
|
2022-11-28 12:18:31 +00:00
|
|
|
end
|
2022-11-25 21:48:17 +00: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-01 12:33:55 +00:00
|
|
|
dir = Config.root .. "/" .. pack,
|
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
|
|
|
|
|
2022-11-29 23:18:59 +00:00
|
|
|
function M.spec()
|
|
|
|
local spec = Spec.new()
|
2022-11-25 21:48:17 +00:00
|
|
|
|
2022-12-01 12:33:55 +00:00
|
|
|
if type(Config.spec) == "string" then
|
|
|
|
-- spec is a module
|
2022-12-02 08:22:15 +00:00
|
|
|
local function _load(name)
|
|
|
|
local modname = name and (Config.spec .. "." .. name) or Config.spec
|
2022-12-02 16:02:25 +00:00
|
|
|
package.loaded[modname] = nil
|
2022-12-01 12:33:55 +00:00
|
|
|
Util.try(function()
|
2022-12-02 08:22:15 +00:00
|
|
|
spec:normalize(require(modname))
|
2022-12-01 12:33:55 +00:00
|
|
|
end, "Failed to load **" .. modname .. "**")
|
|
|
|
end
|
|
|
|
local path_plugins = vim.fn.stdpath("config") .. "/lua/" .. Config.spec:gsub("%.", "/")
|
|
|
|
|
2022-12-02 08:22:15 +00:00
|
|
|
_load()
|
2022-12-01 12:33:55 +00:00
|
|
|
Util.lsmod(path_plugins, _load)
|
|
|
|
else
|
|
|
|
-- spec is a spec
|
|
|
|
spec:normalize(Config.spec)
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
2022-11-29 23:18:59 +00:00
|
|
|
return spec
|
2022-11-25 21:48:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.load()
|
|
|
|
-- load specs
|
2022-11-29 23:18:59 +00:00
|
|
|
Util.track("spec")
|
|
|
|
local spec = M.spec()
|
2022-12-01 15:28:25 +00:00
|
|
|
|
|
|
|
-- add ourselves
|
|
|
|
spec.plugins["lazy.nvim"] = nil
|
|
|
|
spec:add({ "folke/lazy.nvim", lazy = true })
|
|
|
|
|
2022-12-02 16:02:25 +00:00
|
|
|
local existing = Config.plugins
|
2022-11-29 23:18:59 +00:00
|
|
|
Config.plugins = 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
|
|
|
|
Config.plugins[name]._ = plugin._
|
|
|
|
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()
|
|
|
|
end
|
|
|
|
|
2022-12-02 08:22:15 +00:00
|
|
|
-- Finds the plugin that has this path
|
|
|
|
---@param path string
|
|
|
|
function M.find(path)
|
|
|
|
if path:find(Config.root, 1, true) == 1 then
|
|
|
|
local plugin = path:sub(#Config.root + 2)
|
|
|
|
local idx = plugin:find("/", 1, true)
|
|
|
|
return idx and Config.plugins[plugin:sub(1, idx - 1)] or nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 21:48:17 +00:00
|
|
|
return M
|