perf(root): cache root detection. Fixes #1753
This commit is contained in:
@ -180,10 +180,7 @@ function M.setup(opts)
|
|||||||
|
|
||||||
Util.format.setup()
|
Util.format.setup()
|
||||||
Util.news.setup()
|
Util.news.setup()
|
||||||
|
Util.root.setup()
|
||||||
vim.api.nvim_create_user_command("LazyRoot", function()
|
|
||||||
Util.root.info()
|
|
||||||
end, { desc = "LazyVim roots for the current buffer" })
|
|
||||||
|
|
||||||
vim.api.nvim_create_user_command("LazyExtras", function()
|
vim.api.nvim_create_user_command("LazyExtras", function()
|
||||||
Util.extras.show()
|
Util.extras.show()
|
||||||
|
@ -136,18 +136,38 @@ function M.info()
|
|||||||
return roots[1] and roots[1].paths[1] or vim.loop.cwd()
|
return roots[1] and roots[1].paths[1] or vim.loop.cwd()
|
||||||
end
|
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:
|
-- returns the root directory based on:
|
||||||
-- * lsp workspace folders
|
-- * lsp workspace folders
|
||||||
-- * lsp root_dir
|
-- * lsp root_dir
|
||||||
-- * root pattern of filename of the current buffer
|
-- * root pattern of filename of the current buffer
|
||||||
-- * root pattern of cwd
|
-- * root pattern of cwd
|
||||||
---@param opts {normalize?:boolean}
|
---@param opts? {normalize?:boolean}
|
||||||
---@return string
|
---@return string
|
||||||
function M.get(opts)
|
function M.get(opts)
|
||||||
opts = opts or {}
|
local buf = vim.api.nvim_get_current_buf()
|
||||||
local roots = M.detect({ all = false })
|
local ret = M.cache[buf]
|
||||||
local ret = roots[1] and roots[1].paths[1] or vim.loop.cwd()
|
if not ret then
|
||||||
if opts.normalize 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
|
return ret
|
||||||
end
|
end
|
||||||
return Util.is_win() and ret:gsub("/", "\\") or ret
|
return Util.is_win() and ret:gsub("/", "\\") or ret
|
||||||
|
Reference in New Issue
Block a user