diff options
author | Gregory Anders <greg@gpanders.com> | 2022-05-17 08:49:33 -0600 |
---|---|---|
committer | Gregory Anders <greg@gpanders.com> | 2022-05-31 13:30:10 -0600 |
commit | 046b4ed461cb78b8b302a6403cc7ea64ad6b6085 (patch) | |
tree | 9409d824661880a0abdc289b5933c930d4eafae4 /runtime/lua/vim/fs.lua | |
parent | f271d706611049bc53a6a439b310fe60bf0fab13 (diff) | |
download | rneovim-046b4ed461cb78b8b302a6403cc7ea64ad6b6085.tar.gz rneovim-046b4ed461cb78b8b302a6403cc7ea64ad6b6085.tar.bz2 rneovim-046b4ed461cb78b8b302a6403cc7ea64ad6b6085.zip |
feat(fs): add vim.fs.normalize()
Diffstat (limited to 'runtime/lua/vim/fs.lua')
-rw-r--r-- | runtime/lua/vim/fs.lua | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 4519f2a1e4..9bf38f7bc3 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -51,14 +51,14 @@ 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 +--- over. The path is first normalized |vim.fs.normalize()|. ---@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, vim.loop.fs_scandir(M.normalize(path)) end --- Find files or directories in the given path. @@ -178,4 +178,28 @@ function M.find(names, opts) return matches end +--- Normalize a path to a standard format. A tilde (~) character at the +--- beginning of the path is expanded to the user's home directory and any +--- backslash (\\) characters are converted to forward slashes (/). Environment +--- variables are also expanded. +--- +--- Example: +--- <pre> +--- vim.fs.normalize('C:\\Users\\jdoe') +--- => 'C:/Users/jdoe' +--- +--- vim.fs.normalize('~/src/neovim') +--- => '/home/jdoe/src/neovim' +--- +--- vim.fs.normalize('$XDG_CONFIG_HOME/nvim/init.vim') +--- => '/Users/jdoe/.config/nvim/init.vim' +--- </pre> +--- +---@param path (string) Path to normalize +---@return (string) Normalized path +function M.normalize(path) + vim.validate({ path = { path, 's' } }) + return (path:gsub('^~/', vim.env.HOME .. '/'):gsub('%$([%w_]+)', vim.env):gsub('\\', '/')) +end + return M |