feat: add follow opt for vim.fs.dir

This commit is contained in:
Mike 2024-12-04 23:24:02 +01:00
parent 7c5a461b59
commit ee45648d2c
No known key found for this signature in database
GPG Key ID: B27ADEA45243EBEE
3 changed files with 20 additions and 4 deletions

View File

@ -2972,6 +2972,7 @@ vim.fs.dir({path}, {opts}) *vim.fs.dir()*
• skip: (fun(dir_name: string): boolean)|nil Predicate to • skip: (fun(dir_name: string): boolean)|nil Predicate to
control traversal. Return false to stop searching the control traversal. Return false to stop searching the
current directory. Only useful when depth > 1 current directory. Only useful when depth > 1
• follow: boolean|nil Follow symbolic links. (default: true)
Return: ~ Return: ~
(`Iterator`) over items in {path}. Each iteration yields two values: (`Iterator`) over items in {path}. Each iteration yields two values:

View File

@ -127,6 +127,7 @@ end
--- - skip: (fun(dir_name: string): boolean)|nil Predicate --- - skip: (fun(dir_name: string): boolean)|nil Predicate
--- to control traversal. Return false to stop searching the current directory. --- to control traversal. Return false to stop searching the current directory.
--- Only useful when depth > 1 --- Only useful when depth > 1
--- - follow: boolean|nil Follow symbolic links. (default: true)
--- ---
---@return Iterator over items in {path}. Each iteration yields two values: "name" and "type". ---@return Iterator over items in {path}. Each iteration yields two values: "name" and "type".
--- "name" is the basename of the item relative to {path}. --- "name" is the basename of the item relative to {path}.
@ -138,6 +139,7 @@ function M.dir(path, opts)
vim.validate('path', path, 'string') vim.validate('path', path, 'string')
vim.validate('depth', opts.depth, 'number', true) vim.validate('depth', opts.depth, 'number', true)
vim.validate('skip', opts.skip, 'function', true) vim.validate('skip', opts.skip, 'function', true)
vim.validate('follow', opts.follow, 'boolean', true)
path = M.normalize(path) path = M.normalize(path)
if not opts.depth or opts.depth == 1 then if not opts.depth or opts.depth == 1 then
@ -168,7 +170,9 @@ function M.dir(path, opts)
if if
opts.depth opts.depth
and level < opts.depth and level < opts.depth
and t == 'directory' and (t == 'directory' or (t == 'link' and opts.follow ~= false and (vim.uv.fs_stat(
M.joinpath(path, f)
) or {}).type == 'directory'))
and (not opts.skip or opts.skip(f) ~= false) and (not opts.skip or opts.skip(f) ~= false)
then then
dirs[#dirs + 1] = { f, level + 1 } dirs[#dirs + 1] = { f, level + 1 }

View File

@ -152,7 +152,7 @@ describe('vim.fs', function()
) )
end) end)
it('works with opts.depth and opts.skip', function() it('works with opts.depth, opts.skip and opts.follow', function()
io.open('testd/a1', 'w'):close() io.open('testd/a1', 'w'):close()
io.open('testd/b1', 'w'):close() io.open('testd/b1', 'w'):close()
io.open('testd/c1', 'w'):close() io.open('testd/c1', 'w'):close()
@ -166,7 +166,7 @@ describe('vim.fs', function()
io.open('testd/a/b/c/b4', 'w'):close() io.open('testd/a/b/c/b4', 'w'):close()
io.open('testd/a/b/c/c4', 'w'):close() io.open('testd/a/b/c/c4', 'w'):close()
local function run(dir, depth, skip) local function run(dir, depth, skip, follow)
return exec_lua(function() return exec_lua(function()
local r = {} local r = {}
local skip_f local skip_f
@ -177,7 +177,7 @@ describe('vim.fs', function()
end end
end end
end end
for name, type_ in vim.fs.dir(dir, { depth = depth, skip = skip_f }) do for name, type_ in vim.fs.dir(dir, { depth = depth, skip = skip_f, follow = follow }) do
r[name] = type_ r[name] = type_
end end
return r return r
@ -197,6 +197,7 @@ describe('vim.fs', function()
exp['a/b2'] = 'file' exp['a/b2'] = 'file'
exp['a/c2'] = 'file' exp['a/c2'] = 'file'
exp['a/b'] = 'directory' exp['a/b'] = 'directory'
local lexp = vim.deepcopy(exp)
eq(exp, run('testd', 2)) eq(exp, run('testd', 2))
@ -213,6 +214,16 @@ describe('vim.fs', function()
exp['a/b/c/c4'] = 'file' exp['a/b/c/c4'] = 'file'
eq(exp, run('testd', 999)) eq(exp, run('testd', 999))
vim.uv.fs_symlink('a', 'testd/l', { junction = true, dir = true })
lexp['l'] = 'link'
eq(lexp, run('testd', 2, nil, false))
lexp['l/a2'] = 'file'
lexp['l/b2'] = 'file'
lexp['l/c2'] = 'file'
lexp['l/b'] = 'directory'
eq(lexp, run('testd', 2, nil, true))
end) end)
end) end)