From 091274fd82e70c7cda43e9a77cf127fb26a0da79 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 21 Jun 2025 11:51:14 +0200 Subject: [PATCH] nvim: Add custom lazy loading events Using lazy-events.nvim we can create custom events (such as the LazyVim equivalent LazyFile event) based on other events (i.e. a grouping), on globbing files in the current dir, or on arbitrary custom logic. For now, any plugins which require files to be present to work are loaded with the 'LazyFile' event. Any plugins which need to be loaded when opening a directory with vim are loaded with the 'StartWithDir' event (essentially only neo-tree for now). Similarly mini.starter is loaded on the `StartScreen` event. Any plugin which only makes sense to run in a `.git` dir (gitsigns etc) will only run on the `LazyProject:git` event. --- nvim/.config/nvim/lazy-lock.json | 2 + nvim/.config/nvim/lua/core/lazy.lua | 57 +++++++++++++++++++- nvim/.config/nvim/lua/plugins/base.lua | 2 +- nvim/.config/nvim/lua/plugins/git.lua | 2 +- nvim/.config/nvim/lua/plugins/linting.lua | 2 +- nvim/.config/nvim/lua/plugins/lsp.lua | 2 +- nvim/.config/nvim/lua/plugins/pickers.lua | 20 +------ nvim/.config/nvim/lua/plugins/prose.lua | 1 - nvim/.config/nvim/lua/plugins/treesitter.lua | 4 +- nvim/.config/nvim/lua/plugins/ui.lua | 2 +- 10 files changed, 66 insertions(+), 28 deletions(-) diff --git a/nvim/.config/nvim/lazy-lock.json b/nvim/.config/nvim/lazy-lock.json index 96da360..20e94d2 100644 --- a/nvim/.config/nvim/lazy-lock.json +++ b/nvim/.config/nvim/lazy-lock.json @@ -30,6 +30,7 @@ "image.nvim": { "branch": "master", "commit": "a4638ec549c6aa56264cb0371255192ff37a8a90" }, "img-clip.nvim": { "branch": "main", "commit": "0bb8b5ced45c2672c70184c87d014194b0705815" }, "jupytext.nvim": { "branch": "main", "commit": "c8baf3ad344c59b3abd461ecc17fc16ec44d0f7b" }, + "lazy-events.nvim": { "branch": "main", "commit": "63802b7ddc852bdfa29e33b158d52429276fa742" }, "lazy.nvim": { "branch": "main", "commit": "6c3bda4aca61a13a9c63f1c1d1b16b9d3be90d7a" }, "lazydev.nvim": { "branch": "main", "commit": "f59bd14a852ca43db38e3662395354cb2a9b13e0" }, "ltex_extra.nvim": { "branch": "dev", "commit": "5b37806dfbadeb8d6c0f1ee03140a60ffa40852c" }, @@ -74,6 +75,7 @@ "nvim-ts-context-commentstring": { "branch": "main", "commit": "1b212c2eee76d787bbea6aa5e92a2b534e7b4f8f" }, "nvim-web-devicons": { "branch": "master", "commit": "5b9067899ee6a2538891573500e8fd6ff008440f" }, "otter.nvim": { "branch": "main", "commit": "f3a401851c25c64220dcf2470252a1adc28308d5" }, + "pantran.nvim": { "branch": "main", "commit": "b87c3ae48cba4659587fb75abd847e5b7a7c9ca0" }, "peek.nvim": { "branch": "master", "commit": "5820d937d5414baea5f586dc2a3d912a74636e5b" }, "plenary.nvim": { "branch": "master", "commit": "857c5ac632080dba10aae49dba902ce3abf91b35" }, "quarto-nvim": { "branch": "main", "commit": "5325af3731ac9840b308791f08ad660958d76163" }, diff --git a/nvim/.config/nvim/lua/core/lazy.lua b/nvim/.config/nvim/lua/core/lazy.lua index f97e378..cf7bdd1 100644 --- a/nvim/.config/nvim/lua/core/lazy.lua +++ b/nvim/.config/nvim/lua/core/lazy.lua @@ -36,8 +36,63 @@ local spec_dir = vim.fn.stdpath("config") .. "/lua/plugins" local spec_exist = (vim.fn.isdirectory(spec_dir) ~= 0) and (#vim.fn.glob(spec_dir .. "/*.lua", nil, true) ~= 0) local spec = spec_exist and { import = "plugins" } or {} +vim.g.lazy_events_config = { + simple = { + LazyFile = { "BufReadPost", "BufNewFile", "BufWritePre" }, + }, + projects = { + git = { ".git" }, + }, + custom = { + -- We are opening a directory with vim + StartWithDir = { + event = "BufEnter", + once = true, + cond = function() + local arg = vim.fn.argv(0) + if arg == "" then + return false + end + + local stats = vim.uv.fs_stat(arg) + return (stats and stats.type == "directory") or false + end, + }, + -- Nothing but the 'start screen' is shown (no file etc) + StartScreen = { + event = "BufEnter", + once = true, + cond = function() + -- Taken from mini.starter something_is_shown() func! + -- - There are files in arguments (like `nvim foo.txt` with new file). + if vim.fn.argc() > 0 then + return false + end + local listed_buffers = vim.tbl_filter(function(buf_id) + return vim.fn.buflisted(buf_id) == 1 + end, vim.api.nvim_list_bufs()) + if #listed_buffers > 1 or vim.bo.filetype ~= "" then + return false + end + local n_lines = vim.api.nvim_buf_line_count(0) + if n_lines > 1 then + return false + end + local first_line = vim.api.nvim_buf_get_lines(0, 0, 1, true)[1] + if string.len(first_line) > 0 then + return false + end + return true + end, + }, + }, +} + require("lazy").setup({ - spec = { spec }, + spec = { + { "bwpge/lazy-events.nvim", import = "lazy-events.import" }, + spec, + }, defaults = { lazy = true, version = "*" }, performance = { rtp = { diff --git a/nvim/.config/nvim/lua/plugins/base.lua b/nvim/.config/nvim/lua/plugins/base.lua index beeb3de..ee90c77 100644 --- a/nvim/.config/nvim/lua/plugins/base.lua +++ b/nvim/.config/nvim/lua/plugins/base.lua @@ -111,7 +111,7 @@ return { { "echasnovski/mini.nvim", dependencies = { "rktjmp/fwatch.nvim" }, -- for colorscheme updating - event = "VimEnter", -- need to load pretty soon for Starter screen + event = { "VeryLazy", "StartScreen" }, -- need to load pretty soon for Starter screen config = function() -- automatic callback to invoke 'mini.base16' when colorscheme file is changed local colorsfile = vim.fn.stdpath("state") .. "/colorscheme.lua" diff --git a/nvim/.config/nvim/lua/plugins/git.lua b/nvim/.config/nvim/lua/plugins/git.lua index d466133..f0b49fa 100644 --- a/nvim/.config/nvim/lua/plugins/git.lua +++ b/nvim/.config/nvim/lua/plugins/git.lua @@ -31,7 +31,7 @@ return { }, { "lewis6991/gitsigns.nvim", -- show vcs changes on left-hand gutter - event = { "InsertEnter", "CursorHold", "VeryLazy" }, + event = { "LazyProject:git" }, cmd = "Gitsigns", init = function() if require("core.util").is_available("which-key") then diff --git a/nvim/.config/nvim/lua/plugins/linting.lua b/nvim/.config/nvim/lua/plugins/linting.lua index 173520a..e61fc4f 100644 --- a/nvim/.config/nvim/lua/plugins/linting.lua +++ b/nvim/.config/nvim/lua/plugins/linting.lua @@ -33,7 +33,7 @@ return { dependencies = { "williamboman/mason.nvim" }, }, }, - event = { "BufReadPost", "BufNewFile", "BufWritePre" }, + event = "LazyFile", opts = {}, }, } diff --git a/nvim/.config/nvim/lua/plugins/lsp.lua b/nvim/.config/nvim/lua/plugins/lsp.lua index be349a8..630179d 100644 --- a/nvim/.config/nvim/lua/plugins/lsp.lua +++ b/nvim/.config/nvim/lua/plugins/lsp.lua @@ -43,7 +43,7 @@ local lsp = { }, { "saghen/blink.cmp", optional = true }, }, - event = { "BufReadPost", "BufNewFile", "BufWritePre" }, + event = "LazyFile", opts = { servers = get_all_servers() }, init = function() if require("core.util").is_available("which-key") then diff --git a/nvim/.config/nvim/lua/plugins/pickers.lua b/nvim/.config/nvim/lua/plugins/pickers.lua index 7735de2..98e2625 100644 --- a/nvim/.config/nvim/lua/plugins/pickers.lua +++ b/nvim/.config/nvim/lua/plugins/pickers.lua @@ -18,25 +18,7 @@ return { -- file/item pickers and managers "nvim-lua/plenary.nvim", { "nvim-tree/nvim-web-devicons", optional = true }, }, - init = function() - -- ensure neo-tree gets loaded if we start vim with a directory - -- netrw-like. See https://github.com/nvim-neo-tree/neo-tree.nvim/discussions/1326 - vim.api.nvim_create_autocmd("BufEnter", { - group = vim.api.nvim_create_augroup("load_neo_tree", {}), - desc = "Loads neo-tree when opening a directory", - callback = function(args) - local stats = vim.uv.fs_stat(args.file) - - if not stats or stats.type ~= "directory" then - return - end - - require("neo-tree") - - return true - end, - }) - end, + event = "StartWithDir", cmd = "Neotree", opts = function(_, opts) opts.sources = { "filesystem", "git_status", "buffers" } diff --git a/nvim/.config/nvim/lua/plugins/prose.lua b/nvim/.config/nvim/lua/plugins/prose.lua index 3a1f9b1..e6f32bc 100644 --- a/nvim/.config/nvim/lua/plugins/prose.lua +++ b/nvim/.config/nvim/lua/plugins/prose.lua @@ -168,7 +168,6 @@ local prose_plugs = { -- easy copy paste of images into markup files { "HakonHarnes/img-clip.nvim", - event = "VeryLazy", opts = { filetypes = { quarto = { diff --git a/nvim/.config/nvim/lua/plugins/treesitter.lua b/nvim/.config/nvim/lua/plugins/treesitter.lua index e8ab0ba..de2cb9e 100644 --- a/nvim/.config/nvim/lua/plugins/treesitter.lua +++ b/nvim/.config/nvim/lua/plugins/treesitter.lua @@ -1,6 +1,7 @@ return { { "nvim-treesitter/nvim-treesitter", + event = "LazyFile", build = ":TSUpdate", -- show current cursor context at top of buffer -- improves commenting plugin above by using ts @@ -28,7 +29,7 @@ return { goto continue end for _, name in pairs(lang.ts) do - table.insert(enabled_parsers,name) + table.insert(enabled_parsers, name) end ::continue:: end @@ -110,7 +111,6 @@ return { }, }) end, - event = { "VeryLazy" }, cmd = { "TSBufDisable", "TSBufEnable", diff --git a/nvim/.config/nvim/lua/plugins/ui.lua b/nvim/.config/nvim/lua/plugins/ui.lua index 49b614f..cd26f8f 100644 --- a/nvim/.config/nvim/lua/plugins/ui.lua +++ b/nvim/.config/nvim/lua/plugins/ui.lua @@ -94,7 +94,7 @@ return { "folke/todo-comments.nvim", dependencies = { "nvim-lua/plenary.nvim" }, opts = {}, - event = "VeryLazy", + event = "LazyFile", }, { "OXY2DEV/helpview.nvim",