尝试修复 add file 作为上下文时会找不到文件
Some checks are pending
Lua CI / unit tests (ubuntu-22.04, v0.10.0/nvim-linux64.tar.gz) (push) Waiting to run
Lua CI / Check Lua style (push) Waiting to run
Lua CI / Lint Lua (push) Waiting to run

This commit is contained in:
zhangkun9038@dingtalk.com 2025-02-17 00:01:17 +08:00
parent a6288b8357
commit 11c0d13b9a

View File

@ -81,9 +81,13 @@ function M.detect(opts)
opts.spec = opts.spec or type(vim.g.root_spec) == "table" and vim.g.root_spec or M.spec opts.spec = opts.spec or type(vim.g.root_spec) == "table" and vim.g.root_spec or M.spec
opts.buf = (opts.buf == nil or opts.buf == 0) and vim.api.nvim_get_current_buf() or opts.buf opts.buf = (opts.buf == nil or opts.buf == 0) and vim.api.nvim_get_current_buf() or opts.buf
print("Detection Specs:", vim.inspect(opts.spec))
local ret = {} ---@type AvanteRoot[] local ret = {} ---@type AvanteRoot[]
for _, spec in ipairs(opts.spec) do for _, spec in ipairs(opts.spec) do
local paths = M.resolve(spec)(opts.buf) local paths = M.resolve(spec)(opts.buf)
print("Paths for Spec:", spec, vim.inspect(paths))
paths = paths or {} paths = paths or {}
paths = type(paths) == "table" and paths or { paths } paths = type(paths) == "table" and paths or { paths }
local roots = {} ---@type string[] local roots = {} ---@type string[]
@ -97,9 +101,11 @@ function M.detect(opts)
if opts.all == false then break end if opts.all == false then break end
end end
end end
print("Detected Roots:", vim.inspect(ret))
return ret return ret
end end
---@type table<number, string> ---@type table<number, string>
M.cache = {} M.cache = {}
@ -113,15 +119,27 @@ M.cache = {}
function M.get(opts) function M.get(opts)
opts = opts or {} opts = opts or {}
local buf = opts.buf or vim.api.nvim_get_current_buf() local buf = opts.buf or vim.api.nvim_get_current_buf()
print("Current Buffer:", buf)
local ret = M.cache[buf] local ret = M.cache[buf]
print("Cached Root:", ret)
if not ret then if not ret then
local roots = M.detect({ all = false, buf = buf }) local roots = M.detect({ all = false, buf = buf })
print("Detected Roots:", vim.inspect(roots))
ret = roots[1] and roots[1].paths[1] or vim.uv.cwd() ret = roots[1] and roots[1].paths[1] or vim.uv.cwd()
print("Selected Root:", ret)
M.cache[buf] = ret M.cache[buf] = ret
end end
if opts and opts.normalize then return ret end if opts and opts.normalize then return ret end
-- Ensure a non-empty return value -- Ensure a non-empty return value
ret = ret ~= "" and ret or vim.uv.cwd() ret = ret ~= "" and ret or vim.uv.cwd()
print("Final Root:", ret)
return Utils.is_win() and ret:gsub("/", "\\") or ret return Utils.is_win() and ret:gsub("/", "\\") or ret
end end