feat: lazy field for lazy-loading via trigger_load only (#105)

This commit is contained in:
Marc Jakobi
2024-10-21 17:28:23 +02:00
committed by GitHub
parent 2692693947
commit a7b445fd34
6 changed files with 46 additions and 1 deletions

View File

@@ -145,6 +145,7 @@ require("lz.n").load(plugins)
| **ft** | `string?` or `string[]` | Lazy-load on filetype. | `ft` | | **ft** | `string?` or `string[]` | Lazy-load on filetype. | `ft` |
| **keys** | `string?` or `string[]` or `lz.n.KeysSpec[]` | Lazy-load on key mapping. | `keys` | | **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]. | | **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` | | **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. | | **load** | `fun(string)?` | Can be used to override the `vim.g.lz_n.load()` function for an individual plugin. | None. |
<!-- markdownlint-enable MD013 --> <!-- markdownlint-enable MD013 -->

View File

@@ -126,6 +126,7 @@ lz.n.PluginHandlers *lz.n.PluginHandlers*
{keys?} (lz.n.Keys[]) {keys?} (lz.n.Keys[])
{cmd?} (string[]) {cmd?} (string[])
{colorscheme?} (string[]) {colorscheme?} (string[])
{lazy?} (boolean)
lz.n.PluginSpecHandlers *lz.n.PluginSpecHandlers* lz.n.PluginSpecHandlers *lz.n.PluginSpecHandlers*

View File

@@ -6,6 +6,7 @@ local handlers = {
ft = require("lz.n.handler.ft"), ft = require("lz.n.handler.ft"),
keys = require("lz.n.handler.keys"), keys = require("lz.n.handler.keys"),
colorscheme = require("lz.n.handler.colorscheme"), colorscheme = require("lz.n.handler.colorscheme"),
lazy = require("lz.n.handler.lazy"),
} }
---@param name string ---@param name string

32
lua/lz/n/handler/lazy.lua Normal file
View File

@@ -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

View File

@@ -26,6 +26,7 @@
---@field keys? lz.n.Keys[] ---@field keys? lz.n.Keys[]
---@field cmd? string[] ---@field cmd? string[]
---@field colorscheme? string[] ---@field colorscheme? string[]
---@field lazy? boolean
---@class lz.n.PluginSpecHandlers ---@class lz.n.PluginSpecHandlers
--- ---

View File

@@ -13,13 +13,14 @@ describe("trigger_load in before/after hooks", function()
local foo_load_count = 0 local foo_load_count = 0
local zoo_load_count = 0 local zoo_load_count = 0
local hoo_load_count = 0 local hoo_load_count = 0
local lazy_plugin_load_count = 0
local ignored_by_trigger_load local ignored_by_trigger_load
lz.load({ lz.load({
{ {
"bar", "bar",
[hook] = function() [hook] = function()
-- This should remove bar from the event handler's list -- 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, end,
event = "BufEnter", event = "BufEnter",
}, },
@@ -44,12 +45,20 @@ describe("trigger_load in before/after hooks", function()
hoo_load_count = hoo_load_count + 1 hoo_load_count = hoo_load_count + 1
end, end,
}, },
{
"lazy_plugin",
lazy = true,
load = function()
lazy_plugin_load_count = lazy_plugin_load_count + 1
end,
},
}) })
vim.api.nvim_exec_autocmds("BufEnter", {}) vim.api.nvim_exec_autocmds("BufEnter", {})
assert.is_not_nil(ignored_by_trigger_load) -- before invoked assert.is_not_nil(ignored_by_trigger_load) -- before invoked
assert.same(1, foo_load_count) assert.same(1, foo_load_count)
assert.same(1, zoo_load_count) assert.same(1, zoo_load_count)
assert.same(1, hoo_load_count) assert.same(1, hoo_load_count)
assert.same(1, lazy_plugin_load_count)
end end
end) end)
it("resilient against state updates with multiple events in " .. hook .. " hook", function() it("resilient against state updates with multiple events in " .. hook .. " hook", function()