Skip to content

Commit 92a64da

Browse files
committed
fix: normalize cwd with '..'
1 parent d8bf1ad commit 92a64da

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lua/nvim-tree/explorer/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local uv = vim.loop
22

33
local git = require"nvim-tree.git"
44
local renderer = require"nvim-tree.renderer"
5+
local utils = require"nvim-tree.utils"
56

67
local M = {}
78

@@ -12,7 +13,7 @@ local Explorer = {}
1213
Explorer.__index = Explorer
1314

1415
function Explorer.new(cwd)
15-
cwd = cwd or uv.cwd()
16+
cwd = utils.path_normalize(cwd or uv.cwd())
1617
return setmetatable({
1718
cwd = cwd,
1819
nodes = {}

lua/nvim-tree/utils.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,24 @@ function M.file_exists(path)
208208
return error == nil
209209
end
210210

211+
--- @param num number elements to take
212+
--- @param list table elements
213+
--- @return table
214+
function M.take(num, list)
215+
local t = {}
216+
for i, c in ipairs(list) do
217+
if i > num then break end
218+
table.insert(t, c)
219+
end
220+
return t
221+
end
222+
223+
--- @param path string
224+
--- @return string
225+
function M.path_normalize(path)
226+
local components = vim.split(vim.fn.expand(path), path_separator)
227+
local num_dots = #vim.tbl_filter(function(v) return v == ".." end, components)
228+
return M.path_join(M.take(#components - num_dots * 2, components))
229+
end
230+
211231
return M

0 commit comments

Comments
 (0)