lazy.nvim/lua/lazy/plugin.lua

137 lines
3.6 KiB
Lua
Raw Normal View History

local Config = require("lazy.core.config")
local Util = require("lazy.core.util")
local Module = require("lazy.core.module")
2022-11-21 05:33:47 +08:00
local M = {}
---@class LazyPlugin
---@field [1] string
---@field name string display name and name used for plugin config files
---@field pack string package name
2022-11-21 05:33:47 +08:00
---@field uri string
---@field modname? string
2022-11-21 05:33:47 +08:00
---@field branch? string
---@field dir string
---@field opt? boolean
---@field init? fun(LazyPlugin) Will always be run
---@field config? fun(LazyPlugin) Will be executed when loading the plugin
---@field event? string|string[]
---@field cmd? string|string[]
---@field ft? string|string[]
---@field module? string|string[]
---@field keys? string|string[]
---@field requires? string[]
2022-11-23 23:07:57 +08:00
---@field loaded? {[string]:string, time:number}
2022-11-21 05:33:47 +08:00
---@field installed? boolean
---@field run? string|fun()
2022-11-21 06:25:56 +08:00
---@field tasks? LazyTask[]
2022-11-21 05:33:47 +08:00
---@field dirty? boolean
2022-11-23 23:07:57 +08:00
---@field updated? {from:string, to:string}
2022-11-21 05:33:47 +08:00
---@param plugin LazyPlugin
function M.plugin(plugin)
2022-11-21 05:34:55 +08:00
local pkg = plugin[1]
if type(pkg) ~= "string" then
Util.error("Invalid plugin spec " .. vim.inspect(plugin))
end
2022-11-21 05:33:47 +08:00
2022-11-21 05:34:55 +08:00
plugin.uri = plugin.uri or ("https://github.com/" .. pkg .. ".git")
plugin.pack = plugin.pack or plugin.name
2022-11-21 05:33:47 +08:00
2022-11-21 05:34:55 +08:00
if not plugin.name then
local name = plugin.uri:gsub("%.git$", ""):match("/([^/]+)$")
plugin.pack = name
if not name then
name = pkg:gsub("%W+", "_")
end
name = name:gsub("[%.%-]n?vim$", "")
name = name:gsub("^n?vim[%-%.]", "")
name = name:gsub("%.lua$", "")
name = name:gsub("%.", "_")
plugin.name = name:lower()
end
2022-11-21 05:33:47 +08:00
2022-11-21 07:27:28 +08:00
if Config.plugins[plugin.name] and Config.plugins[plugin.name] ~= plugin then
for k, v in pairs(plugin) do
2022-11-21 05:34:55 +08:00
Config.plugins[plugin.name][k] = v
end
return Config.plugins[plugin.name]
else
Config.plugins[plugin.name] = plugin
end
return plugin
2022-11-21 05:33:47 +08:00
end
---@param plugin LazyPlugin
function M.process_local(plugin)
2022-11-21 05:34:55 +08:00
for _, pattern in ipairs(Config.options.plugins_local.patterns) do
if plugin[1]:find(pattern) then
plugin.uri = Config.options.plugins_local.path .. "/" .. plugin.pack
return
end
end
2022-11-21 05:33:47 +08:00
end
---@param plugin LazyPlugin
function M.process_config(plugin)
2022-11-21 05:34:55 +08:00
local name = plugin.name
local modname = Config.options.plugins .. "." .. name
2022-11-21 05:33:47 +08:00
local spec = Module.load(modname)
if spec then
-- add to loaded modules
if spec.requires then
spec.requires = M.normalize(spec.requires)
end
2022-11-21 05:33:47 +08:00
---@diagnostic disable-next-line: no-unknown
for k, v in pairs(spec) do
2022-11-21 05:34:55 +08:00
---@diagnostic disable-next-line: no-unknown
plugin[k] = v
2022-11-21 05:34:55 +08:00
end
plugin.modname = modname
M.plugin(plugin)
2022-11-21 05:34:55 +08:00
end
2022-11-21 05:33:47 +08:00
end
---@param spec table
---@param results? LazyPlugin[]
function M.normalize(spec, results)
2022-11-21 05:34:55 +08:00
results = results or {}
if type(spec) == "string" then
table.insert(results, M.plugin({ spec }).name)
elseif #spec > 1 or vim.tbl_islist(spec) then
---@cast spec LazyPlugin[]
for _, s in ipairs(spec) do
M.normalize(s, results)
end
else
---@cast spec LazyPlugin
spec = M.plugin(spec)
2022-11-21 05:33:47 +08:00
2022-11-21 05:34:55 +08:00
if spec.requires then
-- TODO: fix multiple requires in different packages
spec.requires = M.normalize(spec.requires)
end
table.insert(results, spec.name)
end
return results
2022-11-21 05:33:47 +08:00
end
function M.process()
2022-11-21 05:34:55 +08:00
for _, plugin in pairs(Config.plugins) do
M.process_config(plugin)
end
2022-11-21 05:33:47 +08:00
2022-11-21 05:34:55 +08:00
for _, plugin in pairs(Config.plugins) do
if plugin.opt == nil then
plugin.opt = Config.options.opt
end
plugin.dir = Config.options.package_path .. "/" .. (plugin.opt and "opt" or "start") .. "/" .. plugin.pack
plugin.installed = Util.file_exists(plugin.dir)
M.process_local(plugin)
end
2022-11-21 05:33:47 +08:00
end
return M