mirror of https://github.com/folke/lazy.nvim.git
feat: deactivate WIP
This commit is contained in:
parent
49b43def14
commit
57a3960faf
|
@ -181,6 +181,78 @@ function M.load(plugins, reason, opts)
|
|||
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 reason {[string]:string}
|
||||
---@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)
|
||||
end
|
||||
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)
|
||||
---@type string[]
|
||||
local mods = {}
|
||||
|
@ -254,18 +346,7 @@ function M.config(plugin)
|
|||
break
|
||||
end
|
||||
end
|
||||
if #mods == 1 then
|
||||
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)
|
||||
return #mods == 1 and mods[1] or nil
|
||||
end
|
||||
|
||||
---@param path string
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
---@class LazyPluginHooks
|
||||
---@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 build? string|fun(self:LazyPlugin)|(string|fun(self:LazyPlugin))[]
|
||||
---@field opts? PluginOpts
|
||||
|
@ -40,6 +41,7 @@
|
|||
---@class LazyPluginBase
|
||||
---@field [1] string?
|
||||
---@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 dir string
|
||||
---@field enabled? boolean|(fun():boolean)
|
||||
|
|
Loading…
Reference in New Issue