neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

commit b740709431f5e68dac5238d455f9f86d5a564f36
parent c5526a27c3b61acb33b7c3c3fe518d8f1e0b602f
Author: Gregory Anders <greg@gpanders.com>
Date:   Sun, 15 May 2022 19:55:18 -0600

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

Diffstat:
Mruntime/doc/lua.txt | 9+++++++++
Mruntime/lua/vim/fs.lua | 8++++++++
Mtest/functional/lua/fs_spec.lua | 13+++++++++++++
3 files changed, 30 insertions(+), 0 deletions(-)

diff --git 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* +basename({file}) *vim.fs.basename()* + Return the basename of the given file or directory + + Parameters: ~ + {file} (string) File or directory + + Return: ~ + (string) Basename of {file} + dirname({file}) *vim.fs.dirname()* Return the parent directory of the given file or directory diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua @@ -40,4 +40,12 @@ function M.dirname(file) return vim.fn.fnamemodify(file, ':h') end +--- Return the basename of the given file or directory +--- +---@param file (string) File or directory +---@return (string) Basename of {file} +function M.basename(file) + return vim.fn.fnamemodify(file, ':t') +end + return M diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua @@ -7,6 +7,10 @@ local mkdir_p = helpers.mkdir_p local rmdir = helpers.rmdir local nvim_dir = helpers.nvim_dir local test_build_dir = helpers.test_build_dir +local iswin = helpers.iswin +local nvim_prog = helpers.nvim_prog + +local nvim_prog_basename = iswin() and 'nvim.exe' or 'nvim' before_each(clear) @@ -39,4 +43,13 @@ describe('vim.fs', function() ]], nvim_dir)) end) end) + + describe('basename()', function() + it('works', function() + eq(nvim_prog_basename, exec_lua([[ + local nvim_prog = ... + return vim.fs.basename(nvim_prog) + ]], nvim_prog)) + end) + end) end)