Compare commits
5 Commits
adec93b7f4
...
lazy
Author | SHA1 | Date | |
---|---|---|---|
70853bd01e
|
|||
9d4322572c
|
|||
3b33a604d8
|
|||
5aff147731
|
|||
edf32fbf06
|
@ -217,10 +217,17 @@ is equivalent to the following:
|
|||||||
load:cmd("Command", {
|
load:cmd("Command", {
|
||||||
callback = function()
|
callback = function()
|
||||||
load:cleanup()
|
load:cleanup()
|
||||||
|
|
||||||
|
if (rerun) then
|
||||||
|
vim.cmd("Command")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
<
|
<
|
||||||
If you wish the second argument may be completely ommitted.
|
If you wish the second argument may be completely ommitted. Note the inclusion
|
||||||
|
of a `rerun` field, this is a parameter which may be passed into the options table
|
||||||
|
to re-run the binding after loading the package. You may choose to disable the
|
||||||
|
built-in logic by passing false.
|
||||||
|
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
LOAD:AUTO *dep-lazy-loading-api-auto*
|
LOAD:AUTO *dep-lazy-loading-api-auto*
|
||||||
|
@ -3,10 +3,10 @@ local packager = require('dep.package')
|
|||||||
|
|
||||||
---@class lazy_loader
|
---@class lazy_loader
|
||||||
---@field load function the function to load the plugin
|
---@field load function the function to load the plugin
|
||||||
---@field command_ids table the commands that have been registered
|
---@field command_ids string[] the commands that have been registered
|
||||||
---@field auto_ids table the auto commands that have been registered
|
---@field auto_ids number[] the auto commands that have been registered
|
||||||
---@field keybind_ids table the keybinds that have been registered
|
---@field keybind_ids table[] the keybinds that have been registered
|
||||||
---@field plugin_ids table the plugins that have been registered
|
---@field plugin_ids table[] the plugins that have been registered
|
||||||
local lazy_loader = {}
|
local lazy_loader = {}
|
||||||
|
|
||||||
--- create a new instance of lazy
|
--- create a new instance of lazy
|
||||||
@ -37,6 +37,11 @@ end
|
|||||||
function lazy_loader:cmd(name, opts)
|
function lazy_loader:cmd(name, opts)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
|
|
||||||
|
-- move the rerun arg to a seperate variable because keymap.set doesn't like
|
||||||
|
-- options it doesn't know of
|
||||||
|
local rerun = opts["rerun"] or true
|
||||||
|
opts['rerun'] = nil
|
||||||
|
|
||||||
-- load the plugin on completion
|
-- load the plugin on completion
|
||||||
if not opts["complete"] then
|
if not opts["complete"] then
|
||||||
opts["complete"] = function(_, line, _)
|
opts["complete"] = function(_, line, _)
|
||||||
@ -52,6 +57,11 @@ function lazy_loader:cmd(name, opts)
|
|||||||
|
|
||||||
vim.api.nvim_create_user_command(name, opts['callback'] or function(_)
|
vim.api.nvim_create_user_command(name, opts['callback'] or function(_)
|
||||||
self:cleanup()
|
self:cleanup()
|
||||||
|
|
||||||
|
-- attempt to rerun the command
|
||||||
|
if not rerun then
|
||||||
|
pcall(vim.cmd, name)
|
||||||
|
end
|
||||||
end, opts)
|
end, opts)
|
||||||
|
|
||||||
table.insert(self.command_ids, name)
|
table.insert(self.command_ids, name)
|
||||||
@ -133,29 +143,29 @@ end
|
|||||||
--- cleanup all the callbacks, and load the plugin
|
--- cleanup all the callbacks, and load the plugin
|
||||||
function lazy_loader:cleanup()
|
function lazy_loader:cleanup()
|
||||||
-- cleanup user commands
|
-- cleanup user commands
|
||||||
for _, command_id in pairs(self.command_ids) do
|
for _, command_id in ipairs(self.command_ids) do
|
||||||
local ok, err = pcall(vim.api.nvim_del_user_command, command_id)
|
local ok, err = pcall(vim.api.nvim_del_user_command, command_id)
|
||||||
if not ok then
|
if not ok then
|
||||||
logger:log("lazy", err or "failed to delete user command")
|
logger:log("lazy", err or "failed to delete user command")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- cleanup auto commands
|
-- cleanup auto commands
|
||||||
for _, auto_id in pairs(self.auto_ids) do
|
for _, auto_id in ipairs(self.auto_ids) do
|
||||||
local ok, err = pcall(vim.api.nvim_del_autocmd, auto_id)
|
local ok, err = pcall(vim.api.nvim_del_autocmd, auto_id)
|
||||||
if not ok then
|
if not ok then
|
||||||
logger:log("lazy", err or "failed to delete auto command")
|
logger:log("lazy", err or "failed to delete auto command")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- cleanup keymaps
|
-- cleanup keymaps
|
||||||
for _, keybind_id in pairs(self.keybind_ids) do
|
for _, keybind_id in ipairs(self.keybind_ids) do
|
||||||
local ok, err = pcall(vim.keymap.del, keybind_id.mode, keybind_id.bind, {})
|
local ok, err = pcall(vim.keymap.del, keybind_id.mode, keybind_id.bind, {})
|
||||||
if not ok then
|
if not ok then
|
||||||
logger:log("lazy", err or "failed to delete keymap")
|
logger:log("lazy", err or "failed to delete keymap")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- cleanup plugins
|
-- cleanup plugins
|
||||||
for _, plugin_id in pairs(self.plugin_ids) do
|
for _, plugin_id in ipairs(self.plugin_ids) do
|
||||||
table.remove(packager.get_packages()[plugin_id[0]], plugin_id[1])
|
table.remove(packager.get_packages()[plugin_id[1]].on_load, plugin_id[2])
|
||||||
end
|
end
|
||||||
-- load the plugin
|
-- load the plugin
|
||||||
self:load()
|
self:load()
|
||||||
|
@ -244,6 +244,10 @@ function package:ensureadded(force)
|
|||||||
--- load a package
|
--- load a package
|
||||||
---@param pkg package
|
---@param pkg package
|
||||||
local function loadpkg(pkg)
|
local function loadpkg(pkg)
|
||||||
|
if not self.enabled then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
-- make sure to load the dependencies first
|
-- make sure to load the dependencies first
|
||||||
for _, p in pairs(pkg.requirements) do
|
for _, p in pairs(pkg.requirements) do
|
||||||
if not p.loaded then
|
if not p.loaded then
|
||||||
|
Reference in New Issue
Block a user