mirror of
https://github.com/neovim/neovim.git
synced 2025-11-09 12:05:14 +00:00
feat(fs): add vim.fs.dir()
This function is modeled after the path.dir() function from Penlight and the luafilesystem module.
This commit is contained in:
@@ -2160,6 +2160,20 @@ basename({file}) *vim.fs.basename()*
|
|||||||
Return: ~
|
Return: ~
|
||||||
(string) Basename of {file}
|
(string) Basename of {file}
|
||||||
|
|
||||||
|
dir({path}) *vim.fs.dir()*
|
||||||
|
Return an iterator over the files and directories located in
|
||||||
|
{path}
|
||||||
|
|
||||||
|
Parameters: ~
|
||||||
|
{path} (string) An absolute or relative path to the
|
||||||
|
directory to iterate over
|
||||||
|
|
||||||
|
Return: ~
|
||||||
|
Iterator over files and directories in {path}. Each
|
||||||
|
iteration yields two values: name and type. Each "name" is
|
||||||
|
the basename of the file or directory relative to {path}.
|
||||||
|
Type is one of "file" or "directory".
|
||||||
|
|
||||||
dirname({file}) *vim.fs.dirname()*
|
dirname({file}) *vim.fs.dirname()*
|
||||||
Return the parent directory of the given file or directory
|
Return the parent directory of the given file or directory
|
||||||
|
|
||||||
|
|||||||
@@ -48,4 +48,17 @@ function M.basename(file)
|
|||||||
return vim.fn.fnamemodify(file, ':t')
|
return vim.fn.fnamemodify(file, ':t')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Return an iterator over the files and directories located in {path}
|
||||||
|
---
|
||||||
|
---@param path (string) An absolute or relative path to the directory to iterate
|
||||||
|
--- over
|
||||||
|
---@return Iterator over files and directories in {path}. Each iteration yields
|
||||||
|
--- two values: name and type. Each "name" is the basename of the file or
|
||||||
|
--- directory relative to {path}. Type is one of "file" or "directory".
|
||||||
|
function M.dir(path)
|
||||||
|
return function(fs)
|
||||||
|
return vim.loop.fs_scandir_next(fs)
|
||||||
|
end, vim.loop.fs_scandir(path)
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|||||||
@@ -52,4 +52,18 @@ describe('vim.fs', function()
|
|||||||
]], nvim_prog))
|
]], nvim_prog))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('dir()', function()
|
||||||
|
it('works', function()
|
||||||
|
eq(true, exec_lua([[
|
||||||
|
local dir, nvim = ...
|
||||||
|
for name, type in vim.fs.dir(dir) do
|
||||||
|
if name == nvim and type == 'file' then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
]], nvim_dir, nvim_prog_basename))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user