feat(cache): automatically reset topmods when a user changes a file for a path on the rtp

This commit is contained in:
Folke Lemaitre 2023-03-14 19:54:44 +01:00
parent ea1a044e3c
commit 5b7b8c5149
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 14 additions and 3 deletions

View File

@ -369,9 +369,19 @@ function M.enable()
break break
end end
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 reset the top-mods in case someone adds a new
-- This will make sure we can properly load new top-level lua modules -- 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 end
--- Disables the cache: --- Disables the cache:
@ -391,6 +401,7 @@ function M.disable()
end end
end end
table.insert(package.loaders, 2, vim._load_package) table.insert(package.loaders, 2, vim._load_package)
vim.api.nvim_del_augroup_by_name("cache_topmods_reset")
end end
--- Return the top-level `/lua/*` modules for this path --- Return the top-level `/lua/*` modules for this path