Files
dep/lua/lazy/utils.lua
Squibid fd1c5d6e79 make lazy loading better...
- add a ft function to make it better when trying to load a plugin on a
  ft
- add shorthands to make it easier to create one load condition
2025-06-24 03:08:24 -04:00

125 lines
3.3 KiB
Lua

local logger = require('dep.log')
---@class lazy
---@field load function the function to load the plugin
---@field command_ids table the commands that have been registered
---@field auto_ids table the auto commands that have been registered
---@field keybind_ids table the keybinds that have been registered
local lazy = {}
--- create a new instance of lazy
---@return lazy
function lazy:new()
local o = {}
setmetatable(o, self)
o.command_ids = {}
o.auto_ids = {}
o.keybind_ids = {}
self.__index = self
return o
end
--- set the loading function
---@param load function the loading function
function lazy:set_load(load)
self.load = load
end
--- get the configured load function
---@return function load function
function lazy:get_load()
return self.load
end
--- create a usercommand which will trigger the plugin to load
---@param name string the name of the command
---@param opts vim.api.keyset.user_command? options
function lazy:cmd(name, opts)
opts = opts or {}
vim.api.nvim_create_user_command(name, opts['callback'] or function()
self:cleanup()
end, opts)
table.insert(self.command_ids, name)
end
--- create an auto command which will trigger the plugin to load
---@param event string the event to trigger on
---@param opts vim.api.keyset.create_autocmd? options
function lazy:auto(event, opts)
opts = opts or {}
opts['callback'] = opts['callback'] or function()
self:cleanup()
end
-- create the auto command and save it
table.insert(self.auto_ids, vim.api.nvim_create_autocmd(event, opts))
end
--- create an auto command which will trigger on filetype
---@param filetype string filetype to register the auto on
function lazy:ft(filetype)
lazy:auto("FileType", {
pattern = filetype
})
end
--- create a keybind which will trigger the plugin to load
---@param mode string the mode to trigger in
---@param bind string the binding to use
---@param opts vim.keymap.set.Opts? options
function lazy:keymap(mode, bind, opts)
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
vim.keymap.set(mode, bind, opts['callback'] or function()
-- register keymap unload
self:cleanup()
-- call the keymap after the user has mapped it
if rerun then
local keys = vim.api.nvim_replace_termcodes(bind, true, false, true)
vim.api.nvim_input(keys)
end
end, opts)
table.insert(self.keybind_ids, { ['mode'] = mode, ['bind'] = bind })
end
--- cleanup all the callbacks, and load the plugin
function lazy:cleanup()
-- cleanup user commands
for _, command_id in pairs(self.command_ids) do
local ok, err = pcall(vim.api.nvim_del_user_command, command_id)
if not ok then
logger:log("lazy", err or "failed to delete user command")
end
end
-- cleanup auto commands
for _, auto_id in pairs(self.auto_ids) do
local ok, err = pcall(vim.api.nvim_del_autocmd, auto_id)
if not ok then
logger:log("lazy", err or "failed to delete auto command")
end
end
-- cleanup keymaps
for _, keybind_id in pairs(self.keybind_ids) do
local ok, err = pcall(vim.keymap.del, keybind_id.mode, keybind_id.bind, {})
if not ok then
logger:log("lazy", err or "failed to delete keymap")
end
end
-- load the plugin
self:load()
end
return lazy