2022-11-26 05:48:17 +08:00
|
|
|
local Config = require("lazy.core.config")
|
|
|
|
local Util = require("lazy.core.util")
|
2022-11-30 02:58:23 +08:00
|
|
|
local Handler = require("lazy.core.handler")
|
2022-12-16 22:08:09 +08:00
|
|
|
local Cache = require("lazy.core.cache")
|
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 = {}
|
|
|
|
|
2022-11-28 14:36:32 +08:00
|
|
|
---@class LazySpecLoader
|
2022-11-26 05:48:17 +08:00
|
|
|
---@field plugins table<string, LazyPlugin>
|
2023-01-02 04:07:05 +08:00
|
|
|
---@field modules string[]
|
2022-12-22 23:36:07 +08:00
|
|
|
---@field errors string[]
|
2022-12-30 18:52:09 +08:00
|
|
|
---@field opts LazySpecOptions
|
2022-11-26 05:48:17 +08:00
|
|
|
local Spec = {}
|
2022-11-29 21:27:58 +08:00
|
|
|
M.Spec = Spec
|
|
|
|
|
2022-12-30 18:52:09 +08:00
|
|
|
---@alias LazySpecOptions {show_errors: boolean}
|
|
|
|
|
2022-11-29 21:27:58 +08:00
|
|
|
---@param spec? LazySpec
|
2022-12-30 18:52:09 +08:00
|
|
|
---@param opts? LazySpecOptions
|
|
|
|
function Spec.new(spec, opts)
|
2022-11-29 21:27:58 +08:00
|
|
|
local self = setmetatable({}, { __index = Spec })
|
2022-12-30 18:52:09 +08:00
|
|
|
self.opts = opts or {}
|
2022-11-29 21:27:58 +08:00
|
|
|
self.plugins = {}
|
2023-01-02 04:07:05 +08:00
|
|
|
self.modules = {}
|
2022-12-22 23:36:07 +08:00
|
|
|
self.errors = {}
|
2022-11-29 21:27:58 +08:00
|
|
|
if spec then
|
|
|
|
self:normalize(spec)
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
2022-11-26 05:48:17 +08:00
|
|
|
|
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
|
|
|
|
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
|
2022-11-30 02:51:37 +08:00
|
|
|
---@param is_dep? boolean
|
|
|
|
function Spec:add(plugin, is_dep)
|
2022-12-13 17:09:33 +08:00
|
|
|
if not plugin.url and plugin[1] then
|
|
|
|
plugin.url = Config.options.git.url_format:format(plugin[1])
|
2022-11-26 05:48:17 +08:00
|
|
|
end
|
2022-11-29 21:27:58 +08:00
|
|
|
|
2022-12-13 17:09:33 +08:00
|
|
|
if plugin.dir then
|
2022-12-20 14:19:55 +08:00
|
|
|
plugin.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
|
|
|
|
-- dev plugins
|
|
|
|
if plugin.dev then
|
|
|
|
plugin.dir = Config.options.dev.path .. "/" .. plugin.name
|
2022-11-29 21:27:58 +08:00
|
|
|
else
|
2022-12-13 17:09:33 +08:00
|
|
|
-- remote plugin
|
|
|
|
plugin.dir = Config.options.root .. "/" .. plugin.name
|
2022-11-29 21:27:58 +08:00
|
|
|
end
|
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))
|
2022-11-26 05:48:17 +08:00
|
|
|
end
|
|
|
|
|
2022-12-16 16:13:08 +08:00
|
|
|
plugin.event = type(plugin.event) == "string" and { plugin.event } or plugin.event
|
|
|
|
plugin.keys = type(plugin.keys) == "string" and { plugin.keys } or plugin.keys
|
|
|
|
plugin.cmd = type(plugin.cmd) == "string" and { plugin.cmd } or plugin.cmd
|
|
|
|
plugin.ft = type(plugin.ft) == "string" and { plugin.ft } or plugin.ft
|
|
|
|
|
2022-12-03 00:09:40 +08:00
|
|
|
plugin._ = {}
|
|
|
|
plugin._.dep = is_dep
|
2022-11-30 02:51:37 +08:00
|
|
|
|
2022-11-26 05:48:17 +08:00
|
|
|
local other = self.plugins[plugin.name]
|
2022-11-30 07:18:59 +08:00
|
|
|
self.plugins[plugin.name] = other and self:merge(other, plugin) or plugin
|
2022-11-26 05:48:17 +08:00
|
|
|
return self.plugins[plugin.name]
|
|
|
|
end
|
|
|
|
|
2022-12-22 23:36:07 +08:00
|
|
|
function Spec:error(error)
|
|
|
|
self.errors[#self.errors + 1] = error
|
2022-12-30 18:52:09 +08:00
|
|
|
if self.opts.show_errors ~= false then
|
|
|
|
Util.error(error)
|
|
|
|
end
|
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[]
|
2022-11-30 02:51:37 +08:00
|
|
|
---@param is_dep? boolean
|
|
|
|
function Spec:normalize(spec, results, is_dep)
|
2022-11-26 05:48:17 +08:00
|
|
|
results = results or {}
|
|
|
|
if type(spec) == "string" then
|
2022-12-22 23:36:07 +08:00
|
|
|
if is_dep and not spec:find("/", 1, true) then
|
|
|
|
-- spec is a plugin name
|
|
|
|
table.insert(results, spec)
|
|
|
|
else
|
|
|
|
table.insert(results, self:add({ spec }, is_dep).name)
|
|
|
|
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
|
2022-11-30 02:51:37 +08:00
|
|
|
self:normalize(s, results, is_dep)
|
2022-11-26 05:48:17 +08:00
|
|
|
end
|
2023-01-02 04:07:05 +08:00
|
|
|
elseif spec.import then
|
|
|
|
---@cast spec LazySpecImport
|
|
|
|
self:import(spec)
|
|
|
|
else
|
|
|
|
---@cast spec LazyPluginSpec
|
|
|
|
if spec.enabled == nil or spec.enabled == true or (type(spec.enabled) == "function" and spec.enabled()) then
|
|
|
|
local plugin
|
|
|
|
-- 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
|
|
|
|
if spec._ then
|
|
|
|
plugin = spec
|
|
|
|
else
|
|
|
|
---@cast spec LazyPlugin
|
2023-01-02 16:40:19 +08:00
|
|
|
spec.dependencies = spec.dependencies and self:normalize(spec.dependencies, {}, true) or nil
|
2023-01-02 04:07:05 +08:00
|
|
|
plugin = self:add(spec, is_dep)
|
|
|
|
end
|
|
|
|
table.insert(results, plugin.name)
|
2022-12-21 06:14:49 +08:00
|
|
|
end
|
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)
|
|
|
|
if spec.enabled == false or (type(spec.enabled) == "function" and not spec.enabled()) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
Util.lsmod(spec.import, function(modname)
|
|
|
|
-- unload the module so we get a clean slate
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
package.loaded[modname] = nil
|
|
|
|
Util.try(function()
|
|
|
|
self:normalize(Cache.require(modname))
|
|
|
|
self.modules[#self.modules + 1] = modname
|
|
|
|
end, {
|
|
|
|
msg = "Failed to load `" .. modname .. "`",
|
|
|
|
on_error = function(msg)
|
|
|
|
self:error(msg)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end)
|
|
|
|
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)
|
2022-12-03 00:09:40 +08:00
|
|
|
local is_dep = old._.dep and new._.dep
|
2022-11-30 02:51:37 +08:00
|
|
|
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
for k, v in pairs(new) do
|
2022-12-03 00:09:40 +08:00
|
|
|
if k == "_" then
|
2022-11-30 02:51:37 +08:00
|
|
|
elseif old[k] ~= nil and old[k] ~= v then
|
2022-12-04 06:15:50 +08:00
|
|
|
if Handler.types[k] then
|
2022-11-30 02:51:37 +08:00
|
|
|
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
|
2023-01-02 16:41:09 +08:00
|
|
|
elseif k == "config" or k == "priority" then
|
|
|
|
old[k] = v
|
|
|
|
elseif k == "dependencies" then
|
|
|
|
for _, dep in ipairs(v) do
|
|
|
|
if not vim.tbl_contains(old[k], dep) then
|
|
|
|
table.insert(old[k], dep)
|
|
|
|
end
|
|
|
|
end
|
2022-11-30 02:51:37 +08:00
|
|
|
else
|
2023-01-02 16:41:09 +08:00
|
|
|
old[k] = v
|
2022-12-22 23:36:07 +08:00
|
|
|
self:error("Merging plugins is not supported for key `" .. k .. "`\n" .. vim.inspect({ old = old, new = new }))
|
2022-11-30 02:51:37 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
old[k] = v
|
|
|
|
end
|
|
|
|
end
|
2022-12-03 00:09:40 +08:00
|
|
|
old._.dep = is_dep
|
2022-11-30 02:51:37 +08:00
|
|
|
return old
|
|
|
|
end
|
|
|
|
|
2022-11-30 07:18:59 +08:00
|
|
|
function M.update_state()
|
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
|
|
|
|
end
|
|
|
|
end)
|
2022-11-26 05:48:17 +08:00
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
2022-12-30 18:52:09 +08:00
|
|
|
---@param opts? LazySpecOptions
|
|
|
|
function M.spec(opts)
|
2023-01-02 04:07:05 +08:00
|
|
|
return Spec.new(vim.deepcopy(Config.spec), opts)
|
2022-11-26 05:48:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.load()
|
|
|
|
-- load specs
|
2022-11-30 07:18:59 +08:00
|
|
|
Util.track("spec")
|
|
|
|
local spec = M.spec()
|
2023-01-02 04:07:05 +08:00
|
|
|
Config.parsed = spec
|
2022-12-01 23:28:25 +08:00
|
|
|
|
|
|
|
-- add ourselves
|
2022-12-29 07:49:38 +08:00
|
|
|
spec:add({ "folke/lazy.nvim" })
|
|
|
|
-- override some lazy props
|
|
|
|
local lazy = spec.plugins["lazy.nvim"]
|
|
|
|
lazy.lazy = true
|
|
|
|
lazy.dir = Config.me
|
|
|
|
lazy.config = function()
|
|
|
|
error("lazy config should not be called")
|
|
|
|
end
|
|
|
|
lazy._.loaded = {}
|
2022-12-01 23:28:25 +08:00
|
|
|
|
2022-12-03 00:02:25 +08:00
|
|
|
local existing = Config.plugins
|
2022-11-30 07:18:59 +08:00
|
|
|
Config.plugins = 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
|
|
|
|
Config.plugins[name]._ = plugin._
|
|
|
|
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()
|
2022-12-29 07:49:38 +08:00
|
|
|
require("lazy.core.cache").indexed_unloaded = 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)
|
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)
|
|
|
|
return name and Config.plugins[name] or nil
|
|
|
|
end
|
|
|
|
end
|
2022-12-02 16:22:15 +08:00
|
|
|
end
|
|
|
|
|
2022-11-26 05:48:17 +08:00
|
|
|
return M
|