mirror of
https://github.com/neovim/neovim
synced 2025-07-16 01:01:49 +00:00
fix(vim.fs): joinpath() does not normalize slashes on Windows #31782
This commit is contained in:
@ -3062,8 +3062,11 @@ vim.fs.find({names}, {opts}) *vim.fs.find()*
|
|||||||
items
|
items
|
||||||
|
|
||||||
vim.fs.joinpath({...}) *vim.fs.joinpath()*
|
vim.fs.joinpath({...}) *vim.fs.joinpath()*
|
||||||
Concatenate directories and/or file paths into a single path with
|
Concatenates partial paths into one path. Slashes are normalized
|
||||||
normalization (e.g., `"foo/"` and `"bar"` get joined to `"foo/bar"`)
|
(redundant slashes are removed, and on Windows backslashes are replaced
|
||||||
|
with forward-slashes) (e.g., `"foo/"` and `"/bar"` get joined to
|
||||||
|
`"foo/bar"`) (windows: e.g `"a\foo\"` and `"\bar"` are joined to
|
||||||
|
`"a/foo/bar"`)
|
||||||
|
|
||||||
Attributes: ~
|
Attributes: ~
|
||||||
Since: 0.10.0
|
Since: 0.10.0
|
||||||
|
@ -105,14 +105,19 @@ function M.basename(file)
|
|||||||
return file:match('/$') and '' or (file:match('[^/]*$'))
|
return file:match('/$') and '' or (file:match('[^/]*$'))
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Concatenate directories and/or file paths into a single path with normalization
|
--- Concatenates partial paths into one path. Slashes are normalized (redundant slashes are removed, and on Windows backslashes are replaced with forward-slashes)
|
||||||
--- (e.g., `"foo/"` and `"bar"` get joined to `"foo/bar"`)
|
--- (e.g., `"foo/"` and `"/bar"` get joined to `"foo/bar"`)
|
||||||
|
--- (windows: e.g `"a\foo\"` and `"\bar"` are joined to `"a/foo/bar"`)
|
||||||
---
|
---
|
||||||
---@since 12
|
---@since 12
|
||||||
---@param ... string
|
---@param ... string
|
||||||
---@return string
|
---@return string
|
||||||
function M.joinpath(...)
|
function M.joinpath(...)
|
||||||
return (table.concat({ ... }, '/'):gsub('//+', '/'))
|
local path = table.concat({ ... }, '/')
|
||||||
|
if iswin then
|
||||||
|
path = path:gsub('\\', '/')
|
||||||
|
end
|
||||||
|
return (path:gsub('//+', '/'))
|
||||||
end
|
end
|
||||||
|
|
||||||
---@alias Iterator fun(): string?, string?
|
---@alias Iterator fun(): string?, string?
|
||||||
|
@ -323,6 +323,20 @@ describe('vim.fs', function()
|
|||||||
eq('foo/bar/baz', vim.fs.joinpath('foo', 'bar', 'baz'))
|
eq('foo/bar/baz', vim.fs.joinpath('foo', 'bar', 'baz'))
|
||||||
eq('foo/bar/baz', vim.fs.joinpath('foo', '/bar/', '/baz'))
|
eq('foo/bar/baz', vim.fs.joinpath('foo', '/bar/', '/baz'))
|
||||||
end)
|
end)
|
||||||
|
it('rewrites backslashes on Windows', function()
|
||||||
|
if is_os('win') then
|
||||||
|
eq('foo/bar/baz/zub/', vim.fs.joinpath([[foo]], [[\\bar\\\\baz]], [[zub\]]))
|
||||||
|
else
|
||||||
|
eq([[foo/\\bar\\\\baz/zub\]], vim.fs.joinpath([[foo]], [[\\bar\\\\baz]], [[zub\]]))
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
it('strips redundant slashes', function()
|
||||||
|
if is_os('win') then
|
||||||
|
eq('foo/bar/baz/zub/', vim.fs.joinpath([[foo//]], [[\\bar\\\\baz]], [[zub\]]))
|
||||||
|
else
|
||||||
|
eq('foo/bar/baz/zub/', vim.fs.joinpath([[foo]], [[//bar////baz]], [[zub/]]))
|
||||||
|
end
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('normalize()', function()
|
describe('normalize()', function()
|
||||||
|
Reference in New Issue
Block a user