mirror of https://github.com/folke/lazy.nvim.git
perf: cache handler groups
This commit is contained in:
parent
05a0da532b
commit
42c2fb42c8
|
@ -3,32 +3,39 @@ local Loader = require("lazy.core.loader")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@alias LazyHandler fun(plugins:LazyPlugin[])
|
---@alias LazyHandler fun(grouped:table<string, string[]>)
|
||||||
|
|
||||||
|
---@type table<string, table<string, string[]>>
|
||||||
|
M._groups = nil
|
||||||
|
|
||||||
---@param plugins LazyPlugin[]
|
---@param plugins LazyPlugin[]
|
||||||
---@param key string
|
---@param rebuild? boolean
|
||||||
---@return table<any, LazyPlugin[]>
|
function M.group(plugins, rebuild)
|
||||||
function M.group(plugins, key)
|
if M._groups == nil or rebuild then
|
||||||
---@type table<any, LazyPlugin[]>
|
M._groups = {}
|
||||||
local ret = {}
|
local types = vim.tbl_keys(M.handlers) --[[@as string[] ]]
|
||||||
for _, plugin in pairs(plugins) do
|
for _, key in ipairs(types) do
|
||||||
---@diagnostic disable-next-line: no-unknown
|
M._groups[key] = {}
|
||||||
for _, value in pairs(type(plugin[key]) == "table" and plugin[key] or { plugin[key] }) do
|
for _, plugin in pairs(plugins) do
|
||||||
ret[value] = ret[value] or {}
|
if plugin[key] then
|
||||||
table.insert(ret[value], plugin)
|
---@diagnostic disable-next-line: no-unknown
|
||||||
|
for _, value in pairs(type(plugin[key]) == "table" and plugin[key] or { plugin[key] }) do
|
||||||
|
M._groups[key][value] = M._groups[key][value] or {}
|
||||||
|
table.insert(M._groups[key][value], plugin.name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return ret
|
return M._groups
|
||||||
end
|
end
|
||||||
|
|
||||||
---@type table<string, LazyHandler>
|
---@type table<string, LazyHandler>
|
||||||
M.handlers = {}
|
M.handlers = {}
|
||||||
|
|
||||||
---@param plugins LazyPlugin[]
|
function M.handlers.event(grouped)
|
||||||
function M.handlers.event(plugins)
|
|
||||||
local group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
|
local group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
|
||||||
---@diagnostic disable-next-line: redefined-local
|
for event, plugins in pairs(grouped) do
|
||||||
for event, plugins in pairs(M.group(plugins, "event")) do
|
|
||||||
---@cast event string
|
---@cast event string
|
||||||
if event == "VimEnter" and vim.v.vim_did_enter == 1 then
|
if event == "VimEnter" and vim.v.vim_did_enter == 1 then
|
||||||
Loader.load(plugins, { event = event })
|
Loader.load(plugins, { event = event })
|
||||||
|
@ -48,9 +55,8 @@ function M.handlers.event(plugins)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.handlers.keys(plugins)
|
function M.handlers.keys(grouped)
|
||||||
---@diagnostic disable-next-line: redefined-local
|
for keys, plugins in pairs(grouped) do
|
||||||
for keys, plugins in pairs(M.group(plugins, "keys")) do
|
|
||||||
---@cast keys string
|
---@cast keys string
|
||||||
vim.keymap.set("n", keys, function()
|
vim.keymap.set("n", keys, function()
|
||||||
vim.keymap.del("n", keys)
|
vim.keymap.del("n", keys)
|
||||||
|
@ -62,10 +68,9 @@ function M.handlers.keys(plugins)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.handlers.ft(plugins)
|
function M.handlers.ft(grouped)
|
||||||
local group = vim.api.nvim_create_augroup("lazy_handler_ft", { clear = true })
|
local group = vim.api.nvim_create_augroup("lazy_handler_ft", { clear = true })
|
||||||
---@diagnostic disable-next-line: redefined-local
|
for ft, plugins in pairs(grouped) do
|
||||||
for ft, plugins in pairs(M.group(plugins, "ft")) do
|
|
||||||
---@cast ft string
|
---@cast ft string
|
||||||
vim.api.nvim_create_autocmd("FileType", {
|
vim.api.nvim_create_autocmd("FileType", {
|
||||||
once = true,
|
once = true,
|
||||||
|
@ -80,9 +85,8 @@ function M.handlers.ft(plugins)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.handlers.cmd(plugins)
|
function M.handlers.cmd(grouped)
|
||||||
---@diagnostic disable-next-line: redefined-local
|
for cmd, plugins in pairs(grouped) do
|
||||||
for cmd, plugins in pairs(M.group(plugins, "cmd")) do
|
|
||||||
---@cast cmd string
|
---@cast cmd string
|
||||||
local function _load(complete)
|
local function _load(complete)
|
||||||
vim.api.nvim_del_user_command(cmd)
|
vim.api.nvim_del_user_command(cmd)
|
||||||
|
@ -117,17 +121,16 @@ function M.handlers.cmd(plugins)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.handlers.module(plugins)
|
function M.handlers.module(grouped)
|
||||||
local modules = M.group(plugins, "module")
|
|
||||||
---@param modname string
|
---@param modname string
|
||||||
table.insert(package.loaders, 2, function(modname)
|
table.insert(package.loaders, 2, function(modname)
|
||||||
local idx = modname:find(".", 1, true) or #modname + 1
|
local idx = modname:find(".", 1, true) or #modname + 1
|
||||||
while idx do
|
while idx do
|
||||||
local name = modname:sub(1, idx - 1)
|
local name = modname:sub(1, idx - 1)
|
||||||
---@diagnostic disable-next-line: redefined-local
|
---@diagnostic disable-next-line: redefined-local
|
||||||
local plugins = modules[name]
|
local plugins = grouped[name]
|
||||||
if plugins then
|
if plugins then
|
||||||
modules[name] = nil
|
grouped[name] = nil
|
||||||
local reason = { require = modname }
|
local reason = { require = modname }
|
||||||
if #Loader.loading == 0 then
|
if #Loader.loading == 0 then
|
||||||
local f = 3
|
local f = 3
|
||||||
|
|
|
@ -8,19 +8,13 @@ M.loading = {}
|
||||||
|
|
||||||
function M.setup()
|
function M.setup()
|
||||||
local Handler = require("lazy.core.handler")
|
local Handler = require("lazy.core.handler")
|
||||||
|
local groups = Handler.group(Config.plugins)
|
||||||
for t, handler in pairs(Handler.handlers) do
|
for t, handler in pairs(Handler.handlers) do
|
||||||
Util.track(t)
|
if groups[t] then
|
||||||
---@type LazyPlugin[]
|
Util.track(t)
|
||||||
local plugins = {}
|
handler(groups[t])
|
||||||
for _, plugin in pairs(Config.plugins) do
|
Util.track()
|
||||||
if plugin[t] ~= nil then
|
|
||||||
table.insert(plugins, plugin)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
if #plugins > 0 then
|
|
||||||
handler(plugins)
|
|
||||||
end
|
|
||||||
Util.track()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -207,6 +207,8 @@ function M.load()
|
||||||
|
|
||||||
if M.dirty then
|
if M.dirty then
|
||||||
Cache.dirty = true
|
Cache.dirty = true
|
||||||
|
elseif state then
|
||||||
|
require("lazy.core.handler")._groups = state.handlers
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -215,6 +217,7 @@ function M.save()
|
||||||
local state = {
|
local state = {
|
||||||
---@type table<string, LazySpec>
|
---@type table<string, LazySpec>
|
||||||
specs = {},
|
specs = {},
|
||||||
|
handlers = require("lazy.core.handler").group(Config.plugins, true),
|
||||||
config = Config.options,
|
config = Config.options,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue