Skip to content

Commit 91ce350

Browse files
committed
fix: change dir with nodes unchanged
1 parent 4e396b2 commit 91ce350

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

lua/nvim-tree/actions/root/change-dir.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ M.force_dirchange = add_profiling_to(function(foldername, should_open_view)
8585
if should_change_dir() then
8686
cd(M.options.global, foldername)
8787
end
88-
core.init(foldername)
88+
core.change_root(foldername)
8989
end
9090

9191
if should_open_view then

lua/nvim-tree/core.lua

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ local explorer = require "nvim-tree.explorer"
33
local live_filter = require "nvim-tree.live-filter"
44
local view = require "nvim-tree.view"
55
local log = require "nvim-tree.log"
6+
local Iterator = require "nvim-tree.iterators.node-iterator"
7+
local utils = require "nvim-tree.utils"
68

79
local M = {}
810

@@ -25,6 +27,49 @@ function M.init(foldername)
2527
log.profile_end(profile)
2628
end
2729

30+
---@param path string
31+
function M.change_root(path)
32+
if TreeExplorer == nil then
33+
return
34+
end
35+
local root_parent_cwd = vim.fn.fnamemodify(utils.path_remove_trailing(TreeExplorer.absolute_path), ":h")
36+
if root_parent_cwd == path then
37+
local newTreeExplorer = explorer.Explorer.new(path)
38+
if newTreeExplorer == nil then
39+
return
40+
end
41+
for _, node in ipairs(newTreeExplorer.nodes) do
42+
if node.absolute_path == TreeExplorer.absolute_path then
43+
node.nodes = TreeExplorer.nodes
44+
end
45+
end
46+
TreeExplorer:destroy()
47+
TreeExplorer = newTreeExplorer
48+
else
49+
local newTreeExplorer = explorer.Explorer.new(path)
50+
if newTreeExplorer == nil then
51+
return
52+
end
53+
local child_node
54+
Iterator.builder(TreeExplorer.nodes)
55+
:hidden()
56+
:applier(function(n)
57+
if n.absolute_path == path then
58+
child_node = n
59+
end
60+
end)
61+
:recursor(function(n)
62+
return n.group_next and { n.group_next } or n.nodes
63+
end)
64+
:iterate()
65+
if #child_node.nodes ~= 0 then
66+
newTreeExplorer.nodes = child_node.nodes;
67+
end
68+
TreeExplorer:destroy()
69+
TreeExplorer = newTreeExplorer
70+
end
71+
end
72+
2873
---@return Explorer|nil
2974
function M.get_explorer()
3075
return TreeExplorer

0 commit comments

Comments
 (0)