From 28af1e1ac316330fb2e1c8caf284a7275bba20ff Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Mon, 28 Nov 2022 11:19:50 +0100 Subject: [PATCH] refactor: moved all plugin state to Plugin._ --- lua/lazy/core/loader.lua | 10 +++++----- lua/lazy/core/plugin.lua | 9 ++++++--- lua/lazy/init.lua | 6 +++--- lua/lazy/manage/init.lua | 14 +++++++------- lua/lazy/manage/task/git.lua | 14 +++++++------- lua/lazy/manage/task/init.lua | 4 ++-- lua/lazy/manage/task/plugin.lua | 6 +++--- lua/lazy/view/render.lua | 18 +++++++++--------- lua/lazy/view/sections.lua | 14 +++++++------- 9 files changed, 49 insertions(+), 46 deletions(-) diff --git a/lua/lazy/core/loader.lua b/lua/lazy/core/loader.lua index fc1ce84..49c9adb 100644 --- a/lua/lazy/core/loader.lua +++ b/lua/lazy/core/loader.lua @@ -46,14 +46,14 @@ function M.load(plugins, reason, opts) plugin = type(plugin) == "string" and Config.plugins[plugin] or plugin ---@cast plugin LazyPlugin - if not plugin.loaded then + if not plugin._.loaded then ---@diagnostic disable-next-line: assign-type-mismatch - plugin.loaded = {} + plugin._.loaded = {} for k, v in pairs(reason) do - plugin.loaded[k] = v + plugin._.loaded[k] = v end if #M.loading > 0 then - plugin.loaded.plugin = M.loading[#M.loading].name + plugin._.loaded.plugin = M.loading[#M.loading].name end 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) end - plugin.loaded.time = Util.track().time + plugin._.loaded.time = Util.track().time table.remove(M.loading) vim.schedule(function() vim.cmd("do User LazyRender") diff --git a/lua/lazy/core/plugin.lua b/lua/lazy/core/plugin.lua index dc1d495..7bb252d 100644 --- a/lua/lazy/core/plugin.lua +++ b/lua/lazy/core/plugin.lua @@ -6,7 +6,7 @@ local Cache = require("lazy.core.cache") local M = {} ---@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 } M.dirty = false @@ -29,7 +29,7 @@ M.dirty = false ---@field commit? string ---@field version? string ----@class LazyPlugin: LazyPluginHandlers,LazyPluginHooks,LazyPluginState,LazyPluginRef +---@class LazyPlugin: LazyPluginHandlers,LazyPluginHooks,LazyPluginRef ---@field [1] string ---@field name string display name and name used for plugin config files ---@field uri string @@ -37,6 +37,7 @@ M.dirty = false ---@field enabled? boolean|(fun():boolean) ---@field opt? boolean ---@field requires? string[] +---@field _ LazyPluginState ---@alias LazySpec string|LazyPlugin|LazySpec[]|{requires:LazySpec} @@ -69,6 +70,7 @@ function Spec:add(plugin) Util.error("Invalid plugin spec " .. vim.inspect(plugin)) end plugin.uri = plugin.uri or ("https://github.com/" .. plugin[1] .. ".git") + plugin._ = {} -- PERF: optimized code to get package name without using lua patterns if not plugin.name then @@ -133,11 +135,12 @@ function M.update_state(check_clean) end for _, plugin in pairs(Config.plugins) do + plugin._ = plugin._ or {} plugin[1] = plugin["1"] or plugin[1] plugin.opt = plugin.opt == nil and Config.options.opt or plugin.opt local opt = plugin.opt and "opt" or "start" 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 end diff --git a/lua/lazy/init.lua b/lua/lazy/init.lua index 6fbf500..7d31dda 100644 --- a/lua/lazy/init.lua +++ b/lua/lazy/init.lua @@ -27,7 +27,7 @@ function M.setup(opts) Util.track("install") for _, plugin in pairs(Config.plugins) do - if not plugin.installed then + if not plugin._.installed then vim.cmd("do User LazyInstallPre") require("lazy.manage").install({ wait = true, @@ -49,7 +49,7 @@ function M.setup(opts) Loader.init_plugins() if Config.plugins["lazy.nvim"] then - Config.plugins["lazy.nvim"].loaded.time = lazy_delta + Config.plugins["lazy.nvim"]._.loaded.time = lazy_delta end vim.cmd("do User LazyDone") @@ -64,7 +64,7 @@ function M.stats() for _, plugin in pairs(require("lazy.core.config").plugins) do ret.count = ret.count + 1 - if plugin.loaded then + if plugin._.loaded then ret.loaded = ret.loaded + 1 end end diff --git a/lua/lazy/manage/init.lua b/lua/lazy/manage/init.lua index 76f070c..f6c1a40 100644 --- a/lua/lazy/manage/init.lua +++ b/lua/lazy/manage/init.lua @@ -49,7 +49,7 @@ function M.install(opts) M.run({ pipeline = { "git.install", { "plugin.docs", "plugin.run" } }, plugins = function(plugin) - return plugin.uri and not plugin.installed + return plugin.uri and not plugin._.installed end, }, opts) end @@ -59,7 +59,7 @@ function M.update(opts) M.run({ pipeline = { "git.update", { "plugin.docs", "plugin.run" }, "git.log" }, plugins = function(plugin) - return plugin.uri and plugin.installed + return plugin.uri and plugin._.installed end, }, opts) end @@ -69,7 +69,7 @@ function M.log(opts) M.run({ pipeline = { "git.log" }, plugins = function(plugin) - return plugin.uri and plugin.installed + return plugin.uri and plugin._.installed end, }, opts) end @@ -86,13 +86,13 @@ end function M.clear() for _, plugin in pairs(Config.plugins) do -- clear updated status - plugin.updated = nil + plugin._.updated = nil -- clear finished tasks - if plugin.tasks then + if plugin._.tasks then ---@param task LazyTask - plugin.tasks = vim.tbl_filter(function(task) + plugin._.tasks = vim.tbl_filter(function(task) return task:is_running() - end, plugin.tasks) + end, plugin._.tasks) end end vim.cmd([[do User LazyRender]]) diff --git a/lua/lazy/manage/task/git.lua b/lua/lazy/manage/task/git.lua index 78c1add..b1a08c9 100644 --- a/lua/lazy/manage/task/git.lua +++ b/lua/lazy/manage/task/git.lua @@ -8,7 +8,7 @@ M.log = { if opts.interactive ~= true or not Util.file_exists(plugin.dir .. "/.git") then return false end - return plugin.updated == nil or plugin.updated.from ~= plugin.updated.to + return plugin._.updated == nil or plugin._.updated.from ~= plugin._.updated.to end, run = function(self) local args = { @@ -20,8 +20,8 @@ M.log = { "--color=never", } - if self.plugin.updated then - table.insert(args, self.plugin.updated.from .. ".." .. (self.plugin.updated.to or "HEAD")) + if self.plugin._.updated then + table.insert(args, self.plugin._.updated.from .. ".." .. (self.plugin._.updated.to or "HEAD")) else table.insert(args, "--since=7 days ago") end @@ -59,11 +59,11 @@ M.update = { on_exit = function(ok) if ok then local git_new = assert(Util.git_info(self.plugin.dir)) - self.plugin.updated = { + self.plugin._.updated = { from = git.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, }) @@ -103,8 +103,8 @@ M.install = { args = args, on_exit = function(ok) if ok then - self.plugin.installed = true - self.plugin.dirty = true + self.plugin._.installed = true + self.plugin._.dirty = true end end, }) diff --git a/lua/lazy/manage/task/init.lua b/lua/lazy/manage/task/init.lua index 71dbbb3..6bb81ed 100644 --- a/lua/lazy/manage/task/init.lua +++ b/lua/lazy/manage/task/init.lua @@ -39,8 +39,8 @@ function Task.new(plugin, type, task, opts) self.type = type self.output = "" self.status = "" - plugin.tasks = plugin.tasks or {} - table.insert(plugin.tasks, self) + plugin._.tasks = plugin._.tasks or {} + table.insert(plugin._.tasks, self) return self end diff --git a/lua/lazy/manage/task/plugin.lua b/lua/lazy/manage/task/plugin.lua index 3d29275..ae71201 100644 --- a/lua/lazy/manage/task/plugin.lua +++ b/lua/lazy/manage/task/plugin.lua @@ -6,7 +6,7 @@ local M = {} M.run = { needed = function(plugin) - return plugin.dirty and (plugin.opt == false or plugin.run) + return plugin._.dirty and (plugin.opt == false or plugin.run) end, run = function(self) Loader.load(self.plugin, { task = "run" }, { load_start = true }) @@ -47,13 +47,13 @@ M.clean = { vim.loop.fs_unlink(dir) end - self.plugin.installed = false + self.plugin._.installed = false end, } M.docs = { needed = function(plugin) - return plugin.dirty + return plugin._.dirty end, run = function(self) local docs = self.plugin.dir .. "/doc/" diff --git a/lua/lazy/view/render.lua b/lua/lazy/view/render.lua index 7fc8075..48ded14 100644 --- a/lua/lazy/view/render.lua +++ b/lua/lazy/view/render.lua @@ -47,8 +47,8 @@ function M:update() } for _, plugin in ipairs(self.plugins) do - if plugin.tasks then - for _, task in ipairs(plugin.tasks) do + if plugin._.tasks then + for _, task in ipairs(plugin._.tasks) do self.progress.total = self.progress.total + 1 if not task:is_running() then self.progress.done = self.progress.done + 1 @@ -144,7 +144,7 @@ end ---@param plugin LazyPlugin function M:reason(plugin) - local reason = vim.deepcopy(plugin.loaded or {}) + local reason = vim.deepcopy(plugin._.loaded or {}) ---@type string? local source = reason.source if source then @@ -203,18 +203,18 @@ end ---@param plugin LazyPlugin function M:diagnostics(plugin) - if plugin.updated then - if plugin.updated.from == plugin.updated.to then + if plugin._.updated then + if plugin._.updated.from == plugin._.updated.to then self:diagnostic({ message = "already up to date", }) else 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 - for _, task in ipairs(plugin.tasks or {}) do + for _, task in ipairs(plugin._.tasks or {}) do if task:is_running() then self:diagnostic({ severity = vim.diagnostic.severity.WARN, @@ -233,7 +233,7 @@ end function M:plugin(plugin) self:append(" - ", "LazySpecial"):append(plugin.name) local plugin_start = self:row() - if plugin.loaded then + if plugin._.loaded then self:reason(plugin) end self:diagnostics(plugin) @@ -248,7 +248,7 @@ end ---@param plugin LazyPlugin 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 self:log(task) elseif task.error or self._details == plugin.name then diff --git a/lua/lazy/view/sections.lua b/lua/lazy/view/sections.lua index 22cd545..2015d95 100644 --- a/lua/lazy/view/sections.lua +++ b/lua/lazy/view/sections.lua @@ -1,8 +1,8 @@ ---@param plugin LazyPlugin ---@param filter fun(task:LazyTask):boolean? local function has_task(plugin, filter) - if plugin.tasks then - for _, task in ipairs(plugin.tasks) do + if plugin._.tasks then + for _, task in ipairs(plugin._.tasks) do if filter(task) then return true end @@ -49,7 +49,7 @@ return { { ---@param plugin LazyPlugin filter = function(plugin) - return plugin.updated and plugin.updated.from ~= plugin.updated.to + return plugin._.updated and plugin._.updated.from ~= plugin._.updated.to end, title = "Updated", }, @@ -63,25 +63,25 @@ return { }, { filter = function(plugin) - return plugin.installed and not plugin.uri + return plugin._.installed and not plugin.uri end, title = "Clean", }, { filter = function(plugin) - return not plugin.installed and not plugin.uri + return not plugin._.installed and not plugin.uri end, title = "Cleaned", }, { filter = function(plugin) - return plugin.loaded + return plugin._.loaded end, title = "Loaded", }, { filter = function(plugin) - return plugin.installed + return plugin._.installed end, title = "Installed", },