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

7
flake.lock generated
View file

@ -7,16 +7,15 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1744117652, "lastModified": 1744208565,
"narHash": "sha256-t7dFCDl4vIOOUMhEZnJF15aAzkpaup9x4ZRGToDFYWI=", "narHash": "sha256-vG3JJOar/r8ognz7wuwMtOJ8Knu1MMlOzHB1N6R2MbY=",
"owner": "nix-community", "owner": "nix-community",
"repo": "home-manager", "repo": "home-manager",
"rev": "b4e98224ad1336751a2ac7493967a4c9f6d9cb3f", "rev": "542efdf2dfac351498f534eb71671525b9bd45ed",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nix-community", "owner": "nix-community",
"ref": "release-24.11",
"repo": "home-manager", "repo": "home-manager",
"type": "github" "type": "github"
} }

View file

@ -9,12 +9,45 @@ vim.api.nvim_create_autocmd("TextYankPost", {
pattern = "*", pattern = "*",
}) })
function root_pattern(...) function tbl_flatten(t)
local patterns = M.tbl_flatten({ ... }) --- @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) return function(startpath)
startpath = M.strip_archive_subpath(startpath) startpath = strip_archive_subpath(startpath)
for _, pattern in ipairs(patterns) do 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 for _, p in ipairs(vim.fn.glob(table.concat({ escape_wildcards(path), pattern }, "/"), true, true)) do
if vim.loop.fs_stat(p) then if vim.loop.fs_stat(p) then
return path return path
@ -92,4 +125,5 @@ return {
compile_project = compile_project, compile_project = compile_project,
filter = filter, filter = filter,
filterReactDTS = filterReactDTS, filterReactDTS = filterReactDTS,
root_pattern = root_pattern
} }