mirror of
https://github.com/neovim/neovim.git
synced 2025-12-17 03:45:42 +00:00
feat(fs): add vim.fs.dirname()
This commit is contained in:
@@ -2151,6 +2151,15 @@ set({mode}, {lhs}, {rhs}, {opts}) *vim.keymap.set()*
|
|||||||
==============================================================================
|
==============================================================================
|
||||||
Lua module: fs *lua-fs*
|
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()*
|
parents({start}) *vim.fs.parents()*
|
||||||
Iterate over all the parents of the given file or directory.
|
Iterate over all the parents of the given file or directory.
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ local M = {}
|
|||||||
---@return (function) Iterator
|
---@return (function) Iterator
|
||||||
function M.parents(start)
|
function M.parents(start)
|
||||||
return function(_, dir)
|
return function(_, dir)
|
||||||
local parent = vim.fn.fnamemodify(dir, ":h")
|
local parent = M.dirname(dir)
|
||||||
if parent == dir then
|
if parent == dir then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
@@ -32,4 +32,12 @@ function M.parents(start)
|
|||||||
start
|
start
|
||||||
end
|
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
|
return M
|
||||||
|
|||||||
@@ -30,4 +30,13 @@ describe('vim.fs', function()
|
|||||||
rmdir(test_dir)
|
rmdir(test_dir)
|
||||||
end)
|
end)
|
||||||
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)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user