diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index 283afcc9f3..df4e078acc 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -2592,10 +2592,11 @@ vim.fs.joinpath({...}) *vim.fs.joinpath()* Concatenates partial paths (one absolute or relative path followed by zero or more relative paths). Slashes are normalized: redundant slashes are removed, and (on Windows) backslashes are replaced with forward-slashes. - Paths are not expanded/resolved. + Empty segments are removed. Paths are not expanded/resolved. Examples: • "foo/", "/bar" => "foo/bar" + • "", "after/plugin" => "after/plugin" • Windows: "a\foo\", "\bar" => "a/foo/bar" Attributes: ~ diff --git a/runtime/lua/vim/fs.lua b/runtime/lua/vim/fs.lua index 6e590178d2..dc26790d14 100644 --- a/runtime/lua/vim/fs.lua +++ b/runtime/lua/vim/fs.lua @@ -116,21 +116,30 @@ end --- Concatenates partial paths (one absolute or relative path followed by zero or more relative --- paths). Slashes are normalized: redundant slashes are removed, and (on Windows) backslashes are ---- replaced with forward-slashes. Paths are not expanded/resolved. +--- replaced with forward-slashes. Empty segments are removed. Paths are not expanded/resolved. --- --- Examples: --- - "foo/", "/bar" => "foo/bar" +--- - "", "after/plugin" => "after/plugin" --- - Windows: "a\foo\", "\bar" => "a/foo/bar" --- ---@since 12 ---@param ... string ---@return string function M.joinpath(...) - local path = table.concat({ ... }, '/') - if iswin then - path = path:gsub('\\', '/') + local n = select('#', ...) + ---@type string[] + local segments = {} + for i = 1, n do + local s = select(i, ...) + if s and #s > 0 then + segments[#segments + 1] = s + end end - return (path:gsub('//+', '/')) + + local path = table.concat(segments, '/') + + return (path:gsub(iswin and '[/\\][/\\]*' or '//+', '/')) end --- @class vim.fs.dir.Opts diff --git a/test/functional/lua/fs_spec.lua b/test/functional/lua/fs_spec.lua index 017d158584..8ffdef3566 100644 --- a/test/functional/lua/fs_spec.lua +++ b/test/functional/lua/fs_spec.lua @@ -470,6 +470,12 @@ describe('vim.fs', function() eq('foo/bar/baz/zub/', vim.fs.joinpath([[foo]], [[//bar////baz]], [[zub/]])) end end) + it('handles empty segments', function() + eq('foo/bar', vim.fs.joinpath('', 'foo', '', 'bar', '')) + eq('foo/bar', vim.fs.joinpath('', '', 'foo', 'bar', '', '')) + eq('', vim.fs.joinpath('')) + eq('', vim.fs.joinpath('', '', '', '')) + end) end) describe('normalize()', function()