mirror of
https://github.com/zoriya/lz.n.git
synced 2026-06-05 11:41:26 +00:00
feat: register individual plugin specs for lazy loading
by passing a single spec to `load()`
This commit is contained in:
+23
-9
@@ -16,26 +16,40 @@ end)
|
||||
|
||||
---@param spec string | lz.n.Spec
|
||||
function M.load(spec)
|
||||
if vim.g.lz_n_did_load then
|
||||
return vim.notify("lz.n has already loaded your plugins.", vim.log.levels.WARN, { title = "lz.n" })
|
||||
end
|
||||
vim.g.lz_n_did_load = true
|
||||
|
||||
if type(spec) == "string" then
|
||||
spec = { import = spec }
|
||||
end
|
||||
---@cast spec lz.n.Spec
|
||||
local plugins = require("lz.n.spec").parse(spec)
|
||||
--- @cast spec lz.n.Spec
|
||||
local spec_mod = require("lz.n.spec")
|
||||
local is_single_plugin_spec = spec_mod.is_single_plugin_spec(spec)
|
||||
if not is_single_plugin_spec then
|
||||
if vim.g.lz_n_did_load then
|
||||
return vim.notify(
|
||||
"lz.n.load() should only be called on a list of plugin specs once.",
|
||||
vim.log.levels.WARN,
|
||||
{ title = "lz.n" }
|
||||
)
|
||||
end
|
||||
vim.g.lz_n_did_load = true
|
||||
end
|
||||
local plugins = spec_mod.parse(spec)
|
||||
require("lz.n.loader").load_startup_plugins(plugins)
|
||||
require("lz.n.state").plugins = plugins
|
||||
|
||||
local state = require("lz.n.state")
|
||||
if is_single_plugin_spec then
|
||||
state.plugins = vim.tbl_deep_extend("force", state.plugins, plugins)
|
||||
else
|
||||
state.plugins = plugins
|
||||
end
|
||||
require("lz.n.handler").init(plugins)
|
||||
if vim.v.vim_did_enter == 1 then
|
||||
deferred_ui_enter()
|
||||
else
|
||||
elseif not vim.g.lz_n_did_create_deferred_ui_enter_autocmd then
|
||||
vim.api.nvim_create_autocmd("UIEnter", {
|
||||
once = true,
|
||||
callback = deferred_ui_enter,
|
||||
})
|
||||
vim.g.lz_n_did_create_deferred_ui_enter_autocmd = true
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ function M._load(plugin)
|
||||
end
|
||||
require("lz.n.handler").disable(plugin)
|
||||
---@type fun(name: string) | nil
|
||||
local load_impl = vim.tbl_get(vim.g, "lz_n", "load")
|
||||
local load_impl = plugin.load or vim.tbl_get(vim.g, "lz_n", "load")
|
||||
if type(load_impl) == "function" then
|
||||
load_impl(plugin.name)
|
||||
else
|
||||
|
||||
@@ -9,6 +9,10 @@ error("Cannot import a meta module")
|
||||
--- Only useful for lazy=false plugins to force loading certain plugins first.
|
||||
--- Default priority is 50
|
||||
--- @field priority? number
|
||||
---
|
||||
--- Set this to override the `load` function for an individual plugin.
|
||||
--- Defaults to `vim.g.lz_n.load()`, see |lz.n.Config|.
|
||||
--- @field load? fun(name: string)
|
||||
|
||||
--- @alias lz.n.Event {id:string, event:string[]|string, pattern?:string[]|string}
|
||||
--- @alias lz.n.EventSpec string|{event?:string|string[], pattern?:string|string[]}|string[]
|
||||
|
||||
+14
-2
@@ -123,16 +123,28 @@ local function parse(spec)
|
||||
return result
|
||||
end
|
||||
|
||||
---@param spec lz.n.Spec
|
||||
---@return boolean
|
||||
function M.is_spec_list(spec)
|
||||
return #spec > 1 or vim.islist(spec) and #spec > 1
|
||||
end
|
||||
|
||||
---@param spec lz.n.Spec
|
||||
---@return boolean
|
||||
function M.is_single_plugin_spec(spec)
|
||||
return type(spec[1]) == "string"
|
||||
end
|
||||
|
||||
---@private
|
||||
---@param spec lz.n.Spec
|
||||
---@param result table<string, lz.n.Plugin>
|
||||
function M._normalize(spec, result)
|
||||
if #spec > 1 or vim.islist(spec) and #spec > 1 then
|
||||
if M.is_spec_list(spec) then
|
||||
---@cast spec lz.n.Spec[]
|
||||
for _, sp in ipairs(spec) do
|
||||
M._normalize(sp, result)
|
||||
end
|
||||
elseif spec[1] then
|
||||
elseif M.is_single_plugin_spec(spec) then
|
||||
---@cast spec lz.n.PluginSpec
|
||||
result[spec[1]] = parse(spec)
|
||||
elseif spec.import then
|
||||
|
||||
Reference in New Issue
Block a user