feat(root): allow custom functions as part of vim.g.root_spec
This commit is contained in:
@ -6,7 +6,10 @@ vim.g.maplocalleader = "\\"
|
|||||||
vim.g.autoformat = true
|
vim.g.autoformat = true
|
||||||
|
|
||||||
-- LazyVim root dir detection
|
-- LazyVim root dir detection
|
||||||
-- Each entry can be a detector function like `lsp` or `cwd`, or a pattern like `.git` or `lua`.
|
-- Each entry can be:
|
||||||
|
-- * the name of a detector function like `lsp` or `cwd`
|
||||||
|
-- * a pattern or array of patterns like `.git` or `lua`.
|
||||||
|
-- * a function with signature `function(buf) -> string|string[]`
|
||||||
vim.g.root_spec = { "lsp", { ".git", "lua" }, "cwd" }
|
vim.g.root_spec = { "lsp", { ".git", "lua" }, "cwd" }
|
||||||
|
|
||||||
local opt = vim.opt
|
local opt = vim.opt
|
||||||
|
@ -12,7 +12,9 @@ local M = setmetatable({}, {
|
|||||||
---@field paths string[]
|
---@field paths string[]
|
||||||
---@field spec LazyRootSpec
|
---@field spec LazyRootSpec
|
||||||
|
|
||||||
---@alias LazyRootSpec string|string[]
|
---@alias LazyRootFn fun(buf: number): (string|string[])
|
||||||
|
|
||||||
|
---@alias LazyRootSpec string|string[]|LazyRootFn
|
||||||
|
|
||||||
---@type LazyRootSpec[]
|
---@type LazyRootSpec[]
|
||||||
M.spec = { "lsp", { ".git", "lua" }, "cwd" }
|
M.spec = { "lsp", { ".git", "lua" }, "cwd" }
|
||||||
@ -60,6 +62,19 @@ function M.realpath(path)
|
|||||||
return Util.norm(path)
|
return Util.norm(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param spec LazyRootSpec
|
||||||
|
---@return LazyRootFn
|
||||||
|
function M.resolve(spec)
|
||||||
|
if M.detectors[spec] then
|
||||||
|
return M.detectors[spec]
|
||||||
|
elseif type(spec) == "function" then
|
||||||
|
return spec
|
||||||
|
end
|
||||||
|
return function(buf)
|
||||||
|
return M.detectors.pattern(buf, spec)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---@param opts? { buf?: number, spec?: LazyRootSpec[], all?: boolean }
|
---@param opts? { buf?: number, spec?: LazyRootSpec[], all?: boolean }
|
||||||
function M.detect(opts)
|
function M.detect(opts)
|
||||||
opts = opts or {}
|
opts = opts or {}
|
||||||
@ -68,7 +83,9 @@ function M.detect(opts)
|
|||||||
|
|
||||||
local ret = {} ---@type LazyRoot[]
|
local ret = {} ---@type LazyRoot[]
|
||||||
for _, spec in ipairs(opts.spec) do
|
for _, spec in ipairs(opts.spec) do
|
||||||
local paths = M.detectors[spec] and M.detectors[spec](opts.buf) or M.detectors.pattern(opts.buf, spec)
|
local paths = M.resolve(spec)(opts.buf)
|
||||||
|
paths = paths or {}
|
||||||
|
paths = type(paths) == "table" and paths or { paths }
|
||||||
local roots = {} ---@type string[]
|
local roots = {} ---@type string[]
|
||||||
for _, p in ipairs(paths) do
|
for _, p in ipairs(paths) do
|
||||||
local pp = M.realpath(p)
|
local pp = M.realpath(p)
|
||||||
|
Reference in New Issue
Block a user