fix(config): fixed issues related to LazyFile. Fixes #1601

This commit is contained in:
Folke Lemaitre 2023-10-06 15:40:27 +02:00
parent a1c5886947
commit 6e0e01f5b4
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 34 additions and 11 deletions

View file

@ -32,9 +32,6 @@ vim.api.nvim_create_autocmd({ "VimResized" }, {
vim.api.nvim_create_autocmd("BufReadPost", { vim.api.nvim_create_autocmd("BufReadPost", {
group = augroup("last_loc"), group = augroup("last_loc"),
callback = function(event) callback = function(event)
if event.data and event.data.lazy_file then
return
end
local exclude = { "gitcommit" } local exclude = { "gitcommit" }
local buf = event.buf local buf = event.buf
if vim.tbl_contains(exclude, vim.bo[buf].filetype) then if vim.tbl_contains(exclude, vim.bo[buf].filetype) then

View file

@ -2,6 +2,7 @@
local M = {} local M = {}
M.lazy_version = ">=9.1.0" M.lazy_version = ">=9.1.0"
M.use_lazy_file = true
---@class LazyVimOptions ---@class LazyVimOptions
local defaults = { local defaults = {
@ -136,7 +137,10 @@ function M.setup(opts)
end, end,
}) })
M.lazy_file() M.use_lazy_file = M.use_lazy_file and require("lazy.core.handler.event").trigger_events == nil
if M.use_lazy_file then
M.lazy_file()
end
require("lazy.core.util").try(function() require("lazy.core.util").try(function()
if type(M.colorscheme) == "function" then if type(M.colorscheme) == "function" then
@ -155,31 +159,50 @@ end
-- Properly load file based plugins without blocking the UI -- Properly load file based plugins without blocking the UI
function M.lazy_file() function M.lazy_file()
local events = {} ---@type {event: string, pattern?: string, buf: number, data?: any}[] local events = {} ---@type {event: string, buf: number, data?: any}[]
local function load() local function load()
if #events == 0 then if #events == 0 then
return return
end end
local Event = require("lazy.core.handler.event")
local Util = require("lazy.core.util")
vim.api.nvim_del_augroup_by_name("lazy_file") vim.api.nvim_del_augroup_by_name("lazy_file")
Util.track({ event = "LazyFile" })
---@type table<string,string[]>
local skips = { FileType = Event.get_augroups("FileType") }
for _, event in ipairs(events) do
skips[event.event] = skips[event.event] or Event.get_augroups(event.event)
end
vim.api.nvim_exec_autocmds("User", { pattern = "LazyFile", modeline = false }) vim.api.nvim_exec_autocmds("User", { pattern = "LazyFile", modeline = false })
for _, event in ipairs(events) do for _, event in ipairs(events) do
vim.api.nvim_exec_autocmds(event.event, { Event.trigger({
pattern = event.pattern, event = event.event,
modeline = false, exclude = skips[event.event],
buffer = event.buf, data = event.data,
data = { lazy_file = true }, buf = event.buf,
}) })
if vim.bo[event.buf].filetype then
Event.trigger({
event = "FileType",
exclude = skips.FileType,
buf = event.buf,
})
end
end end
vim.api.nvim_exec_autocmds("CursorMoved", { modeline = false }) vim.api.nvim_exec_autocmds("CursorMoved", { modeline = false })
events = {} events = {}
Util.track()
end end
-- schedule wrap so that nested autocmds are executed -- schedule wrap so that nested autocmds are executed
-- and the UI can continue rendering without blocking -- and the UI can continue rendering without blocking
load = vim.schedule_wrap(load) load = vim.schedule_wrap(load)
vim.api.nvim_create_autocmd({ "BufReadPost", "BufWritePost", "BufNewFile" }, { vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
group = vim.api.nvim_create_augroup("lazy_file", { clear = true }), group = vim.api.nvim_create_augroup("lazy_file", { clear = true }),
callback = function(event) callback = function(event)
table.insert(events, event) table.insert(events, event)
@ -251,6 +274,9 @@ function M.init()
) )
plugin[1] = M.renames[plugin[1]] plugin[1] = M.renames[plugin[1]]
end end
if not M.use_lazy_file and type(plugin) == "table" and plugin.event == "LazyFile" then
plugin.event = { "BufReadPost", "BufNewFile" }
end
return add(self, plugin, ...) return add(self, plugin, ...)
end end