From 92a64daf273da678a7834558f103ad89e22cf595 Mon Sep 17 00:00:00 2001 From: kiyan Date: Tue, 8 Feb 2022 21:36:46 +0100 Subject: [PATCH] fix: normalize cwd with '..' --- lua/nvim-tree/explorer/init.lua | 3 ++- lua/nvim-tree/utils.lua | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua index 2a90030b..efad47fe 100644 --- a/lua/nvim-tree/explorer/init.lua +++ b/lua/nvim-tree/explorer/init.lua @@ -2,6 +2,7 @@ local uv = vim.loop local git = require"nvim-tree.git" local renderer = require"nvim-tree.renderer" +local utils = require"nvim-tree.utils" local M = {} @@ -12,7 +13,7 @@ local Explorer = {} Explorer.__index = Explorer function Explorer.new(cwd) - cwd = cwd or uv.cwd() + cwd = utils.path_normalize(cwd or uv.cwd()) return setmetatable({ cwd = cwd, nodes = {} diff --git a/lua/nvim-tree/utils.lua b/lua/nvim-tree/utils.lua index 4e54b56a..78bc0a40 100644 --- a/lua/nvim-tree/utils.lua +++ b/lua/nvim-tree/utils.lua @@ -208,4 +208,24 @@ function M.file_exists(path) return error == nil end +--- @param num number elements to take +--- @param list table elements +--- @return table +function M.take(num, list) + local t = {} + for i, c in ipairs(list) do + if i > num then break end + table.insert(t, c) + end + return t +end + +--- @param path string +--- @return string +function M.path_normalize(path) + local components = vim.split(vim.fn.expand(path), path_separator) + local num_dots = #vim.tbl_filter(function(v) return v == ".." end, components) + return M.path_join(M.take(#components - num_dots * 2, components)) +end + return M