Skip to content

Conversation

@xvzc
Copy link
Contributor

@xvzc xvzc commented Dec 17, 2025

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.

                                    *nvim-tree-api.tree.change_root_to_node()*
tree.change_root_to_node({node})
    Change the tree's root to a folder node or the parent of a file node.

    Parameters: ~
      • {node} (Node) folder or 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.

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.
@xvzc xvzc marked this pull request as ready for review December 17, 2025 07:19
@alex-courtis
Copy link
Member

Testing notes:

  vim.keymap.set("n", "t", function()
    api.tree.change_root_to_node()
  end, opts("CD"))

  vim.keymap.set("n", "T", function()
    local n = api.tree.get_node_under_cursor()
    api.tree.change_root_to_node(n)
  end, opts("CDN"))

Change to dir, OK:

  • t on dir
  • T on dir
  • t on root
  • T on root

Change to parent, OK:

  • t on file
  • T on file

Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

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

Many thanks for the fix!

@alex-courtis alex-courtis changed the title fix: wrong behavior for api.tree.change_root_to_node fix: api.tree.change_root_to_node on a file will change directory to parent Dec 20, 2025
@alex-courtis alex-courtis changed the title fix: api.tree.change_root_to_node on a file will change directory to parent fix: api.tree.change_root_to_node on a file now changes directory to parent as per documentation Dec 20, 2025
@alex-courtis alex-courtis merged commit b8b44b6 into nvim-tree:master Dec 20, 2025
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants