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
|
||||||
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,22 +314,11 @@ function M.config(plugin)
|
||||||
plugin.config(plugin, opts)
|
plugin.config(plugin, opts)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
local normname = Util.normname(plugin.name)
|
local main = M.get_main(plugin)
|
||||||
---@type string[]
|
if main then
|
||||||
local mods = {}
|
|
||||||
for _, modname in ipairs(Cache.get_topmods(plugin.dir)) do
|
|
||||||
mods[#mods + 1] = modname
|
|
||||||
local modnorm = Util.normname(modname)
|
|
||||||
-- if we found an exact match, then use that
|
|
||||||
if modnorm == normname then
|
|
||||||
mods = { modname }
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if #mods == 1 then
|
|
||||||
fn = function()
|
fn = function()
|
||||||
local opts = Plugin.values(plugin, "opts", false)
|
local opts = Plugin.values(plugin, "opts", false)
|
||||||
require(mods[1]).setup(opts)
|
require(main).setup(opts)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
return Util.error(
|
return Util.error(
|
||||||
|
@ -268,6 +329,26 @@ function M.config(plugin)
|
||||||
Util.try(fn, "Failed to run `config` for " .. plugin.name)
|
Util.try(fn, "Failed to run `config` for " .. plugin.name)
|
||||||
end
|
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 = {}
|
||||||
|
for _, modname in ipairs(Cache.get_topmods(plugin.dir)) do
|
||||||
|
mods[#mods + 1] = modname
|
||||||
|
local modnorm = Util.normname(modname)
|
||||||
|
-- if we found an exact match, then use that
|
||||||
|
if modnorm == normname then
|
||||||
|
mods = { modname }
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return #mods == 1 and mods[1] or nil
|
||||||
|
end
|
||||||
|
|
||||||
---@param path string
|
---@param path string
|
||||||
function M.packadd(path)
|
function M.packadd(path)
|
||||||
M.source_runtime(path, "plugin")
|
M.source_runtime(path, "plugin")
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue