Updates utils

This commit is contained in:
Dennis Schoepf 2025-04-09 19:05:02 +02:00
parent 7114d779f6
commit f9aa694520
2 changed files with 41 additions and 8 deletions

View file

@ -9,12 +9,45 @@ vim.api.nvim_create_autocmd("TextYankPost", {
pattern = "*",
})
function root_pattern(...)
local patterns = M.tbl_flatten({ ... })
function tbl_flatten(t)
--- @diagnostic disable-next-line:deprecated
return nvim_eleven and vim.iter(t):flatten(math.huge):totable() or vim.tbl_flatten(t)
end
function search_ancestors(startpath, func)
if nvim_eleven then
validate('func', func, 'function')
end
if func(startpath) then
return startpath
end
local guard = 100
for path in vim.fs.parents(startpath) do
-- Prevent infinite recursion if our algorithm breaks
guard = guard - 1
if guard == 0 then
return
end
if func(path) then
return path
end
end
end
function strip_archive_subpath(path)
-- Matches regex from zip.vim / tar.vim
path = vim.fn.substitute(path, 'zipfile://\\(.\\{-}\\)::[^\\\\].*$', '\\1', '')
path = vim.fn.substitute(path, 'tarfile:\\(.\\{-}\\)::.*$', '\\1', '')
return path
end
local function root_pattern(...)
local patterns = tbl_flatten({ ... })
return function(startpath)
startpath = M.strip_archive_subpath(startpath)
startpath = strip_archive_subpath(startpath)
for _, pattern in ipairs(patterns) do
local match = M.search_ancestors(startpath, function(path)
local match = search_ancestors(startpath, function(path)
for _, p in ipairs(vim.fn.glob(table.concat({ escape_wildcards(path), pattern }, "/"), true, true)) do
if vim.loop.fs_stat(p) then
return path
@ -92,4 +125,5 @@ return {
compile_project = compile_project,
filter = filter,
filterReactDTS = filterReactDTS,
root_pattern = root_pattern
}