fix: get project root (#2026)
This commit is contained in:
@@ -28,9 +28,10 @@ function M.detectors.lsp(buf)
|
|||||||
local bufpath = M.bufpath(buf)
|
local bufpath = M.bufpath(buf)
|
||||||
if not bufpath then return {} end
|
if not bufpath then return {} end
|
||||||
local roots = {} ---@type string[]
|
local roots = {} ---@type string[]
|
||||||
for _, client in pairs(Utils.lsp.get_clients({ bufnr = buf })) do
|
local lsp_clients = Utils.lsp.get_clients({ bufnr = buf })
|
||||||
|
for _, client in ipairs(lsp_clients) do
|
||||||
local workspace = client.config.workspace_folders
|
local workspace = client.config.workspace_folders
|
||||||
for _, ws in pairs(workspace or {}) do
|
for _, ws in ipairs(workspace or {}) do
|
||||||
roots[#roots + 1] = vim.uri_to_fname(ws.uri)
|
roots[#roots + 1] = vim.uri_to_fname(ws.uri)
|
||||||
end
|
end
|
||||||
if client.root_dir then roots[#roots + 1] = client.root_dir end
|
if client.root_dir then roots[#roots + 1] = client.root_dir end
|
||||||
@@ -135,8 +136,8 @@ M.cache = {}
|
|||||||
---@param opts? {normalize?:boolean, buf?:number}
|
---@param opts? {normalize?:boolean, buf?:number}
|
||||||
---@return string
|
---@return string
|
||||||
function M.get(opts)
|
function M.get(opts)
|
||||||
if Config.behaviour.use_cwd_as_project_root then
|
|
||||||
local cwd = vim.uv.cwd()
|
local cwd = vim.uv.cwd()
|
||||||
|
if Config.behaviour.use_cwd_as_project_root then
|
||||||
if cwd and cwd ~= "" then return cwd end
|
if cwd and cwd ~= "" then return cwd end
|
||||||
end
|
end
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
@@ -147,6 +148,7 @@ function M.get(opts)
|
|||||||
ret = roots[1] and roots[1].paths[1] or vim.uv.cwd()
|
ret = roots[1] and roots[1].paths[1] or vim.uv.cwd()
|
||||||
M.cache[buf] = ret
|
M.cache[buf] = ret
|
||||||
end
|
end
|
||||||
|
if cwd ~= nil and #ret > #cwd then ret = cwd end
|
||||||
if opts and opts.normalize then return ret end
|
if opts and opts.normalize then return ret end
|
||||||
return Utils.is_win() and ret:gsub("/", "\\") or ret
|
return Utils.is_win() and ret:gsub("/", "\\") or ret
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user