move lazy loading helpers inside of the dep folder
This commit is contained in:
43
lua/dep/lazy/short.lua
Normal file
43
lua/dep/lazy/short.lua
Normal file
@ -0,0 +1,43 @@
|
||||
--- shorthands for when you only need to define one callback
|
||||
local short = {}
|
||||
|
||||
--- 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
|
||||
---@return function callback lazy setup
|
||||
function short:cmd(name, opts)
|
||||
return function(load)
|
||||
load:cmd(name, opts)
|
||||
end
|
||||
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
|
||||
---@return function callback lazy setup
|
||||
function short:auto(event, opts)
|
||||
return function(load)
|
||||
load:auto(event, opts)
|
||||
end
|
||||
end
|
||||
|
||||
--- create an auto command which will trigger on filetype
|
||||
---@param filetype string filetype to register the auto on
|
||||
function short:ft(filetype)
|
||||
return function(load)
|
||||
load:ft(filetype)
|
||||
end
|
||||
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
|
||||
---@return function callback lazy setup
|
||||
function short:keymap(mode, bind, opts)
|
||||
return function(load)
|
||||
load:keymap(mode, bind, opts)
|
||||
end
|
||||
end
|
||||
|
||||
return short
|
124
lua/dep/lazy/utils.lua
Normal file
124
lua/dep/lazy/utils.lua
Normal file
@ -0,0 +1,124 @@
|
||||
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
|
@ -299,7 +299,7 @@ function package:ensureadded(force)
|
||||
self.lazied = true
|
||||
for _, load_cond in pairs(self.lazy_load) do
|
||||
-- configure the lazy loader for the user
|
||||
local l = require('lazy.utils'):new()
|
||||
local l = require('dep.lazy.utils'):new()
|
||||
if l == true then
|
||||
logger:log("lazy", "failed to get lazy utils")
|
||||
return false
|
||||
|
Reference in New Issue
Block a user