From a7b445fd34fb242f7fa5d294499e89f55ed0935a Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Mon, 21 Oct 2024 17:28:23 +0200 Subject: [PATCH] feat: `lazy` field for lazy-loading via `trigger_load` only (#105) --- README.md | 1 + doc/lz.n.txt | 1 + lua/lz/n/handler/init.lua | 1 + lua/lz/n/handler/lazy.lua | 32 ++++++++++++++++++++++++++++++ lua/lz/n/meta.lua | 1 + spec/trigger_load_in_hook_spec.lua | 11 +++++++++- 6 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 lua/lz/n/handler/lazy.lua diff --git a/README.md b/README.md index f2368c8..1eb29cc 100755 --- a/README.md +++ b/README.md @@ -145,6 +145,7 @@ require("lz.n").load(plugins) | **ft** | `string?` or `string[]` | Lazy-load on filetype. | `ft` | | **keys** | `string?` or `string[]` or `lz.n.KeysSpec[]` | Lazy-load on key mapping. | `keys` | | **colorscheme** | `string?` or `string[]` | Lazy-load on colorscheme. | None. `lazy.nvim` lazy-loads colorschemes automatically[^2]. | +| **lazy** | `boolean?` | Lazy-load manually, e.g. using `trigger_load`. | `lazy` | | **priority** | `number?` | Only useful for **start** plugins (not lazy-loaded) to force loading certain plugins first. Default priority is `50`. | `priority` | | **load** | `fun(string)?` | Can be used to override the `vim.g.lz_n.load()` function for an individual plugin. | None. | diff --git a/doc/lz.n.txt b/doc/lz.n.txt index 4b03992..67b870a 100644 --- a/doc/lz.n.txt +++ b/doc/lz.n.txt @@ -126,6 +126,7 @@ lz.n.PluginHandlers *lz.n.PluginHandlers* {keys?} (lz.n.Keys[]) {cmd?} (string[]) {colorscheme?} (string[]) + {lazy?} (boolean) lz.n.PluginSpecHandlers *lz.n.PluginSpecHandlers* diff --git a/lua/lz/n/handler/init.lua b/lua/lz/n/handler/init.lua index 8707087..4231e2a 100644 --- a/lua/lz/n/handler/init.lua +++ b/lua/lz/n/handler/init.lua @@ -6,6 +6,7 @@ local handlers = { ft = require("lz.n.handler.ft"), keys = require("lz.n.handler.keys"), colorscheme = require("lz.n.handler.colorscheme"), + lazy = require("lz.n.handler.lazy"), } ---@param name string diff --git a/lua/lz/n/handler/lazy.lua b/lua/lz/n/handler/lazy.lua new file mode 100644 index 0000000..122ef92 --- /dev/null +++ b/lua/lz/n/handler/lazy.lua @@ -0,0 +1,32 @@ +---A handler for plugins that have `lazy` set to true without any other lazy-loading mechanisms configured. +---@class lz.n.LazyHandler: lz.n.Handler + +---@type lz.n.handler.State +local state = require("lz.n.handler.state").new() + +local M = { + spec_field = "lazy", +} + +---@param name string +---@return lz.n.Plugin? +function M.lookup(name) + return state.lookup_plugin(name) +end + +---@param name string +function M.del(name) + state.del(name, function(cmd) + pcall(vim.api.nvim_del_user_command, cmd) + end) +end + +---@param plugin lz.n.Plugin +function M.add(plugin) + if not plugin.lazy then + return + end + state.insert(plugin) +end + +return M diff --git a/lua/lz/n/meta.lua b/lua/lz/n/meta.lua index ea45c8b..5fa0fdc 100644 --- a/lua/lz/n/meta.lua +++ b/lua/lz/n/meta.lua @@ -26,6 +26,7 @@ ---@field keys? lz.n.Keys[] ---@field cmd? string[] ---@field colorscheme? string[] +---@field lazy? boolean ---@class lz.n.PluginSpecHandlers --- diff --git a/spec/trigger_load_in_hook_spec.lua b/spec/trigger_load_in_hook_spec.lua index b30540b..2c5d011 100644 --- a/spec/trigger_load_in_hook_spec.lua +++ b/spec/trigger_load_in_hook_spec.lua @@ -13,13 +13,14 @@ describe("trigger_load in before/after hooks", function() local foo_load_count = 0 local zoo_load_count = 0 local hoo_load_count = 0 + local lazy_plugin_load_count = 0 local ignored_by_trigger_load lz.load({ { "bar", [hook] = function() -- This should remove bar from the event handler's list - ignored_by_trigger_load = lz.trigger_load({ "foo", "zoo", "hoo" }) + ignored_by_trigger_load = lz.trigger_load({ "foo", "zoo", "hoo", "lazy_plugin" }) end, event = "BufEnter", }, @@ -44,12 +45,20 @@ describe("trigger_load in before/after hooks", function() hoo_load_count = hoo_load_count + 1 end, }, + { + "lazy_plugin", + lazy = true, + load = function() + lazy_plugin_load_count = lazy_plugin_load_count + 1 + end, + }, }) vim.api.nvim_exec_autocmds("BufEnter", {}) assert.is_not_nil(ignored_by_trigger_load) -- before invoked assert.same(1, foo_load_count) assert.same(1, zoo_load_count) assert.same(1, hoo_load_count) + assert.same(1, lazy_plugin_load_count) end end) it("resilient against state updates with multiple events in " .. hook .. " hook", function()