Skip to content

Commit 5b7b8c5

Browse files
committed
feat(cache): automatically reset topmods when a user changes a file for a path on the rtp
1 parent ea1a044 commit 5b7b8c5

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lua/lazy/core/cache.lua

+14-3
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,19 @@ function M.enable()
369369
break
370370
end
371371
end
372-
-- TODO: add an autocmd on BufWritePost that checks if its in a /lua folder
373-
-- if thats the case, then reset the plugin path.
374-
-- This will make sure we can properly load new top-level lua modules
372+
373+
-- this will reset the top-mods in case someone adds a new
374+
-- top-level lua module to a path already on the rtp
375+
vim.api.nvim_create_autocmd("BufWritePost", {
376+
group = vim.api.nvim_create_augroup("cache_topmods_reset", { clear = true }),
377+
callback = function(event)
378+
local bufname = event.match ---@type string
379+
local idx = bufname:find("/lua/", 1, true)
380+
if idx then
381+
M.reset(bufname:sub(1, idx - 1))
382+
end
383+
end,
384+
})
375385
end
376386

377387
--- Disables the cache:
@@ -391,6 +401,7 @@ function M.disable()
391401
end
392402
end
393403
table.insert(package.loaders, 2, vim._load_package)
404+
vim.api.nvim_del_augroup_by_name("cache_topmods_reset")
394405
end
395406

396407
--- Return the top-level `/lua/*` modules for this path

0 commit comments

Comments
 (0)