From 5b7b8c51495de8ced973cc23f0a58cadd21de875 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Tue, 14 Mar 2023 19:54:44 +0100 Subject: [PATCH] feat(cache): automatically reset topmods when a user changes a file for a path on the rtp --- lua/lazy/core/cache.lua | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lua/lazy/core/cache.lua b/lua/lazy/core/cache.lua index d96875a..522b2ac 100644 --- a/lua/lazy/core/cache.lua +++ b/lua/lazy/core/cache.lua @@ -369,9 +369,19 @@ function M.enable() break end end - -- TODO: add an autocmd on BufWritePost that checks if its in a /lua folder - -- if thats the case, then reset the plugin path. - -- This will make sure we can properly load new top-level lua modules + + -- this will reset the top-mods in case someone adds a new + -- top-level lua module to a path already on the rtp + vim.api.nvim_create_autocmd("BufWritePost", { + group = vim.api.nvim_create_augroup("cache_topmods_reset", { clear = true }), + callback = function(event) + local bufname = event.match ---@type string + local idx = bufname:find("/lua/", 1, true) + if idx then + M.reset(bufname:sub(1, idx - 1)) + end + end, + }) end --- Disables the cache: @@ -391,6 +401,7 @@ function M.disable() end end table.insert(package.loaders, 2, vim._load_package) + vim.api.nvim_del_augroup_by_name("cache_topmods_reset") end --- Return the top-level `/lua/*` modules for this path