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.
This commit is contained in:
Marty Oehme 2025-06-21 11:51:14 +02:00
parent 71ddce4119
commit 091274fd82
Signed by: Marty
GPG key ID: 4E535BC19C61886E
10 changed files with 66 additions and 28 deletions

View file

@ -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" },

View file

@ -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 = {

View file

@ -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"

View file

@ -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

View file

@ -33,7 +33,7 @@ return {
dependencies = { "williamboman/mason.nvim" },
},
},
event = { "BufReadPost", "BufNewFile", "BufWritePre" },
event = "LazyFile",
opts = {},
},
}

View file

@ -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

View file

@ -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" }

View file

@ -168,7 +168,6 @@ local prose_plugs = {
-- easy copy paste of images into markup files
{
"HakonHarnes/img-clip.nvim",
event = "VeryLazy",
opts = {
filetypes = {
quarto = {

View file

@ -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",

View file

@ -94,7 +94,7 @@ return {
"folke/todo-comments.nvim",
dependencies = { "nvim-lua/plenary.nvim" },
opts = {},
event = "VeryLazy",
event = "LazyFile",
},
{
"OXY2DEV/helpview.nvim",