From 55d7aa4fe5b7ca89fb5567c365d23a6f4504a44e Mon Sep 17 00:00:00 2001 From: xvzc Date: Wed, 17 Dec 2025 16:02:04 +0900 Subject: [PATCH] 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. --- lua/nvim-tree/api.lua | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lua/nvim-tree/api.lua b/lua/nvim-tree/api.lua index a1ee507f8af..6ec24523dc3 100644 --- a/lua/nvim-tree/api.lua +++ b/lua/nvim-tree/api.lua @@ -9,6 +9,7 @@ local keymap = require("nvim-tree.keymap") local notify = require("nvim-tree.notify") local DirectoryNode = require("nvim-tree.node.directory") +local FileNode = require("nvim-tree.node.file") local FileLinkNode = require("nvim-tree.node.file-link") local RootNode = require("nvim-tree.node.root") local UserDecorator = require("nvim-tree.renderer.decorator.user") @@ -160,11 +161,17 @@ end) Api.tree.change_root_to_node = wrap_node(function(node) if node.name == ".." or node:is(RootNode) then actions.root.change_dir.fn("..") - else - node = node:as(DirectoryNode) - if node then - actions.root.change_dir.fn(node:last_group_node().absolute_path) - end + return + end + + if node:is(FileNode) and node.parent ~= nil then + actions.root.change_dir.fn(node.parent:last_group_node().absolute_path) + return + end + + if node:is(DirectoryNode) then + actions.root.change_dir.fn(node:last_group_node().absolute_path) + return end end)