fix: reset cache for project root if buf name changed (#2638)

This commit is contained in:
brook hong
2025-08-25 10:30:00 +08:00
committed by GitHub
parent 2791db9984
commit ce2c9ea5a2

View File

@@ -127,6 +127,7 @@ end
---@type table<number, string>
M.cache = {}
local buf_names = {}
-- returns the root directory based on:
-- * lsp workspace folders
@@ -142,10 +143,12 @@ function M.get(opts)
end
opts = opts or {}
local buf = opts.buf or vim.api.nvim_get_current_buf()
local ret = M.cache[buf]
local buf_name = vim.api.nvim_buf_get_name(buf)
local ret = buf_names[buf] == buf_name and M.cache[buf] or nil
if not ret then
local roots = M.detect({ all = false, buf = buf })
ret = roots[1] and roots[1].paths[1] or vim.uv.cwd()
buf_names[buf] = buf_name
M.cache[buf] = ret
end
if cwd ~= nil and #ret > #cwd then ret = cwd end