feat: deactivate WIP

This commit is contained in:
Folke Lemaitre 2023-02-07 23:52:02 +01:00
parent 49b43def14
commit 57a3960faf
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 97 additions and 14 deletions

View File

@ -181,6 +181,78 @@ function M.load(plugins, reason, opts)
end end
end end
---@param plugin LazyPlugin
function M.deactivate(plugin)
local main = M.get_main(plugin)
if main then
Util.try(function()
local mod = require(main)
if mod.deactivate then
mod.deactivate(plugin)
end
end, "Failed to deactivate plugin " .. plugin.name)
end
-- execute deactivate when needed
if plugin._.loaded and plugin.deactivate then
Util.try(function()
plugin.deactivate(plugin)
end, "Failed to deactivate plugin " .. plugin.name)
end
-- disable handlers
Handler.disable(plugin)
-- remove loaded lua modules
Util.walkmods(plugin.dir .. "/lua", function(modname)
package.loaded[modname] = nil
package.preload[modname] = nil
end)
-- clear vim.g.loaded_ for plugins
Util.ls(plugin.dir .. "/plugin", function(_, name, type)
if type == "file" then
vim.g["loaded_" .. name:gsub("%..*", "")] = nil
end
end)
-- set as not loaded
plugin._.loaded = nil
end
--- reload a plugin
---@param plugin LazyPlugin
function M.reload(plugin)
M.deactivate(plugin)
local load = false -- plugin._.loaded ~= nil
-- enable handlers
Handler.enable(plugin)
-- run init
if plugin.init then
Util.try(function()
plugin.init(plugin)
end, "Failed to run `init` for **" .. plugin.name .. "**")
end
-- if this is a start plugin, load it now
if plugin.lazy == false then
load = true
end
for _, event in ipairs(plugin.event or {}) do
if event == "VimEnter" or event == "UIEnter" or event:find("VeryLazy") then
load = true
break
end
end
if load then
M.load(plugin, { start = "reload" })
end
end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
---@param reason {[string]:string} ---@param reason {[string]:string}
---@param opts? {force:boolean} when force is true, we skip the cond check ---@param opts? {force:boolean} when force is true, we skip the cond check
@ -242,6 +314,26 @@ function M.config(plugin)
plugin.config(plugin, opts) plugin.config(plugin, opts)
end end
else else
local main = M.get_main(plugin)
if main then
fn = function()
local opts = Plugin.values(plugin, "opts", false)
require(main).setup(opts)
end
else
return Util.error(
"Lua module not found for config of " .. plugin.name .. ". Please use a `config()` function instead"
)
end
end
Util.try(fn, "Failed to run `config` for " .. plugin.name)
end
---@param plugin LazyPlugin
function M.get_main(plugin)
if plugin.main then
return plugin.main
end
local normname = Util.normname(plugin.name) local normname = Util.normname(plugin.name)
---@type string[] ---@type string[]
local mods = {} local mods = {}
@ -254,18 +346,7 @@ function M.config(plugin)
break break
end end
end end
if #mods == 1 then return #mods == 1 and mods[1] or nil
fn = function()
local opts = Plugin.values(plugin, "opts", false)
require(mods[1]).setup(opts)
end
else
return Util.error(
"Lua module not found for config of " .. plugin.name .. ". Please use a `config()` function instead"
)
end
end
Util.try(fn, "Failed to run `config` for " .. plugin.name)
end end
---@param path string ---@param path string

View File

@ -19,6 +19,7 @@
---@class LazyPluginHooks ---@class LazyPluginHooks
---@field init? fun(self:LazyPlugin) Will always be run ---@field init? fun(self:LazyPlugin) Will always be run
---@field deactivate? fun(self:LazyPlugin) Unload/Stop a plugin
---@field config? fun(self:LazyPlugin, opts:table)|true Will be executed when loading the plugin ---@field config? fun(self:LazyPlugin, opts:table)|true Will be executed when loading the plugin
---@field build? string|fun(self:LazyPlugin)|(string|fun(self:LazyPlugin))[] ---@field build? string|fun(self:LazyPlugin)|(string|fun(self:LazyPlugin))[]
---@field opts? PluginOpts ---@field opts? PluginOpts
@ -40,6 +41,7 @@
---@class LazyPluginBase ---@class LazyPluginBase
---@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 main? string Entry module that has setup & deactivate
---@field url string? ---@field url string?
---@field dir string ---@field dir string
---@field enabled? boolean|(fun():boolean) ---@field enabled? boolean|(fun():boolean)