Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change dir with explorer nodes unchanged #2850

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ Following is the default configuration. See |nvim-tree-opts| for details.
enable = true,
global = false,
restrict_above_cwd = false,
explorer_node_unchanged = false,
},
expand_all = {
max_folder_discovery = 300,
Expand Down Expand Up @@ -1399,6 +1400,9 @@ vim |current-directory| behaviour.
Restrict changing to a directory above the global cwd.
Type: `boolean`, Default: `false`

*nvim-tree.actions.change_dir.explorer_node_unchanged*
Change dir with explorer nodes unchanged.

*nvim-tree.actions.expand_all*
Configuration for expand_all behaviour.

Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
enable = true,
global = false,
restrict_above_cwd = false,
explorer_node_unchanged = false,
},
expand_all = {
max_folder_discovery = 300,
Expand Down
6 changes: 5 additions & 1 deletion lua/nvim-tree/actions/root/change-dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ M.force_dirchange = add_profiling_to(function(foldername, should_open_view)
if should_change_dir() then
cd(M.options.global, foldername)
end
core.init(foldername)
if M.options.explorer_node_unchanged then
core.change_root(foldername)
else
core.init(foldername)
end
end

if should_open_view then
Expand Down
45 changes: 45 additions & 0 deletions lua/nvim-tree/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local explorer = require "nvim-tree.explorer"
local live_filter = require "nvim-tree.live-filter"
local view = require "nvim-tree.view"
local log = require "nvim-tree.log"
local Iterator = require "nvim-tree.iterators.node-iterator"
local utils = require "nvim-tree.utils"

local M = {}

Expand All @@ -25,6 +27,49 @@ function M.init(foldername)
log.profile_end(profile)
end

---@param path string
function M.change_root(path)
if TreeExplorer == nil then
return
end
local root_parent_cwd = vim.fn.fnamemodify(utils.path_remove_trailing(TreeExplorer.absolute_path), ":h")
if root_parent_cwd == path then
local newTreeExplorer = explorer.Explorer.new(path)
if newTreeExplorer == nil then
return
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 lines repeated; can be moved above if

for _, node in ipairs(newTreeExplorer.nodes) do
if node.absolute_path == TreeExplorer.absolute_path then
node.nodes = TreeExplorer.nodes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These nodes are being destroyed when TreeExplorer:destroy() is executed.

Test case:
create.sh

#!/bin/sh

mkdir -p a/1/A
mkdir -p a/2/A
mkdir -p a/2/B
mkdir -p b/1/A
mkdir -p b/2/B

touch foo
touch a/1/foo
touch a/1/A/bar
touch a/2/A/baz
touch a/2/B/foo
touch b/1/A/bar
touch b/2/B/baz

Open from directory a and expand all E.

From another terminal or cmd mode touch a/1/A/zero and see it appear.

- to change directory up.

rm a/1/A/zero does not update the tree until manual refresh R.

This is because the watcher has been destroyed.

20240728_152354

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I overlooked that fact. Let me make some changes. Thank you for providing the test case.

end
end
TreeExplorer:destroy()
TreeExplorer = newTreeExplorer
else
local newTreeExplorer = explorer.Explorer.new(path)
if newTreeExplorer == nil then
return
end
local child_node
Iterator.builder(TreeExplorer.nodes)
:hidden()
:applier(function(n)
if n.absolute_path == path then
child_node = n
end
end)
:recursor(function(n)
return n.group_next and { n.group_next } or n.nodes
end)
:iterate()
if #child_node.nodes ~= 0 then
newTreeExplorer.nodes = child_node.nodes;
end
TreeExplorer:destroy()
TreeExplorer = newTreeExplorer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 lines repeated; can be moved below end

end
end

---@return Explorer|nil
function M.get_explorer()
return TreeExplorer
Expand Down
Loading