From fd1c5d6e79d09c025fe978f423e7730397dc341a Mon Sep 17 00:00:00 2001 From: Squibid Date: Tue, 24 Jun 2025 03:08:24 -0400 Subject: [PATCH] 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 --- lua/lazy/short.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ lua/lazy/utils.lua | 10 +++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 lua/lazy/short.lua diff --git a/lua/lazy/short.lua b/lua/lazy/short.lua new file mode 100644 index 0000000..d00fce2 --- /dev/null +++ b/lua/lazy/short.lua @@ -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 diff --git a/lua/lazy/utils.lua b/lua/lazy/utils.lua index ede428d..889c9be 100644 --- a/lua/lazy/utils.lua +++ b/lua/lazy/utils.lua @@ -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