diff --git a/lua/lazyvim/config/init.lua b/lua/lazyvim/config/init.lua index 964ca5b9..36335f7b 100644 --- a/lua/lazyvim/config/init.lua +++ b/lua/lazyvim/config/init.lua @@ -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 diff --git a/lua/lazyvim/plugins/editor.lua b/lua/lazyvim/plugins/editor.lua index 20b466fd..3f4c5d52 100644 --- a/lua/lazyvim/plugins/editor.lua +++ b/lua/lazyvim/plugins/editor.lua @@ -310,7 +310,7 @@ return { -- hunks in a commit. { "lewis6991/gitsigns.nvim", - event = { "BufReadPre", "BufNewFile" }, + event = "LazyFile", opts = { signs = { add = { text = "▎" }, @@ -349,7 +349,7 @@ return { -- instances. { "RRethy/vim-illuminate", - event = { "BufReadPost", "BufNewFile" }, + event = "LazyFile", opts = { delay = 200, large_file_cutoff = 2000, @@ -440,7 +440,7 @@ return { { "folke/todo-comments.nvim", cmd = { "TodoTrouble", "TodoTelescope" }, - event = { "BufReadPost", "BufNewFile" }, + event = "LazyFile", config = true, -- stylua: ignore keys = { diff --git a/lua/lazyvim/plugins/extras/lang/cmake.lua b/lua/lazyvim/plugins/extras/lang/cmake.lua index cd802edf..bc7b235a 100644 --- a/lua/lazyvim/plugins/extras/lang/cmake.lua +++ b/lua/lazyvim/plugins/extras/lang/cmake.lua @@ -45,6 +45,6 @@ return { { "Civitasv/cmake-tools.nvim", opts = {}, - event = "BufRead", + event = "LazyFile", }, } diff --git a/lua/lazyvim/plugins/extras/linting/nvim-lint.lua b/lua/lazyvim/plugins/extras/linting/nvim-lint.lua index 879b2177..2d750309 100644 --- a/lua/lazyvim/plugins/extras/linting/nvim-lint.lua +++ b/lua/lazyvim/plugins/extras/linting/nvim-lint.lua @@ -1,7 +1,7 @@ return { { "mfussenegger/nvim-lint", - event = "BufReadPost", + event = "LazyFile", opts = { -- Event to trigger linters events = { "BufWritePost", "BufReadPost", "InsertLeave" }, @@ -43,7 +43,6 @@ return { end function M.lint() - local lint = require("lint") local names = lint.linters_by_ft[vim.bo.filetype] or {} local ctx = { filename = vim.api.nvim_buf_get_name(0) } ctx.dirname = vim.fn.fnamemodify(ctx.filename, ":h") diff --git a/lua/lazyvim/plugins/extras/util/mini-hipatterns.lua b/lua/lazyvim/plugins/extras/util/mini-hipatterns.lua index 1367f908..43b8c49d 100644 --- a/lua/lazyvim/plugins/extras/util/mini-hipatterns.lua +++ b/lua/lazyvim/plugins/extras/util/mini-hipatterns.lua @@ -5,7 +5,7 @@ M.hl = {} M.plugin = { "echasnovski/mini.hipatterns", - event = "BufReadPre", + event = "LazyFile", opts = function() local hi = require("mini.hipatterns") return { diff --git a/lua/lazyvim/plugins/lsp/init.lua b/lua/lazyvim/plugins/lsp/init.lua index 178102e3..211385f9 100644 --- a/lua/lazyvim/plugins/lsp/init.lua +++ b/lua/lazyvim/plugins/lsp/init.lua @@ -2,7 +2,7 @@ return { -- lspconfig { "neovim/nvim-lspconfig", - event = { "BufReadPre", "BufNewFile" }, + event = "LazyFile", dependencies = { { "folke/neoconf.nvim", cmd = "Neoconf", config = false, dependencies = { "nvim-lspconfig" } }, { "folke/neodev.nvim", opts = {} }, @@ -203,7 +203,7 @@ return { -- formatters { "nvimtools/none-ls.nvim", - event = { "BufReadPre", "BufNewFile" }, + event = "LazyFile", dependencies = { "mason.nvim" }, opts = function() local nls = require("null-ls") diff --git a/lua/lazyvim/plugins/treesitter.lua b/lua/lazyvim/plugins/treesitter.lua index 0b9b6895..f6d36519 100644 --- a/lua/lazyvim/plugins/treesitter.lua +++ b/lua/lazyvim/plugins/treesitter.lua @@ -6,7 +6,7 @@ return { "nvim-treesitter/nvim-treesitter", version = false, -- last release is way too old and doesn't work on Windows build = ":TSUpdate", - event = { "BufReadPost", "BufNewFile" }, + event = { "LazyFile", "VeryLazy" }, dependencies = { { "nvim-treesitter/nvim-treesitter-textobjects", diff --git a/lua/lazyvim/plugins/ui.lua b/lua/lazyvim/plugins/ui.lua index d8d6e181..677b902a 100644 --- a/lua/lazyvim/plugins/ui.lua +++ b/lua/lazyvim/plugins/ui.lua @@ -53,7 +53,7 @@ return { -- tabs, which include filetype icons and close buttons. { "akinsho/bufferline.nvim", - event = "VeryLazy", + event = "LazyFile", keys = { { "bp", "BufferLineTogglePin", desc = "Toggle pin" }, { "bP", "BufferLineGroupClose ungrouped", desc = "Delete non-pinned buffers" }, @@ -166,7 +166,7 @@ return { -- indent guides for Neovim { "lukas-reineke/indent-blankline.nvim", - event = { "BufReadPost", "BufNewFile" }, + event = "LazyFile", opts = { indent = { char = "│", @@ -197,7 +197,7 @@ return { { "echasnovski/mini.indentscope", version = false, -- wait till new 0.7.0 release to put it back on semver - event = { "BufReadPre", "BufNewFile" }, + event = "LazyFile", opts = { -- symbol = "▏", symbol = "│",