chore: run stylua [generated] (#460)
* chore: add stylua Signed-off-by: Aaron Pham <contact@aarnphm.xyz> * chore: running stylua Signed-off-by: Aaron Pham <contact@aarnphm.xyz> --------- Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
-- COPIED and MODIFIED from https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/util/root.lua
|
||||
local Utils = require("avante.utils")
|
||||
local Utils = require "avante.utils"
|
||||
|
||||
---@class avante.utils.root
|
||||
---@overload fun(): string
|
||||
local M = setmetatable({}, {
|
||||
__call = function(m)
|
||||
return m.get()
|
||||
end,
|
||||
__call = function(m) return m.get() end,
|
||||
})
|
||||
|
||||
---@class AvanteRoot
|
||||
@@ -22,25 +20,19 @@ M.spec = { "lsp", { ".git", "lua" }, "cwd" }
|
||||
|
||||
M.detectors = {}
|
||||
|
||||
function M.detectors.cwd()
|
||||
return { vim.uv.cwd() }
|
||||
end
|
||||
function M.detectors.cwd() return { vim.uv.cwd() } end
|
||||
|
||||
---@param buf number
|
||||
function M.detectors.lsp(buf)
|
||||
local bufpath = M.bufpath(buf)
|
||||
if not bufpath then
|
||||
return {}
|
||||
end
|
||||
if not bufpath then return {} end
|
||||
local roots = {} ---@type string[]
|
||||
for _, client in pairs(Utils.lsp.get_clients({ bufnr = buf })) do
|
||||
for _, client in pairs(Utils.lsp.get_clients { bufnr = buf }) do
|
||||
local workspace = client.config.workspace_folders
|
||||
for _, ws in pairs(workspace or {}) do
|
||||
roots[#roots + 1] = vim.uri_to_fname(ws.uri)
|
||||
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
|
||||
end
|
||||
return vim.tbl_filter(function(path)
|
||||
path = Utils.norm(path)
|
||||
@@ -54,30 +46,20 @@ function M.detectors.pattern(buf, patterns)
|
||||
local path = M.bufpath(buf) or vim.uv.cwd()
|
||||
local pattern = vim.fs.find(function(name)
|
||||
for _, p in ipairs(patterns) do
|
||||
if name == p then
|
||||
return true
|
||||
end
|
||||
if p:sub(1, 1) == "*" and name:find(vim.pesc(p:sub(2)) .. "$") then
|
||||
return true
|
||||
end
|
||||
if name == p then return true end
|
||||
if p:sub(1, 1) == "*" and name:find(vim.pesc(p:sub(2)) .. "$") then return true end
|
||||
end
|
||||
return false
|
||||
end, { path = path, upward = true })[1]
|
||||
return pattern and { vim.fs.dirname(pattern) } or {}
|
||||
end
|
||||
|
||||
function M.bufpath(buf)
|
||||
return M.realpath(vim.api.nvim_buf_get_name(assert(buf)))
|
||||
end
|
||||
function M.bufpath(buf) return M.realpath(vim.api.nvim_buf_get_name(assert(buf))) end
|
||||
|
||||
function M.cwd()
|
||||
return M.realpath(vim.uv.cwd()) or ""
|
||||
end
|
||||
function M.cwd() return M.realpath(vim.uv.cwd()) or "" end
|
||||
|
||||
function M.realpath(path)
|
||||
if path == "" or path == nil then
|
||||
return nil
|
||||
end
|
||||
if path == "" or path == nil then return nil end
|
||||
path = vim.uv.fs_realpath(path) or path
|
||||
return Utils.norm(path)
|
||||
end
|
||||
@@ -90,9 +72,7 @@ function M.resolve(spec)
|
||||
elseif type(spec) == "function" then
|
||||
return spec
|
||||
end
|
||||
return function(buf)
|
||||
return M.detectors.pattern(buf, spec)
|
||||
end
|
||||
return function(buf) return M.detectors.pattern(buf, spec) end
|
||||
end
|
||||
|
||||
---@param opts? { buf?: number, spec?: AvanteRootSpec[], all?: boolean }
|
||||
@@ -109,18 +89,12 @@ function M.detect(opts)
|
||||
local roots = {} ---@type string[]
|
||||
for _, p in ipairs(paths) do
|
||||
local pp = M.realpath(p)
|
||||
if pp and not vim.tbl_contains(roots, pp) then
|
||||
roots[#roots + 1] = pp
|
||||
end
|
||||
if pp and not vim.tbl_contains(roots, pp) then roots[#roots + 1] = pp end
|
||||
end
|
||||
table.sort(roots, function(a, b)
|
||||
return #a > #b
|
||||
end)
|
||||
table.sort(roots, function(a, b) return #a > #b end)
|
||||
if #roots > 0 then
|
||||
ret[#ret + 1] = { spec = spec, paths = roots }
|
||||
if opts.all == false then
|
||||
break
|
||||
end
|
||||
if opts.all == false then break end
|
||||
end
|
||||
end
|
||||
return ret
|
||||
@@ -141,13 +115,11 @@ function M.get(opts)
|
||||
local buf = opts.buf or vim.api.nvim_get_current_buf()
|
||||
local ret = M.cache[buf]
|
||||
if not ret then
|
||||
local roots = M.detect({ all = false, buf = buf })
|
||||
local roots = M.detect { all = false, buf = buf }
|
||||
ret = roots[1] and roots[1].paths[1] or vim.uv.cwd()
|
||||
M.cache[buf] = ret
|
||||
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
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user