refactor: moved all plugin state to Plugin._

This commit is contained in:
Folke Lemaitre 2022-11-28 11:19:50 +01:00
parent 352dbadcb6
commit 28af1e1ac3
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
9 changed files with 49 additions and 46 deletions

View File

@ -46,14 +46,14 @@ function M.load(plugins, reason, opts)
plugin = type(plugin) == "string" and Config.plugins[plugin] or plugin plugin = type(plugin) == "string" and Config.plugins[plugin] or plugin
---@cast plugin LazyPlugin ---@cast plugin LazyPlugin
if not plugin.loaded then if not plugin._.loaded then
---@diagnostic disable-next-line: assign-type-mismatch ---@diagnostic disable-next-line: assign-type-mismatch
plugin.loaded = {} plugin._.loaded = {}
for k, v in pairs(reason) do for k, v in pairs(reason) do
plugin.loaded[k] = v plugin._.loaded[k] = v
end end
if #M.loading > 0 then if #M.loading > 0 then
plugin.loaded.plugin = M.loading[#M.loading].name plugin._.loaded.plugin = M.loading[#M.loading].name
end end
table.insert(M.loading, plugin) table.insert(M.loading, plugin)
@ -69,7 +69,7 @@ function M.load(plugins, reason, opts)
Util.try(plugin.config, "Failed to run `config` for " .. plugin.name) Util.try(plugin.config, "Failed to run `config` for " .. plugin.name)
end end
plugin.loaded.time = Util.track().time plugin._.loaded.time = Util.track().time
table.remove(M.loading) table.remove(M.loading)
vim.schedule(function() vim.schedule(function()
vim.cmd("do User LazyRender") vim.cmd("do User LazyRender")

View File

@ -6,7 +6,7 @@ local Cache = require("lazy.core.cache")
local M = {} local M = {}
---@alias CachedPlugin LazyPlugin | {_funs: string[]} ---@alias CachedPlugin LazyPlugin | {_funs: string[]}
local skip = { installed = true, loaded = true, tasks = true, dirty = true, dir = true } local skip = { _ = true, dir = true }
local funs = { config = true, init = true, run = true } local funs = { config = true, init = true, run = true }
M.dirty = false M.dirty = false
@ -29,7 +29,7 @@ M.dirty = false
---@field commit? string ---@field commit? string
---@field version? string ---@field version? string
---@class LazyPlugin: LazyPluginHandlers,LazyPluginHooks,LazyPluginState,LazyPluginRef ---@class LazyPlugin: LazyPluginHandlers,LazyPluginHooks,LazyPluginRef
---@field [1] string ---@field [1] string
---@field name string display name and name used for plugin config files ---@field name string display name and name used for plugin config files
---@field uri string ---@field uri string
@ -37,6 +37,7 @@ M.dirty = false
---@field enabled? boolean|(fun():boolean) ---@field enabled? boolean|(fun():boolean)
---@field opt? boolean ---@field opt? boolean
---@field requires? string[] ---@field requires? string[]
---@field _ LazyPluginState
---@alias LazySpec string|LazyPlugin|LazySpec[]|{requires:LazySpec} ---@alias LazySpec string|LazyPlugin|LazySpec[]|{requires:LazySpec}
@ -69,6 +70,7 @@ function Spec:add(plugin)
Util.error("Invalid plugin spec " .. vim.inspect(plugin)) Util.error("Invalid plugin spec " .. vim.inspect(plugin))
end end
plugin.uri = plugin.uri or ("https://github.com/" .. plugin[1] .. ".git") plugin.uri = plugin.uri or ("https://github.com/" .. plugin[1] .. ".git")
plugin._ = {}
-- PERF: optimized code to get package name without using lua patterns -- PERF: optimized code to get package name without using lua patterns
if not plugin.name then if not plugin.name then
@ -133,11 +135,12 @@ function M.update_state(check_clean)
end end
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.plugins) do
plugin._ = plugin._ or {}
plugin[1] = plugin["1"] or plugin[1] plugin[1] = plugin["1"] or plugin[1]
plugin.opt = plugin.opt == nil and Config.options.opt or plugin.opt plugin.opt = plugin.opt == nil and Config.options.opt or plugin.opt
local opt = plugin.opt and "opt" or "start" local opt = plugin.opt and "opt" or "start"
plugin.dir = Config.options.package_path .. "/" .. opt .. "/" .. plugin.name plugin.dir = Config.options.package_path .. "/" .. opt .. "/" .. plugin.name
plugin.installed = installed[opt][plugin.name] == true plugin._.installed = installed[opt][plugin.name] == true
installed[opt][plugin.name] = nil installed[opt][plugin.name] = nil
end end

View File

@ -27,7 +27,7 @@ function M.setup(opts)
Util.track("install") Util.track("install")
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.plugins) do
if not plugin.installed then if not plugin._.installed then
vim.cmd("do User LazyInstallPre") vim.cmd("do User LazyInstallPre")
require("lazy.manage").install({ require("lazy.manage").install({
wait = true, wait = true,
@ -49,7 +49,7 @@ function M.setup(opts)
Loader.init_plugins() Loader.init_plugins()
if Config.plugins["lazy.nvim"] then if Config.plugins["lazy.nvim"] then
Config.plugins["lazy.nvim"].loaded.time = lazy_delta Config.plugins["lazy.nvim"]._.loaded.time = lazy_delta
end end
vim.cmd("do User LazyDone") vim.cmd("do User LazyDone")
@ -64,7 +64,7 @@ function M.stats()
for _, plugin in pairs(require("lazy.core.config").plugins) do for _, plugin in pairs(require("lazy.core.config").plugins) do
ret.count = ret.count + 1 ret.count = ret.count + 1
if plugin.loaded then if plugin._.loaded then
ret.loaded = ret.loaded + 1 ret.loaded = ret.loaded + 1
end end
end end

View File

@ -49,7 +49,7 @@ function M.install(opts)
M.run({ M.run({
pipeline = { "git.install", { "plugin.docs", "plugin.run" } }, pipeline = { "git.install", { "plugin.docs", "plugin.run" } },
plugins = function(plugin) plugins = function(plugin)
return plugin.uri and not plugin.installed return plugin.uri and not plugin._.installed
end, end,
}, opts) }, opts)
end end
@ -59,7 +59,7 @@ function M.update(opts)
M.run({ M.run({
pipeline = { "git.update", { "plugin.docs", "plugin.run" }, "git.log" }, pipeline = { "git.update", { "plugin.docs", "plugin.run" }, "git.log" },
plugins = function(plugin) plugins = function(plugin)
return plugin.uri and plugin.installed return plugin.uri and plugin._.installed
end, end,
}, opts) }, opts)
end end
@ -69,7 +69,7 @@ function M.log(opts)
M.run({ M.run({
pipeline = { "git.log" }, pipeline = { "git.log" },
plugins = function(plugin) plugins = function(plugin)
return plugin.uri and plugin.installed return plugin.uri and plugin._.installed
end, end,
}, opts) }, opts)
end end
@ -86,13 +86,13 @@ end
function M.clear() function M.clear()
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.plugins) do
-- clear updated status -- clear updated status
plugin.updated = nil plugin._.updated = nil
-- clear finished tasks -- clear finished tasks
if plugin.tasks then if plugin._.tasks then
---@param task LazyTask ---@param task LazyTask
plugin.tasks = vim.tbl_filter(function(task) plugin._.tasks = vim.tbl_filter(function(task)
return task:is_running() return task:is_running()
end, plugin.tasks) end, plugin._.tasks)
end end
end end
vim.cmd([[do User LazyRender]]) vim.cmd([[do User LazyRender]])

View File

@ -8,7 +8,7 @@ M.log = {
if opts.interactive ~= true or not Util.file_exists(plugin.dir .. "/.git") then if opts.interactive ~= true or not Util.file_exists(plugin.dir .. "/.git") then
return false return false
end end
return plugin.updated == nil or plugin.updated.from ~= plugin.updated.to return plugin._.updated == nil or plugin._.updated.from ~= plugin._.updated.to
end, end,
run = function(self) run = function(self)
local args = { local args = {
@ -20,8 +20,8 @@ M.log = {
"--color=never", "--color=never",
} }
if self.plugin.updated then if self.plugin._.updated then
table.insert(args, self.plugin.updated.from .. ".." .. (self.plugin.updated.to or "HEAD")) table.insert(args, self.plugin._.updated.from .. ".." .. (self.plugin._.updated.to or "HEAD"))
else else
table.insert(args, "--since=7 days ago") table.insert(args, "--since=7 days ago")
end end
@ -59,11 +59,11 @@ M.update = {
on_exit = function(ok) on_exit = function(ok)
if ok then if ok then
local git_new = assert(Util.git_info(self.plugin.dir)) local git_new = assert(Util.git_info(self.plugin.dir))
self.plugin.updated = { self.plugin._.updated = {
from = git.hash, from = git.hash,
to = git_new.hash, to = git_new.hash,
} }
self.plugin.dirty = not vim.deep_equal(git, git_new) self.plugin._.dirty = not vim.deep_equal(git, git_new)
end end
end, end,
}) })
@ -103,8 +103,8 @@ M.install = {
args = args, args = args,
on_exit = function(ok) on_exit = function(ok)
if ok then if ok then
self.plugin.installed = true self.plugin._.installed = true
self.plugin.dirty = true self.plugin._.dirty = true
end end
end, end,
}) })

