mirror of
https://github.com/zoriya/lz.n.git
synced 2025-12-06 06:36:11 +00:00
feat: lazy field for lazy-loading via trigger_load only (#105)
This commit is contained in:
@@ -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. |
|
||||
<!-- markdownlint-enable MD013 -->
|
||||
|
||||
@@ -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*
|
||||
|
||||
@@ -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
|
||||
|
||||
32
lua/lz/n/handler/lazy.lua
Normal file
32
lua/lz/n/handler/lazy.lua
Normal 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
|
||||
@@ -26,6 +26,7 @@
|
||||
---@field keys? lz.n.Keys[]
|
||||
---@field cmd? string[]
|
||||
---@field colorscheme? string[]
|
||||
---@field lazy? boolean
|
||||
|
||||
---@class lz.n.PluginSpecHandlers
|
||||
---
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user