Best way to get the current root node? #2549
-
In my work flow I would like to be able to use Telescope from the root of the current tree (I keep different trees & don't sync with cwd). This requires me be able to determine the path of that node. If I needed to Telescope into a child of the root this isn't that difficult using When it comes to using the root, the solution I came up with seems hacky and I am curious if there is a better way to find local api = require("nvim-tree.api")
--- @return string|nil
local function root_node_dir_path()
local node = api.tree.get_node_under_cursor()
if not node then
return
end
while ( node.parent ) do
node = node.parent
end
return node.absolute_path
end
I can use this approach whenever my current tree line is not at the root But if it is, i.e. at '~/dotfiles/..` then I can't use this method because it appears that the root isn't a node. ~/dotfiles/..
> [] .git
> [] _config
> [] desktop
> [] scripts
.gitignore
link.sh
read.me Is there a better way to find this that doesn't rely on the cursor position? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I use something surprisingly similar for telescope: https://github.com/alex-courtis/arch/blob/9e7e3e8d7aeabe9e49bc7c52eb128bdf21f3d9e5/config/nvim/lua/amc/plugins/nvt.lua#L43 I do, however, sync global cwd everywhere. We've recently added some new API. You could do something like this to retrieve the local working directory of the tree.
|
Beta Was this translation helpful? Give feedback.
Try
:help nvim-tree-api.tree.get_nodes()
The root will contain
absolute_path