mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
fix: support UNC paths in vim.fs.normalize
Closes https://github.com/neovim/neovim/issues/27068.
This commit is contained in:
parent
38e38d1b40
commit
2424c3e696
@ -371,7 +371,8 @@ function M.normalize(path, opts)
|
|||||||
expand_env = { opts.expand_env, { 'boolean' }, true },
|
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 '~'
|
local home = vim.uv.os_homedir() or '~'
|
||||||
if home:sub(-1) == os_sep then
|
if home:sub(-1) == os_sep then
|
||||||
home = home:sub(1, -2)
|
home = home:sub(1, -2)
|
||||||
@ -379,15 +380,32 @@ function M.normalize(path, opts)
|
|||||||
path = home .. path:sub(2)
|
path = home .. path:sub(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Expand environment variables if `opts.expand_env` isn't `false`
|
||||||
if opts.expand_env == nil or opts.expand_env then
|
if opts.expand_env == nil or opts.expand_env then
|
||||||
path = path:gsub('%$([%w_]+)', vim.uv.os_getenv)
|
path = path:gsub('%$([%w_]+)', vim.uv.os_getenv)
|
||||||
end
|
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
|
if iswin and path:match('^%w:/$') then
|
||||||
return path
|
return path
|
||||||
end
|
end
|
||||||
return (path:gsub('(.)/$', '%1'))
|
|
||||||
|
-- Remove trailing slashes
|
||||||
|
path = path:gsub('(.)/$', '%1')
|
||||||
|
|
||||||
|
return path
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
@ -308,6 +308,15 @@ describe('vim.fs', function()
|
|||||||
)
|
)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('works with UNC paths', function()
|
||||||
|
eq('//foo', vim.fs.normalize('//foo')) -- UNC path
|
||||||
|
eq('//foo/bar', vim.fs.normalize('//foo//bar////')) -- UNC path
|
||||||
|
eq('/foo', vim.fs.normalize('///foo')) -- Not a UNC path
|
||||||
|
eq('/', vim.fs.normalize('//')) -- Not a UNC path
|
||||||
|
eq('/', vim.fs.normalize('///')) -- Not a UNC path
|
||||||
|
eq('/foo/bar', vim.fs.normalize('/foo//bar////')) -- Not a UNC path
|
||||||
|
end)
|
||||||
|
|
||||||
if is_os('win') then
|
if is_os('win') then
|
||||||
it('Last slash is not truncated from root drive', function()
|
it('Last slash is not truncated from root drive', function()
|
||||||
eq('C:/', vim.fs.normalize('C:/'))
|
eq('C:/', vim.fs.normalize('C:/'))
|
||||||
|
Loading…
Reference in New Issue
Block a user