feat(fs): add vim.fs.dirname()

This commit is contained in:
Gregory Anders
2022-05-15 19:53:23 -06:00
parent 67cbaf58c4
commit c5526a27c3
3 changed files with 27 additions and 1 deletions

View File

@@ -2151,6 +2151,15 @@ set({mode}, {lhs}, {rhs}, {opts}) *vim.keymap.set()*
==============================================================================
Lua module: fs *lua-fs*
dirname({file}) *vim.fs.dirname()*
Return the parent directory of the given file or directory
Parameters: ~
{file} (string) File or directory
Return: ~
(string) Parent directory of {file}
parents({start}) *vim.fs.parents()*
Iterate over all the parents of the given file or directory.

View File

@@ -21,7 +21,7 @@ local M = {}
---@return (function) Iterator
function M.parents(start)
return function(_, dir)
local parent = vim.fn.fnamemodify(dir, ":h")
local parent = M.dirname(dir)
if parent == dir then
return nil
end
@@ -32,4 +32,12 @@ function M.parents(start)
start
end
--- Return the parent directory of the given file or directory
---
---@param file (string) File or directory
---@return (string) Parent directory of {file}
function M.dirname(file)
return vim.fn.fnamemodify(file, ':h')
end
return M

View File

@@ -30,4 +30,13 @@ describe('vim.fs', function()
rmdir(test_dir)
end)
end)
describe('dirname()', function()
it('works', function()
eq(test_build_dir, exec_lua([[
local nvim_dir = ...
return vim.fs.dirname(nvim_dir)
]], nvim_dir))
end)
end)
end)