Skip to content

Commit

Permalink
feat(trigger_load): return list of skipped plugins instead of failing (
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Aug 23, 2024
1 parent d04f490 commit 9c74d06
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ using the `trigger_load` function:

```lua
---@overload fun(plugin: lz.n.Plugin | lz.n.Plugin[])
---@overload fun(plugin: string | string[], opts: lz.n.lookup.Opts)
---@overload fun(plugin_name: string | string[], opts: lz.n.lookup.Opts): string[]
require('lz.n').trigger_load
```

Expand All @@ -435,7 +435,9 @@ The function provides two overloads, each suited for different use cases:
remains isolated and unaffected by external influences[^5],
thereby preventing multiple sources of truth.
2. **Stateful version:**
- Usage: `trigger_load(plugin: string | string[], opts?: lz.n.lookup.Opts)`
- Usage: `trigger_load(plugin_name: string | string[], opts?: lz.n.lookup.Opts)`
- Returns: A list of plugin names that were skipped
(empty if all plugins were loaded).
- Intended for: Scenarios where handler state is unknown or inaccessible,
such as in `before` or `after` hooks.
- Description: This version allows you to load plugins by name.
Expand Down
4 changes: 2 additions & 2 deletions lua/lz/n/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ end
--- Once a plugin has been loaded, it will be removed from all handlers (via `del`).
--- As a result, calling `trigger_load` with a plugin name is stateful and idempotent.
---@overload fun(plugins: lz.n.Plugin | string[] | lz.n.Plugin[] | table<unknown, lz.n.Plugin>)
---@overload fun(plugins: string | string[], opts: lz.n.lookup.Opts)
---@overload fun(plugins: string | string[], opts: lz.n.lookup.Opts): string[]
M.trigger_load = function(plugins, opts)
require("lz.n.loader").load(plugins, function(name)
return require("lz.n.loader").load(plugins, function(name)
return M.lookup(name, opts)
end)
end
Expand Down
19 changes: 13 additions & 6 deletions lua/lz/n/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,33 @@ local function hook(hook_key, plugin)
end

---@overload fun(plugins: lz.n.Plugin | string[] | lz.n.Plugin[] | table<unknown, lz.n.Plugin>)
---@overload fun(plugins: string | string[], lookup: fun(name: string): lz.n.Plugin?)
---@overload fun(plugins: string | string[], lookup: fun(name: string): lz.n.Plugin?): string[]
function M.load(plugins, lookup)
local iterator = vim.islist(plugins) and ipairs or pairs
plugins = (type(plugins) == "string" or plugins.name) and { plugins } or plugins
---@cast plugins (string|lz.n.Plugin)[] | table<unknown, lz.n.Plugin>
---@type string[]
local skipped = {}
for _, plugin in iterator(plugins) do
local loadable = true
-- NOTE: do not make this loop into vim.iter
-- https://github.com/nvim-neorocks/lz.n/pull/21
if type(plugin) == "string" then
---@diagnostic disable-next-line: cast-local-type
plugin = lookup and lookup(plugin) or plugin
if type(plugin) == "string" then
vim.notify("Plugin " .. plugin .. " not found", vim.log.levels.ERROR, { title = "lz.n" })
return
loadable = false
table.insert(skipped, plugin)
end
---@cast plugin lz.n.Plugin
end
hook("before", plugin)
M._load(plugin)
hook("after", plugin)
if loadable then
hook("before", plugin)
M._load(plugin)
hook("after", plugin)
end
end
return skipped
end

return M
18 changes: 18 additions & 0 deletions spec/trigger_load_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
vim.g.lz_n = {
load = function() end,
}
local lz = require("lz.n")

---@type lz.n.PluginSpec
local testplugin = {
"trigger_load_testplugin",
cmd = "Foo",
}
lz.load(testplugin)

describe("trigger_load", function()
it("returns a list of skipped plugins", function()
local skipped = lz.trigger_load({ "trigger_load_testplugin", "unknown_testplugin" })
assert.same({ "unknown_testplugin" }, skipped)
end)
end)

0 comments on commit 9c74d06

Please sign in to comment.