diff --git a/flake.lock b/flake.lock index 083b6c4..bdaad6a 100644 --- a/flake.lock +++ b/flake.lock @@ -7,16 +7,15 @@ ] }, "locked": { - "lastModified": 1744117652, - "narHash": "sha256-t7dFCDl4vIOOUMhEZnJF15aAzkpaup9x4ZRGToDFYWI=", + "lastModified": 1744208565, + "narHash": "sha256-vG3JJOar/r8ognz7wuwMtOJ8Knu1MMlOzHB1N6R2MbY=", "owner": "nix-community", "repo": "home-manager", - "rev": "b4e98224ad1336751a2ac7493967a4c9f6d9cb3f", + "rev": "542efdf2dfac351498f534eb71671525b9bd45ed", "type": "github" }, "original": { "owner": "nix-community", - "ref": "release-24.11", "repo": "home-manager", "type": "github" } diff --git a/home/modules/nvim/config/lua/dnsc/utils.lua b/home/modules/nvim/config/lua/dnsc/utils.lua index 4a97445..08de00c 100644 --- a/home/modules/nvim/config/lua/dnsc/utils.lua +++ b/home/modules/nvim/config/lua/dnsc/utils.lua @@ -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 }