-
-
Notifications
You must be signed in to change notification settings - Fork 611
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = {} | ||
|
||
|
@@ -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 | ||
for _, node in ipairs(newTreeExplorer.nodes) do | ||
if node.absolute_path == TreeExplorer.absolute_path then | ||
node.nodes = TreeExplorer.nodes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These nodes are being destroyed when Test case: #!/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 From another terminal or cmd mode
This is because the watcher has been destroyed. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 lines repeated; can be moved below |
||
end | ||
end | ||
|
||
---@return Explorer|nil | ||
function M.get_explorer() | ||
return TreeExplorer | ||
|
There was a problem hiding this comment.
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