make iterator stateless

This commit is contained in:
Ivar Fatland
2026-06-15 09:54:16 +02:00
parent b9ac029bd2
commit 1d5169b0d9
+18 -11
View File
@@ -364,26 +364,33 @@ do
table.insert(folders, dir) table.insert(folders, dir)
end end
---@param _ any
---@param i integer
---@return integer?
---@return string?
local function iter(_, i)
i = i + 1
local f = folders[i]
if f == nil then
return nil
end
if file_exists(f, '.git') then
i = -1
end
return i, f
end
---return iterator starting from cwd and going upwards until the project ---return iterator starting from cwd and going upwards until the project
---root is found by looking for the .git folder. ---root is found by looking for the .git folder.
---@return fun(): string?
all_folders_till_root = function() all_folders_till_root = function()
local curr = 0 return iter, nil, 0
return function()
curr = curr + 1
local f = folders[curr]
if file_exists(f, '.git') then
curr = -1
end
return f
end
end end
end end
---@param glob_pattern string ---@param glob_pattern string
---@return boolean ---@return boolean
local function look_for_file(glob_pattern) local function look_for_file(glob_pattern)
for dir in all_folders_till_root() do for _, dir in all_folders_till_root() do
if file_exists(dir, glob_pattern) then if file_exists(dir, glob_pattern) then
return true return true
end end