Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions lua/neo-tree/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1128,21 +1128,34 @@ M.split_path = function(path)
if prefix and vim.startswith(prefix, path) then
return nil, path
end

-- this is more than just a root path
if path:sub(-1) == M.path_separator then
-- trim it off
path = path:sub(1, -2)
end

local rest_of_path = prefix and path:sub(#prefix + 1) or path
local rest_parts = vim.split(rest_of_path, M.path_separator, { plain = true })
local name = table.remove(rest_parts)
local parentPath = (prefix or "") .. table.concat(rest_parts, M.path_separator)
local last_separator_index
local i = prefix and #prefix + 1 or 1
local j
repeat
j = path:find(M.path_separator, i, true)
if j then
last_separator_index = j
i = j + 1
end
until not j

if #parentPath == 0 then
return prefix, name
if not last_separator_index then
if not prefix then
return nil, path
end
return prefix, path:sub(#prefix + 1)
end

return parentPath, name
local parent_path = path:sub(1, last_separator_index - 1)
local tail = path:sub(last_separator_index + 1)
return parent_path, tail
end

---Joins arbitrary number of paths together.
Expand Down
1 change: 1 addition & 0 deletions tests/neo-tree/utils/path_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ describe("utils path functions", function()

-- Absolute paths
assert.are.same({ "/a", "b" }, { utils.split_path("/a/b") })
assert.are.same({ "/a/b/c/d", "e" }, { utils.split_path("/a/b/c/d/e") })
assert.are.same({ "/", "a" }, { utils.split_path("/a") })

-- Edge cases
Expand Down
Loading