perf: added support for LazyFile event that properly loads file based plugins without blocking the ui

This commit is contained in:
Folke Lemaitre 2023-10-04 10:45:10 +02:00
parent f15dd301e5
commit 936d74bb61
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
8 changed files with 58 additions and 14 deletions

View file

@ -124,8 +124,9 @@ function M.setup(opts)
M.load("autocmds")
end
local group = vim.api.nvim_create_augroup("LazyVim", { clear = true })
vim.api.nvim_create_autocmd("User", {
group = vim.api.nvim_create_augroup("LazyVim", { clear = true }),
group = group,
pattern = "VeryLazy",
callback = function()
if lazy_autocmds then
@ -135,6 +136,8 @@ function M.setup(opts)
end,
})
M.lazy_file()
require("lazy.core.util").try(function()
if type(M.colorscheme) == "function" then
M.colorscheme()
@ -150,6 +153,40 @@ function M.setup(opts)
})
end
-- Properly load file based plugins without blocking the UI
function M.lazy_file()
local events = {} ---@type {event: string, pattern?: string, buf: number, data?: any}[]
local function load()
if #events == 0 then
return
end
vim.api.nvim_del_augroup_by_name("lazy_file")
vim.api.nvim_exec_autocmds("User", { pattern = "LazyFile", modeline = false })
for _, event in ipairs(events) do
vim.api.nvim_exec_autocmds(event.event, {
pattern = event.pattern,
modeline = false,
buffer = event.buf,
data = event.data,
})
end
events = {}
end
-- schedule wrap so that nested autocmds are executed
-- and the UI can continue rendering without blocking
load = vim.schedule_wrap(load)
vim.api.nvim_create_autocmd({ "BufReadPost", "BufWritePost", "BufNewFile" }, {
group = vim.api.nvim_create_augroup("lazy_file", { clear = true }),
callback = function(event)
table.insert(events, event)
load()
end,
})
end
---@param range? string
function M.has(range)
local Semver = require("lazy.manage.semver")
@ -214,6 +251,14 @@ function M.init()
end
return add(self, plugin, ...)
end
-- Add support for the LazyFile event
local Event = require("lazy.core.handler.event")
local _event = Event._event
---@diagnostic disable-next-line: duplicate-set-field
Event._event = function(self, value)
return value == "LazyFile" and "User LazyFile" or _event(self, value)
end
end
end