Skip to content

Commit

Permalink
feat: lazy field for lazy-loading via trigger_load only (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Oct 21, 2024
1 parent 2692693 commit a7b445f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down
1 change: 1 addition & 0 deletions doc/lz.n.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
1 change: 1 addition & 0 deletions lua/lz/n/handler/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions lua/lz/n/handler/lazy.lua
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lua/lz/n/meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
---@field keys? lz.n.Keys[]
---@field cmd? string[]
---@field colorscheme? string[]
---@field lazy? boolean

---@class lz.n.PluginSpecHandlers
---
Expand Down
11 changes: 10 additions & 1 deletion spec/trigger_load_in_hook_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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()
Expand Down

0 comments on commit a7b445f

Please sign in to comment.