Skip to content

Commit fd4cace

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

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ 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 utils = require "nvim-tree.utils"
67

78
local M = {}
89

@@ -25,6 +26,19 @@ function M.init(foldername)
2526
log.profile_end(profile)
2627
end
2728

29+
---@param path string
30+
function M.change_root(path)
31+
if TreeExplorer == nil then
32+
return
33+
end
34+
local root_parent_cwd = vim.fn.fnamemodify(utils.path_remove_trailing(TreeExplorer.absolute_path), ":h")
35+
if root_parent_cwd == path then
36+
TreeExplorer = explorer.Explorer.new_from_child(path, TreeExplorer)
37+
else
38+
TreeExplorer = explorer.Explorer.new_from_parent(path, TreeExplorer)
39+
end
40+
end
41+
2842
---@return Explorer|nil
2943
function M.get_explorer()
3044
return TreeExplorer

lua/nvim-tree/explorer/init.lua

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local git = require "nvim-tree.git"
22
local notify = require "nvim-tree.notify"
33
local watch = require "nvim-tree.explorer.watch"
44
local explorer_node = require "nvim-tree.explorer.node"
5+
local Iterator = require "nvim-tree.iterators.node-iterator"
56
local Marks = require "nvim-tree.marks"
67

78
local M = {}
@@ -45,6 +46,58 @@ function Explorer.new(path)
4546
return explorer
4647
end
4748

49+
---@param parent_path string
50+
---@param oldexplorer Explorer
51+
---@return Explorer
52+
function Explorer.new_from_child(parent_path, oldexplorer)
53+
---@class Explorer
54+
local explorer = setmetatable({
55+
absolute_path = parent_path,
56+
nodes = {},
57+
open = true,
58+
marks = Marks:new(),
59+
}, Explorer)
60+
explorer.watcher = watch.create_watcher(explorer)
61+
explorer:_load(explorer)
62+
for _, node in ipairs(explorer.nodes) do
63+
if node.absolute_path == oldexplorer.absolute_path then
64+
node.nodes = oldexplorer.nodes
65+
end
66+
end
67+
return explorer
68+
end
69+
70+
---@param child_path string
71+
---@param oldexplorer Explorer
72+
---@return Explorer
73+
function Explorer.new_from_parent(child_path, oldexplorer)
74+
---@class Explorer
75+
local explorer = setmetatable({
76+
absolute_path = child_path,
77+
nodes = {},
78+
open = true,
79+
marks = Marks:new(),
80+
}, Explorer)
81+
explorer.watcher = watch.create_watcher(explorer)
82+
explorer:_load(explorer)
83+
84+
local child_node
85+
Iterator.builder(oldexplorer.nodes)
86+
:hidden()
87+
:applier(function(n)
88+
if n.absolute_path == child_path then
89+
child_node = n
90+
end
91+
end)
92+
:recursor(function(n)
93+
return n.group_next and { n.group_next } or n.nodes
94+
end)
95+
:iterate()
96+
97+
explorer.nodes = child_node.nodes;
98+
return explorer
99+
end
100+
48101
---@private
49102
---@param node Node
50103
function Explorer:_load(node)

0 commit comments

Comments
 (0)