Skip to content

Commit

Permalink
feat: deactivate WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Feb 7, 2023
1 parent 49b43de commit 57a3960
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 14 deletions.
109 changes: 95 additions & 14 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -242,22 +314,11 @@ function M.config(plugin)
plugin.config(plugin, opts)
end
else
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
if #mods == 1 then
local main = M.get_main(plugin)
if main then
fn = function()
local opts = Plugin.values(plugin, "opts", false)
require(mods[1]).setup(opts)
require(main).setup(opts)
end
else
return Util.error(
Expand All @@ -268,6 +329,26 @@ function M.config(plugin)
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 = {}
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
function M.packadd(path)
M.source_runtime(path, "plugin")
Expand Down
2 changes: 2 additions & 0 deletions lua/lazy/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 57a3960

Please sign in to comment.