Skip to content

Commit 0385835

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

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

doc/nvim-tree-lua.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ Following is the default configuration. See |nvim-tree-opts| for details.
568568
enable = true,
569569
global = false,
570570
restrict_above_cwd = false,
571+
explorer_node_unchanged = false,
571572
},
572573
expand_all = {
573574
max_folder_discovery = 300,
@@ -1399,6 +1400,9 @@ vim |current-directory| behaviour.
13991400
Restrict changing to a directory above the global cwd.
14001401
Type: `boolean`, Default: `false`
14011402

1403+
*nvim-tree.actions.change_dir.explorer_node_unchanged*
1404+
Change dir with explorer nodes unchanged.
1405+
14021406
*nvim-tree.actions.expand_all*
14031407
Configuration for expand_all behaviour.
14041408

lua/nvim-tree.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
532532
enable = true,
533533
global = false,
534534
restrict_above_cwd = false,
535+
explorer_node_unchanged = false,
535536
},
536537
expand_all = {
537538
max_folder_discovery = 300,

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ 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+
if M.options.explorer_node_unchanged then
89+
core.change_root(foldername)
90+
else
91+
core.init(foldername)
92+
end
8993
end
9094

9195
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)