bundle/nui.nvim/lua/nui/tree/README.md
NuiTree can render tree-like structured content on the buffer.
Examples
local NuiTree = require("nui.tree")
local tree = NuiTree({
bufnr = bufnr,
nodes = {
NuiTree.Node({ text = "a" }),
NuiTree.Node({ text = "b" }, {
NuiTree.Node({ text = "b-1" }),
NuiTree.Node({ text = { "b-2", "b-3" } }),
}),
},
})
tree:render()
bufnrType: number
Id of the buffer where the tree will be rendered.
ns_idType: number or string
Namespace id (number) or name (string).
nodesType: table
List of NuiTree.Node objects.
get_node_idType: function
Signature: get_node_id(node) -> string
If provided, this function is used for generating node's id.
The return value should be a unique string.
Example
get_node_id = function(node)
if node.id then
return "-" .. node.id
end
if node.text then
return string.format("%s-%s-%s", node:get_parent_id() or "", node:get_depth(), node.text)
end
return "-" .. math.random()
end,
prepare_nodeType: function
Signature: prepare_node(node, parent_node?) -> nil | string | string[] | NuiLine | NuiLine[]
If provided, this function is used for preparing each node line.
The return value should be a NuiLine object or string or a list containing either of them.
If return value is nil, that node will not be rendered.
Example
prepare_node = function(node)
local line = NuiLine()
line:append(string.rep(" ", node:get_depth() - 1))
if node:has_children() then
line:append(node:is_expanded() and " " or " ")
else
line:append(" ")
end
line:append(node.text)
return line
end,
buf_optionsType: table
Contains all buffer related options (check :h options | /local to buffer).
Examples
buf_options = {
bufhidden = "hide",
buflisted = false,
buftype = "nofile",
swapfile = false,
},
tree:get_nodeSignature: tree:get_node(node_id_or_linenr?) -> NuiTreeNode | nil, number | nil, number | nil
Parameters
| Name | Type | Description |
|---|---|---|
node_id_or_linenr | number or string or nil | node's id or line number |
If node_id_or_linenr is string, the node with that id is returned.
If node_id_or_linenr is number, the node on that linenr is returned.
If node_id is nil, the current node under cursor is returned.
Returns the node if found, and the start and end linenr if it is rendered.
tree:get_nodesSignature: tree:get_node(parent_id?) -> NuiTreeNode[]
Parameters
| Name | Type | Description |
|---|---|---|
parent_id | string or nil | parent node's id |
If parent_id is present, child nodes under that parent are returned,
Otherwise root nodes are returned.
tree:add_nodeSignature: tree:add_node(node, parent_id?)
Adds a node to the tree.
| Name | Type | Description |
|---|---|---|
node | NuiTree.Node | node |
parent_id | string or nil | parent node's id |
If parent_id is present, node is added under that parent,
Otherwise node is added to the tree root.
tree:remove_nodeSignature: tree:remove_node(node)
Removes a node from the tree.
Returns the removed node.
| Name | Type | Description |
|---|---|---|
node_id | string | node's id |
tree:set_nodesSignature: tree:set_nodes(nodes, parent_id?)
Adds a node to the tree.
| Name | Type | Description |
|---|---|---|
nodes | NuiTree.Node[] | list of nodes |
parent_id | string or nil | parent node's id |
If parent_id is present, nodes are set as parent node's children,
otherwise nodes are set at tree root.
tree:renderSignature: tree:render(linenr_start?)
Renders the tree on buffer.
| Name | Type | Description |
|---|---|---|
linenr_start | number / nil | start line number (1-indexed) |
NuiTree.Node is used to create a node object for NuiTree.
Signature: NuiTree.Node(data, children)
Examples
local NuiTree = require("nui.tree")
local node = NuiTree.Node({ text = "b" }, {
NuiTree.Node({ text = "b-1" }),
NuiTree.Node({ text = "b-2" }),
})
dataType: table
Data for the node. Can contain anything. The default get_node_id
and prepare_node functions uses the id and text keys.
Example
{
id = "/usr/local/bin/lua",
text = "lua"
}
If you don't want to provide those two values, you should consider
providing your own get_node_id and prepare_node functions.
childrenType: table
List of NuiTree.Node objects.
node:get_idSignature: node:get_id()
Returns node's id.
node:get_depthSignature: node:get_depth()
Returns node's depth.
node:get_parent_idSignature: node:get_parent_id()
Returns parent node's id.
node:has_childrenSignature: node:has_children()
Checks if node has children.
node:get_child_idsSignature: node:get_child_ids() -> string[]
Returns ids of child nodes.
node:is_expandedSignature: node:is_expanded()
Checks if node is expanded.
node:expandSignature: node:expand()
Expands node.
node:collapseSignature: node:collapse()
Collapses node.
You can find additional documentation/examples/guides/tips-n-tricks in nui.tree wiki page.