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
This commit is contained in:
2025-06-24 03:08:24 -04:00
parent 6bd6db3c02
commit fd1c5d6e79
2 changed files with 52 additions and 1 deletions

43
lua/lazy/short.lua Normal file
View 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

View File

@ -46,7 +46,7 @@ function lazy:cmd(name, opts)
table.insert(self.command_ids, name)
end
--- user an auto command which will trigger the plugin to load
--- 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)
@ -60,6 +60,14 @@ function lazy:auto(event, opts)
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