Skip to content

Commit b8b44b6

Browse files
xvzcalex-courtis
andauthored
fix: api.tree.change_root_to_node on a file now changes directory to parent as per documentation (#3228)
fix: wrong behavior for api.tree.change_root_to_node As described in the documentation, `api.tree.change_root_to_node` should set the tree root to the parent directory when the target node is a file. Currently, calling this function on a file node does nothing. This commit adds logic to handle cases where the input node is a `FileNode` in the `change_root_to_node` function. Co-authored-by: Alexander Courtis <alex@courtis.org>
1 parent b63d367 commit b8b44b6

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

lua/nvim-tree/api.lua

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local keymap = require("nvim-tree.keymap")
99
local notify = require("nvim-tree.notify")
1010

1111
local DirectoryNode = require("nvim-tree.node.directory")
12+
local FileNode = require("nvim-tree.node.file")
1213
local FileLinkNode = require("nvim-tree.node.file-link")
1314
local RootNode = require("nvim-tree.node.root")
1415
local UserDecorator = require("nvim-tree.renderer.decorator.user")
@@ -160,11 +161,17 @@ end)
160161
Api.tree.change_root_to_node = wrap_node(function(node)
161162
if node.name == ".." or node:is(RootNode) then
162163
actions.root.change_dir.fn("..")
163-
else
164-
node = node:as(DirectoryNode)
165-
if node then
166-
actions.root.change_dir.fn(node:last_group_node().absolute_path)
167-
end
164+
return
165+
end
166+
167+
if node:is(FileNode) and node.parent ~= nil then
168+
actions.root.change_dir.fn(node.parent:last_group_node().absolute_path)
169+
return
170+
end
171+
172+
if node:is(DirectoryNode) then
173+
actions.root.change_dir.fn(node:last_group_node().absolute_path)
174+
return
168175
end
169176
end)
170177

0 commit comments

Comments
 (0)