refactor: move handlers to its own file

This commit is contained in:
Folke Lemaitre 2022-11-26 21:29:40 +01:00
parent 870d8924f7
commit 05a0da532b
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
5 changed files with 169 additions and 180 deletions

152
lua/lazy/core/handler.lua Normal file
View File

@ -0,0 +1,152 @@
local Util = require("lazy.core.util")
local Loader = require("lazy.core.loader")
local M = {}
---@alias LazyHandler fun(plugins:LazyPlugin[])
---@param plugins LazyPlugin[]
---@param key string
---@return table<any, LazyPlugin[]>
function M.group(plugins, key)
---@type table<any, LazyPlugin[]>
local ret = {}
for _, plugin in pairs(plugins) do
---@diagnostic disable-next-line: no-unknown
for _, value in pairs(type(plugin[key]) == "table" and plugin[key] or { plugin[key] }) do
ret[value] = ret[value] or {}
table.insert(ret[value], plugin)
end
end
return ret
end
---@type table<string, LazyHandler>
M.handlers = {}
---@param plugins LazyPlugin[]
function M.handlers.event(plugins)
local group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
---@diagnostic disable-next-line: redefined-local
for event, plugins in pairs(M.group(plugins, "event")) do
---@cast event string
if event == "VimEnter" and vim.v.vim_did_enter == 1 then
Loader.load(plugins, { event = event })
else
local _event, pattern = event:match("^(%w+)%s+(.*)$")
vim.api.nvim_create_autocmd(_event or event, {
group = group,
once = true,
pattern = pattern,
callback = function()
Util.track("event: " .. (_event == "User" and pattern or event))
Loader.load(plugins, { event = event })
Util.track()
end,
})
end
end
end
function M.handlers.keys(plugins)
---@diagnostic disable-next-line: redefined-local
for keys, plugins in pairs(M.group(plugins, "keys")) do
---@cast keys string
vim.keymap.set("n", keys, function()
vim.keymap.del("n", keys)
Util.track("keys: " .. keys)
Loader.load(plugins, { keys = keys })
vim.api.nvim_input(keys)
Util.track()
end)
end
end
function M.handlers.ft(plugins)
local group = vim.api.nvim_create_augroup("lazy_handler_ft", { clear = true })
---@diagnostic disable-next-line: redefined-local
for ft, plugins in pairs(M.group(plugins, "ft")) do
---@cast ft string
vim.api.nvim_create_autocmd("FileType", {
once = true,
pattern = ft,
group = group,
callback = function()
Util.track("filetype: " .. ft)
Loader.load(plugins, { ft = ft })
Util.track()
end,
})
end
end
function M.handlers.cmd(plugins)
---@diagnostic disable-next-line: redefined-local
for cmd, plugins in pairs(M.group(plugins, "cmd")) do
---@cast cmd string
local function _load(complete)
vim.api.nvim_del_user_command(cmd)
if complete then
Util.track("cmd-complete: " .. cmd)
else
Util.track("cmd: " .. cmd)
end
Loader.load(plugins, { cmd = cmd })
Util.track()
end
vim.api.nvim_create_user_command(cmd, function(event)
_load()
vim.cmd(
("%s %s%s%s %s"):format(
event.mods or "",
event.line1 == event.line2 and "" or event.line1 .. "," .. event.line2,
cmd,
event.bang and "!" or "",
event.args or ""
)
)
end, {
bang = true,
nargs = "*",
complete = function()
_load(true)
-- HACK: trick Neovim to show the newly loaded command completion
vim.api.nvim_input("<space><bs><tab>")
end,
})
end
end
function M.handlers.module(plugins)
local modules = M.group(plugins, "module")
---@param modname string
table.insert(package.loaders, 2, function(modname)
local idx = modname:find(".", 1, true) or #modname + 1
while idx do
local name = modname:sub(1, idx - 1)
---@diagnostic disable-next-line: redefined-local
local plugins = modules[name]
if plugins then
modules[name] = nil
local reason = { require = modname }
if #Loader.loading == 0 then
local f = 3
while not reason.source do
local info = debug.getinfo(f, "S")
if not info then
break
end
if info.what ~= "C" then
reason.source = info.source:sub(2)
end
f = f + 1
end
end
Loader.load(plugins, reason)
end
idx = modname:find(".", idx + 1, true)
end
end)
end
return M

View File

