2022-11-26 05:48:17 +08:00
|
|
|
local Config = require("lazy.core.config")
|
2024-06-23 04:18:26 +08:00
|
|
|
local Meta = require("lazy.core.meta")
|
2024-06-24 04:00:48 +08:00
|
|
|
local Pkg = require("lazy.pkg")
|
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
|
2024-06-23 04:18:26 +08:00
|
|
|
---@field meta LazyMeta
|
2022-11-26 05:48:17 +08:00
|
|
|
---@field plugins table<string, LazyPlugin>
|
2023-01-03 17:34:53 +08:00
|
|
|
---@field disabled table<string, LazyPlugin>
|
2023-09-29 22:11:56 +08:00
|
|
|
---@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
|
2024-05-26 22:43:52 +08:00
|
|
|
M.LOCAL_SPEC = ".lazy.lua"
|
2022-11-29 21:27:58 +08:00
|
|
|
|
|
|
|
---@param spec? LazySpec
|
2024-06-29 16:18:31 +08:00
|
|
|
---@param opts? {optional?:boolean, pkg?:boolean}
|
2023-05-23 14:57:53 +08:00
|
|
|
function Spec.new(spec, opts)
|
2024-06-23 04:18:26 +08:00
|
|
|
local self = setmetatable({}, Spec)
|
|
|
|
self.meta = Meta.new(self)
|
2023-01-03 17:34:53 +08:00
|
|
|
self.disabled = {}
|
2023-01-02 04:07:05 +08:00
|
|
|
self.modules = {}
|
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
|
2024-06-29 16:18:31 +08:00
|
|
|
if not (opts and opts.pkg == false) then
|
|
|
|
self.meta:load_pkgs()
|
|
|
|
end
|
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
|
|
|
|
2024-06-23 04:18:26 +08:00
|
|
|
function Spec:__index(key)
|
|
|
|
if Spec[key] then
|
|
|
|
return Spec[key]
|
|
|
|
end
|
|
|
|
if key == "plugins" then
|
|
|
|
self.meta:rebuild()
|
|
|
|
return self.meta.plugins
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-12 20:07:51 +08:00
|
|
|
function Spec:parse(spec)
|
|
|
|
self:normalize(spec)
|
2024-06-23 04:30:20 +08:00
|
|
|
self.meta:resolve()
|
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
|
2024-06-19 01:40:58 +08:00
|
|
|
---@return string
|
2022-12-13 17:09:33 +08:00
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
---@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
|
2024-06-23 04:18:26 +08:00
|
|
|
function Spec:normalize(spec)
|
2022-11-26 05:48:17 +08:00
|
|
|
if type(spec) == "string" then
|
2024-06-23 23:49:19 +08:00
|
|
|
self.meta:add({ spec })
|
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
|
2024-06-23 04:18:26 +08:00
|
|
|
self:normalize(s)
|
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
|
2024-06-23 04:18:26 +08:00
|
|
|
---@cast spec LazyPluginSpec
|
2024-06-23 23:49:19 +08:00
|
|
|
self.meta:add(spec)
|
2023-01-05 00:50:57 +08:00
|
|
|
---@diagnostic disable-next-line: cast-type-mismatch
|
2024-06-23 23:49:19 +08:00
|
|
|
---@cast spec LazySpecImport
|
|
|
|
if spec and spec.import then
|
|
|
|
self:import(spec)
|
2023-01-05 00:50:57 +08:00
|
|
|
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
|
|
|
|
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
|
2024-06-16 13:09:33 +08: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 14:25:34 +08:00
|
|
|
return self:error("Invalid import spec. `import` should be a string: " .. vim.inspect(spec))
|
|
|
|
end
|
2024-06-16 13:09:33 +08: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 16:20:39 +08:00
|
|
|
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
|
|
|
|
2024-06-16 13:09:33 +08:00
|
|
|
self.modules[#self.modules + 1] = import_name
|
|
|
|
|
|
|
|
local import = spec.import
|
2023-01-02 16:44:09 +08:00
|
|
|
|
|
|
|
local imported = 0
|
2023-02-17 20:58:16 +08:00
|
|
|
|
2024-06-16 13:09:33 +08:00
|
|
|
---@type (string|(fun():LazyPluginSpec))[]
|
|
|
|
local modspecs = {}
|
2024-05-26 22:43:52 +08:00
|
|
|
|
2024-06-16 13:09:33 +08:00
|
|
|
if type(import) == "string" then
|
2024-07-01 13:07:49 +08:00
|
|
|
Util.lsmod(import, function(modname, modpath)
|
2024-06-16 13:09:33 +08:00
|
|
|
modspecs[#modspecs + 1] = modname
|
2024-07-01 13:07:49 +08:00
|
|
|
package.preload[modname] = function()
|
|
|
|
return loadfile(modpath)()
|
|
|
|
end
|
2024-05-26 22:43:52 +08:00
|
|
|
end)
|
2024-06-16 13:09:33 +08:00
|
|
|
table.sort(modspecs)
|
|
|
|
else
|
|
|
|
modspecs = { spec.import }
|
2024-05-26 22:43:52 +08:00
|
|
|
end
|
2023-02-17 20:58:16 +08:00
|
|
|
|
2024-06-16 13:09:33 +08:00
|
|
|
for _, modspec in ipairs(modspecs) do
|
2023-01-02 16:44:09 +08:00
|
|
|
imported = imported + 1
|
2024-06-16 13:09:33 +08:00
|
|
|
local modname = type(modspec) == "string" and modspec or import_name
|
|
|
|
Util.track({ import = modname })
|
2023-01-02 16:44:09 +08:00
|
|
|
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-06-16 13:09:33 +08:00
|
|
|
local mod = type(modspec) == "function" and modspec() or require(modspec)
|
2023-04-22 20:10:06 +08:00
|
|
|
if type(mod) ~= "table" then
|
|
|
|
self.importing = nil
|
|
|
|
return self:error(
|
|
|
|
"Invalid spec module: `"
|
2024-06-24 23:36:59 +08:00
|
|
|
.. modname
|
|
|
|
.. "`\nExpected a `table` of specs, but a `"
|
|
|
|
.. type(mod)
|
|
|
|
.. "` was returned instead"
|
2023-04-22 20:10:06 +08:00
|
|
|
)
|
|
|
|
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 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
|
2024-06-24 23:36:59 +08:00
|
|
|
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
|
|
|
|
|
2024-06-24 20:14:41 +08:00
|
|
|
M.update_rocks_state()
|
2024-06-19 01:39:47 +08:00
|
|
|
|
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-06-24 20:14:41 +08:00
|
|
|
function M.update_rocks_state()
|
|
|
|
local root = Config.options.rocks.root
|
|
|
|
---@type table<string,string>
|
|
|
|
local installed = {}
|
|
|
|
Util.ls(root, function(_, name, type)
|
|
|
|
if type == "directory" then
|
|
|
|
installed[name] = name
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2024-06-27 01:38:45 +08:00
|
|
|
if plugin.build == "rockspec" or plugin.name == "hererocks" then
|
2024-06-24 20:14:41 +08:00
|
|
|
plugin._.build = not installed[plugin.name]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-16 13:09:33 +08:00
|
|
|
---@return LazySpecImport?
|
2024-06-15 15:28:56 +08:00
|
|
|
function M.find_local_spec()
|
|
|
|
if not Config.options.local_spec then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local path = vim.uv.cwd()
|
2024-06-26 02:53:42 +08:00
|
|
|
while path and path ~= "" do
|
2024-06-15 15:28:56 +08:00
|
|
|
local file = path .. "/" .. M.LOCAL_SPEC
|
|
|
|
if vim.fn.filereadable(file) == 1 then
|
2024-06-16 13:09:33 +08:00
|
|
|
return {
|
|
|
|
name = vim.fn.fnamemodify(file, ":~:."),
|
|
|
|
import = function()
|
|
|
|
local data = vim.secure.read(file)
|
|
|
|
if data then
|
2024-07-04 22:16:39 +08:00
|
|
|
return loadstring(data, M.LOCAL_SPEC)()
|
2024-06-16 13:09:33 +08:00
|
|
|
end
|
|
|
|
return {}
|
|
|
|
end,
|
|
|
|
}
|
2024-06-15 15:28:56 +08:00
|
|
|
end
|
|
|
|
local p = vim.fn.fnamemodify(path, ":h")
|
|
|
|
if p == path then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
path = p
|
|
|
|
end
|
|
|
|
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-06-15 15:28:56 +08:00
|
|
|
|
2024-06-16 13:09:33 +08:00
|
|
|
local specs = {
|
2024-06-23 04:18:26 +08:00
|
|
|
---@diagnostic disable-next-line: param-type-mismatch
|
2024-05-26 22:43:52 +08:00
|
|
|
vim.deepcopy(Config.options.spec),
|
2024-06-16 13:09:33 +08:00
|
|
|
}
|
|
|
|
specs[#specs + 1] = M.find_local_spec()
|
|
|
|
specs[#specs + 1] = { "folke/lazy.nvim" }
|
|
|
|
|
|
|
|
Config.spec:parse(specs)
|
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
|
|
|
|
2024-06-25 19:23:25 +08:00
|
|
|
-- add hererocks when enabled and needed
|
2024-06-29 17:44:31 +08:00
|
|
|
for _, plugin in pairs(Config.spec.plugins) do
|
|
|
|
if plugin.build == "rockspec" then
|
|
|
|
if Config.hererocks() then
|
2024-06-25 19:23:25 +08:00
|
|
|
Config.spec.meta:add({
|
|
|
|
"luarocks/hererocks",
|
|
|
|
build = "rockspec",
|
|
|
|
lazy = true,
|
|
|
|
})
|
|
|
|
end
|
2024-06-29 17:44:31 +08:00
|
|
|
break
|
2024-06-25 19:23:25 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
2024-06-24 20:14:41 +08:00
|
|
|
local new_state = Config.plugins[name]._
|
2022-12-03 00:02:25 +08:00
|
|
|
Config.plugins[name]._ = plugin._
|
2024-06-24 20:14:41 +08:00
|
|
|
Config.plugins[name]._.dep = new_state.dep
|
|
|
|
Config.plugins[name]._.frags = new_state.frags
|
2024-06-25 01:38:33 +08:00
|
|
|
Config.plugins[name]._.pkg = new_state.pkg
|
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()
|
2024-06-24 04:00:48 +08:00
|
|
|
|
|
|
|
if Config.options.pkg.enabled and Pkg.dirty then
|
|
|
|
Pkg.update()
|
|
|
|
return M.load()
|
|
|
|
end
|
|
|
|
|
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
|
2024-06-25 03:56:43 +08:00
|
|
|
---@param opts? {fast?:boolean}
|
|
|
|
function M.find(path, opts)
|
2023-01-02 21:35:36 +08:00
|
|
|
if not Config.spec then
|
|
|
|
return
|
|
|
|
end
|
2024-06-25 03:56:43 +08:00
|
|
|
opts = opts or {}
|
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)
|
2024-06-25 03:56:43 +08:00
|
|
|
if name then
|
|
|
|
if opts.fast then
|
|
|
|
return Config.spec.meta.plugins[name]
|
|
|
|
end
|
|
|
|
return Config.spec.plugins[name]
|
|
|
|
end
|
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
|
2024-06-27 00:31:31 +08:00
|
|
|
if task:has_errors() then
|
2023-01-04 05:50:14 +08:00
|
|
|
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
|
2024-06-23 04:18:26 +08:00
|
|
|
local super = getmetatable(plugin)
|
2023-01-12 20:07:51 +08:00
|
|
|
---@type table
|
2024-06-23 04:18:26 +08:00
|
|
|
local ret = super and M._values(root, super.__index, 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 }
|
2024-06-07 15:02:52 +08:00
|
|
|
if is_list then
|
|
|
|
return Util.extend(ret, values)
|
|
|
|
else
|
|
|
|
---@type {path:string[], list:any[]}[]
|
|
|
|
local lists = {}
|
2024-06-23 04:18:26 +08:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2024-06-07 15:02:52 +08:00
|
|
|
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 20:07:51 +08:00
|
|
|
end
|
|
|
|
|
2022-11-26 05:48:17 +08:00
|
|
|
return M
|