perf(root): cache root detection. Fixes #1753

This commit is contained in:
Folke Lemaitre
2023-10-16 12:42:54 +02:00
parent db175ef3f6
commit 98db7ec0d2
2 changed files with 26 additions and 9 deletions

View File

@ -136,18 +136,38 @@ function M.info()
return roots[1] and roots[1].paths[1] or vim.loop.cwd()
end
---@type table<number, string>
M.cache = {}
function M.setup()
vim.api.nvim_create_user_command("LazyRoot", function()
Util.root.info()
end, { desc = "LazyVim roots for the current buffer" })
vim.api.nvim_create_autocmd({ "LspAttach", "BufWritePost" }, {
group = vim.api.nvim_create_augroup("lazyvim_root_cache", { clear = true }),
callback = function(event)
M.cache[event.buf] = nil
end,
})
end
-- returns the root directory based on:
-- * lsp workspace folders
-- * lsp root_dir
-- * root pattern of filename of the current buffer
-- * root pattern of cwd
---@param opts {normalize?:boolean}
---@param opts? {normalize?:boolean}
---@return string
function M.get(opts)
opts = opts or {}
local roots = M.detect({ all = false })
local ret = roots[1] and roots[1].paths[1] or vim.loop.cwd()
if opts.normalize then
local buf = vim.api.nvim_get_current_buf()
local ret = M.cache[buf]
if not ret then
local roots = M.detect({ all = false })
ret = roots[1] and roots[1].paths[1] or vim.loop.cwd()
M.cache[buf] = ret
end
if opts and opts.normalize then
return ret
end
return Util.is_win() and ret:gsub("/", "\\") or ret