View File

@ -39,8 +39,8 @@ function Task.new(plugin, type, task, opts)
self.type = type self.type = type
self.output = "" self.output = ""
self.status = "" self.status = ""
plugin.tasks = plugin.tasks or {} plugin._.tasks = plugin._.tasks or {}
table.insert(plugin.tasks, self) table.insert(plugin._.tasks, self)
return self return self
end end

View File

@ -6,7 +6,7 @@ local M = {}
M.run = { M.run = {
needed = function(plugin) needed = function(plugin)
return plugin.dirty and (plugin.opt == false or plugin.run) return plugin._.dirty and (plugin.opt == false or plugin.run)
end, end,
run = function(self) run = function(self)
Loader.load(self.plugin, { task = "run" }, { load_start = true }) Loader.load(self.plugin, { task = "run" }, { load_start = true })
@ -47,13 +47,13 @@ M.clean = {
vim.loop.fs_unlink(dir) vim.loop.fs_unlink(dir)
end end
self.plugin.installed = false self.plugin._.installed = false
end, end,
} }
M.docs = { M.docs = {
needed = function(plugin) needed = function(plugin)
return plugin.dirty return plugin._.dirty
end, end,
run = function(self) run = function(self)
local docs = self.plugin.dir .. "/doc/" local docs = self.plugin.dir .. "/doc/"

View File

@ -47,8 +47,8 @@ function M:update()
} }
for _, plugin in ipairs(self.plugins) do for _, plugin in ipairs(self.plugins) do
if plugin.tasks then if plugin._.tasks then
for _, task in ipairs(plugin.tasks) do for _, task in ipairs(plugin._.tasks) do
self.progress.total = self.progress.total + 1 self.progress.total = self.progress.total + 1
if not task:is_running() then if not task:is_running() then
self.progress.done = self.progress.done + 1 self.progress.done = self.progress.done + 1
@ -144,7 +144,7 @@ end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M:reason(plugin) function M:reason(plugin)
local reason = vim.deepcopy(plugin.loaded or {}) local reason = vim.deepcopy(plugin._.loaded or {})
---@type string? ---@type string?
local source = reason.source local source = reason.source
if source then if source then
@ -203,18 +203,18 @@ end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M:diagnostics(plugin) function M:diagnostics(plugin)
if plugin.updated then if plugin._.updated then
if plugin.updated.from == plugin.updated.to then if plugin._.updated.from == plugin._.updated.to then
self:diagnostic({ self:diagnostic({
message = "already up to date", message = "already up to date",
}) })
else else
self:diagnostic({ self:diagnostic({
message = "updated from " .. plugin.updated.from:sub(1, 7) .. " to " .. plugin.updated.to:sub(1, 7), message = "updated from " .. plugin._.updated.from:sub(1, 7) .. " to " .. plugin._.updated.to:sub(1, 7),
}) })
end end
end end
for _, task in ipairs(plugin.tasks or {}) do for _, task in ipairs(plugin._.tasks or {}) do
if task:is_running() then if task:is_running() then
self:diagnostic({ self:diagnostic({
severity = vim.diagnostic.severity.WARN, severity = vim.diagnostic.severity.WARN,
@ -233,7 +233,7 @@ end
function M:plugin(plugin) function M:plugin(plugin)
self:append(" - ", "LazySpecial"):append(plugin.name) self:append(" - ", "LazySpecial"):append(plugin.name)
local plugin_start = self:row() local plugin_start = self:row()
if plugin.loaded then if plugin._.loaded then
self:reason(plugin) self:reason(plugin)
end end
self:diagnostics(plugin) self:diagnostics(plugin)
@ -248,7 +248,7 @@ end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
function M:tasks(plugin) function M:tasks(plugin)
for _, task in ipairs(plugin.tasks or {}) do for _, task in ipairs(plugin._.tasks or {}) do
if task.type == "log" and not task.error then if task.type == "log" and not task.error then
self:log(task) self:log(task)
elseif task.error or self._details == plugin.name then elseif task.error or self._details == plugin.name then

View File

@ -1,8 +1,8 @@
---@param plugin LazyPlugin ---@param plugin LazyPlugin
---@param filter fun(task:LazyTask):boolean? ---@param filter fun(task:LazyTask):boolean?
local function has_task(plugin, filter) local function has_task(plugin, filter)
if plugin.tasks then if plugin._.tasks then
for _, task in ipairs(plugin.tasks) do for _, task in ipairs(plugin._.tasks) do
if filter(task) then if filter(task) then
return true return true
end end
@ -49,7 +49,7 @@ return {
{ {
---@param plugin LazyPlugin ---@param plugin LazyPlugin
filter = function(plugin) filter = function(plugin)
return plugin.updated and plugin.updated.from ~= plugin.updated.to return plugin._.updated and plugin._.updated.from ~= plugin._.updated.to
end, end,
title = "Updated", title = "Updated",
}, },
@ -63,25 +63,25 @@ return {
}, },
{ {
filter = function(plugin) filter = function(plugin)
return plugin.installed and not plugin.uri return plugin._.installed and not plugin.uri
end, end,
title = "Clean", title = "Clean",
}, },
{ {
filter = function(plugin) filter = function(plugin)
return not plugin.installed and not plugin.uri return not plugin._.installed and not plugin.uri
end, end,
title = "Cleaned", title = "Cleaned",
}, },
{ {
filter = function(plugin) filter = function(plugin)
return plugin.loaded return plugin._.loaded
end, end,
title = "Loaded", title = "Loaded",
}, },
{ {
filter = function(plugin) filter = function(plugin)
return plugin.installed return plugin._.installed
end, end,
title = "Installed", title = "Installed",
}, },