aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/fs.lua
diff options
context:
space:
mode:
authordundargoc <gocdundar@gmail.com>2024-03-29 18:05:02 +0100
committerdundargoc <33953936+dundargoc@users.noreply.github.com>2024-03-30 00:51:09 +0100
commit2424c3e6967ef953222cf48564629ffe5812d415 (patch)
tree31ece3f3d5518627b12bf7ac13550f245fcc3406 /runtime/lua/vim/fs.lua
parent38e38d1b401e38459842f0e4da431e3dd6c2e527 (diff)
downloadrneovim-2424c3e6967ef953222cf48564629ffe5812d415.tar.gz
rneovim-2424c3e6967ef953222cf48564629ffe5812d415.tar.bz2
rneovim-2424c3e6967ef953222cf48564629ffe5812d415.zip
fix: support UNC paths in vim.fs.normalize
Closes https://github.com/neovim/neovim/issues/27068.
Diffstat (limited to 'runtime/lua/vim/fs.lua')
-rw-r--r--runtime/lua/vim/fs.lua24
1 files changed, 21 insertions, 3 deletions
diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua
index b7718ac87a..ad0d914ea2 100644
--- a/runtime/lua/vim/fs.lua
+++ b/runtime/lua/vim/fs.lua
@@ -371,7 +371,8 @@ function M.normalize(path, opts)
expand_env = { opts.expand_env, { 'boolean' }, true },
})
- if path:sub(1, 1) == '~' then
+ -- Expand ~ to users home directory
+ if vim.startswith(path, '~') then
local home = vim.uv.os_homedir() or '~'
if home:sub(-1) == os_sep then
home = home:sub(1, -2)
@@ -379,15 +380,32 @@ function M.normalize(path, opts)
path = home .. path:sub(2)
end
+ -- Expand environment variables if `opts.expand_env` isn't `false`
if opts.expand_env == nil or opts.expand_env then
path = path:gsub('%$([%w_]+)', vim.uv.os_getenv)
end
- path = path:gsub(os_sep, '/'):gsub('/+', '/')
+ -- Convert path separator to `/`
+ path = path:gsub(os_sep, '/')
+
+ -- Don't modify leading double slash as those have implementation-defined behavior according to
+ -- POSIX. They are also valid UNC paths. Three or more leading slashes are however collapsed to
+ -- a single slash.
+ if vim.startswith(path, '//') and not vim.startswith(path, '///') then
+ path = '/' .. path:gsub('/+', '/')
+ else
+ path = path:gsub('/+', '/')
+ end
+
+ -- Ensure last slash is not truncated from root drive on Windows
if iswin and path:match('^%w:/$') then
return path
end
- return (path:gsub('(.)/$', '%1'))
+
+ -- Remove trailing slashes
+ path = path:gsub('(.)/$', '%1')
+
+ return path
end
return M