diff options
author | Lewis Russell <lewis6991@gmail.com> | 2023-03-30 13:57:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-30 13:57:59 +0100 |
commit | 8fa7d833cf3a6c906c91c5acf9187b4544cf94be (patch) | |
tree | 857a980da314eff640ce2ac31db067ffde4d6aa4 /runtime/lua/vim/fs.lua | |
parent | e5fa2e3a5d0f120c0b957b4643ea63a807ba377b (diff) | |
parent | fd70a9dca199aae1eb7714608d63e8b2e63fb293 (diff) | |
download | rneovim-8fa7d833cf3a6c906c91c5acf9187b4544cf94be.tar.gz rneovim-8fa7d833cf3a6c906c91c5acf9187b4544cf94be.tar.bz2 rneovim-8fa7d833cf3a6c906c91c5acf9187b4544cf94be.zip |
Merge pull request #22791 from lewis6991/refactor/loadermisc
refactor(loader): changes
Diffstat (limited to 'runtime/lua/vim/fs.lua')
-rw-r--r-- | runtime/lua/vim/fs.lua | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 2c3fc64d57..407b334f20 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -77,6 +77,8 @@ local function join_paths(...) return (table.concat({ ... }, '/'):gsub('//+', '/')) end +---@alias Iterator fun(): string?, string? + --- Return an iterator over the files and directories located in {path} --- ---@param path (string) An absolute or relative path to the directory to iterate @@ -100,10 +102,13 @@ function M.dir(path, opts) }) if not opts.depth or opts.depth == 1 then - return function(fs) + local fs = vim.loop.fs_scandir(M.normalize(path)) + return function() + if not fs then + return + end return vim.loop.fs_scandir_next(fs) - end, - vim.loop.fs_scandir(M.normalize(path)) + end end --- @async @@ -316,16 +321,32 @@ end --- </pre> --- ---@param path (string) Path to normalize +---@param opts table|nil Options: +--- - expand_env: boolean Expand environment variables (default: true) ---@return (string) Normalized path -function M.normalize(path) - vim.validate({ path = { path, 's' } }) - return ( - path - :gsub('^~$', vim.loop.os_homedir()) - :gsub('^~/', vim.loop.os_homedir() .. '/') - :gsub('%$([%w_]+)', vim.loop.os_getenv) - :gsub('\\', '/') - ) +function M.normalize(path, opts) + opts = opts or {} + + vim.validate({ + path = { path, { 'string' } }, + expand_env = { opts.expand_env, { 'boolean' }, true }, + }) + + if path:sub(1, 1) == '~' then + local home = vim.loop.os_homedir() or '~' + if home:sub(-1) == '\\' or home:sub(-1) == '/' then + home = home:sub(1, -2) + end + path = home .. path:sub(2) + end + + if opts.expand_env == nil or opts.expand_env then + path = path:gsub('%$([%w_]+)', vim.loop.os_getenv) + end + + path = path:gsub('\\', '/'):gsub('/+', '/') + + return path:sub(-1) == '/' and path:sub(1, -2) or path end return M |