@ -3,148 +3,30 @@ local Config = require("lazy.core.config")
local M = {}
---@alias LoaderType "event"|"ft"|"module"|"keys"|"cmd"|"init"
---@type LoaderType[]
M.types = {
"event",
"ft",
"module",
"keys",
"cmd",
}
---@type table<LoaderType, table<string, string[]>>|{init: string[]}
M.loaders = nil
---@type LazyPlugin[]
M.loading = {}
function M.get_loaders()
---@type table<LoaderType, table<string, string[]>>|{init: string[]}
local loaders = { init = {} }
for _, lt in ipairs(M.types) do
loaders[lt] = {}
end
for _, plugin in pairs(Config.plugins) do
if plugin.init or (plugin.opt == false) then
table.insert(loaders.init, plugin.name)
end
for _, lt in ipairs(M.types) do
if plugin[lt] then
---@diagnostic disable-next-line: no-unknown
for _, loader in ipairs(type(plugin[lt]) == "table" and plugin[lt] or { plugin[lt] }) do
loaders[lt][loader] = loaders[lt][loader] or {}
table.insert(loaders[lt][loader], plugin.name)
end
end
end
end
return loaders
end
function M.setup()
M.loaders = M.loaders or M.get_loaders()
local group = vim.api.nvim_create_augroup("lazy_loader", {
clear = true,
})
-- modules
table.insert(package.loaders, 2, M.module)
-- events
Util.track("loader_events")
for event, plugins in pairs(M.loaders.event) do
if event == "VimEnter" and vim.v.vim_did_enter == 1 then
M.load(plugins, { event = event })
else
local user_event = event:match("User (.*)")
vim.api.nvim_create_autocmd(user_event and "User" or event, {
once = true,
group = group,
pattern = user_event,
callback = function()
Util.track("event: " .. (user_event or event))
M.load(plugins, { event = event })
Util.track()
end,
})
end
end
Util.track()
-- filetypes
Util.track("loader_filetypes")
for ft, plugins in pairs(M.loaders.ft) do
vim.api.nvim_create_autocmd("FileType", {
once = true,
pattern = ft,
group = group,
callback = function()
Util.track("filetype: " .. ft)
M.load(plugins, { ft = ft })
Util.track()
end,
})
end
Util.track()
-- keys
Util.track("loader_keys")
for keys, plugins in pairs(M.loaders.keys or {}) do
vim.keymap.set("n", keys, function()
vim.keymap.del("n", keys)
Util.track("keys: " .. keys)
M.load(plugins, { keys = keys })
vim.api.nvim_input(keys)
Util.track()
end)
end
Util.track()
-- commands
Util.track("loader_commands")
for cmd, plugins in pairs(M.loaders.cmd or {}) do
local function _load(complete)
vim.api.nvim_del_user_command(cmd)
if complete then
Util.track("cmd-complete: " .. cmd)
else
Util.track("cmd: " .. cmd)
local Handler = require("lazy.core.handler")
for t, handler in pairs(Handler.handlers) do
Util.track(t)
---@type LazyPlugin[]
local plugins = {}
for _, plugin in pairs(Config.plugins) do
if plugin[t] ~= nil then
table.insert(plugins, plugin)
end
M.load(plugins, { cmd = cmd })
Util.track()
end
vim.api.nvim_create_user_command(cmd, function(event)
_load()
vim.cmd(
("%s %s%s%s %s"):format(
event.mods or "",
event.line1 == event.line2 and "" or event.line1 .. "," .. event.line2,
cmd,
event.bang and "!" or "",
event.args or ""
)
)
end, {
bang = true,
nargs = "*",
complete = function()
_load(true)
-- HACK: trick Neovim to show the newly loaded command completion
vim.api.nvim_input("<space><bs><tab>")
end,
})
if #plugins > 0 then
handler(plugins)
end
Util.track()
end
Util.track()
end
function M.init_plugins()
Util.track("plugin_init")
for _, name in ipairs(M.loaders.init) do
local plugin = Config.plugins[name]
if not plugin then
error(name)
end
for _, plugin in pairs(Config.plugins) do
if plugin.init then
Util.track(plugin.name)
Util.try(plugin.init, "Failed to run `init` for **" .. plugin.name .. "**")
@ -157,34 +39,7 @@ function M.init_plugins()
Util.track()
end
---@param modname string
function M.module(modname)
local idx = modname:find(".", 1, true) or #modname + 1
while idx do
local name = modname:sub(1, idx - 1)
local plugins = M.loaders.module[name]
if plugins then
M.loaders.module[name] = nil
local reason = { require = modname }
if #M.loading == 0 then
local f = 3
while not reason.source do
local info = debug.getinfo(f, "S")
if not info then
break
end
if info.what ~= "C" then
reason.source = info.source:sub(2)
end
f = f + 1
end
end
M.load(plugins, reason)
end
idx = modname:find(".", idx + 1, true)
end
end
---@class Loader
---@param plugins string|LazyPlugin|string[]|LazyPlugin[]
---@param reason {[string]:string}
---@param opts? {load_start: boolean}

View File

@ -54,7 +54,7 @@ function M.setup()
-- preload core modules
local root = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h:h")
for _, name in ipairs({ "util", "config", "loader", "plugin" }) do
for _, name in ipairs({ "util", "config", "loader", "plugin", "handler" }) do
local modname = "lazy.core." .. name
---@diagnostic disable-next-line: no-unknown
package.preload[modname] = function()

View File

@ -207,8 +207,6 @@ function M.load()
if M.dirty then
Cache.dirty = true
elseif state then
require("lazy.core.loader").loaders = state.loaders
end
end
@ -217,7 +215,6 @@ function M.save()
local state = {
---@type table<string, LazySpec>
specs = {},
loaders = require("lazy.core.loader").get_loaders(),
config = Config.options,
}

View File

@ -2,32 +2,18 @@ local M = {}
---@param opts? LazyConfig
function M.setup(opts)
local done = false
-- table.insert(package.loaders, 1, function(modname)
-- if not done and modname:find("lazy") == 1 then
-- dd(modname)
-- end
-- end)
-- Loading order
-- 1. load module cache
-- 2. if changes, then reload
local cache_start = vim.loop.hrtime()
require("lazy.core.cache").setup()
local module_start = vim.loop.hrtime()
require("lazy.core.module").setup()
local require_start = vim.loop.hrtime()
local Util = require("lazy.core.util")
local Config = require("lazy.core.config")
local Loader = require("lazy.core.loader")
local Plugin = require("lazy.core.plugin")
Util.track("cache.setup", module_start - cache_start)
Util.track("module.setup", require_start - module_start)
Util.track("require.core", vim.loop.hrtime() - require_start)
Util.track("cache", module_start - cache_start)
Util.track("module", vim.loop.hrtime() - module_start)
Util.track("setup")
@ -65,7 +51,6 @@ function M.setup(opts)
if Config.plugins["lazy.nvim"] then
Config.plugins["lazy.nvim"].loaded.time = lazy_delta
end
done = true
vim.cmd("do User LazyDone")
end