diff options
author | Gregory Anders <greg@gpanders.com> | 2022-05-15 19:53:23 -0600 |
---|---|---|
committer | Gregory Anders <greg@gpanders.com> | 2022-05-31 13:04:41 -0600 |
commit | c5526a27c3b61acb33b7c3c3fe518d8f1e0b602f (patch) | |
tree | 574fc05f7d38347d63ca7610343637c98e065f59 | |
parent | 67cbaf58c41a3db19c5014587e72d06be9e3d58e (diff) | |
download | rneovim-c5526a27c3b61acb33b7c3c3fe518d8f1e0b602f.tar.gz rneovim-c5526a27c3b61acb33b7c3c3fe518d8f1e0b602f.tar.bz2 rneovim-c5526a27c3b61acb33b7c3c3fe518d8f1e0b602f.zip |
feat(fs): add vim.fs.dirname()
-rw-r--r-- | runtime/doc/lua.txt | 9 | ||||
-rw-r--r-- | runtime/lua/vim/fs.lua | 10 | ||||
-rw-r--r-- | test/functional/lua/fs_spec.lua | 9 |
3 files changed, 27 insertions, 1 deletions
diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index 8f74dca418..cf6d2e7276 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -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. diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 08d2e495d2..a3b7321cc9 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -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 diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua index 69eb8cd539..23ffb1e607 100644 --- a/test/functional/lua/fs_spec.lua +++ b/test/functional/lua/fs_spec.lua @@ -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) |