CARVIEW |
Select Language
HTTP/2 200
date: Wed, 30 Jul 2025 21:04:48 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
x-robots-tag: none
etag: W/"6154164aee07e542e015cf72b4ae455d"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com wss://alive-staging.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=XgI4DbYjwHlv0fFQuWJZxHyG2QSkHtPWq57%2Fy0IYBAli%2BBMPzJhu7jRw1NYs0NDCnGBFvwKcFUMHKR8NNxIgsT5HMbVJzSHie1iTCpyZCj4ieiUBbgtxcjl6Ub6DyzNwaQY3wwKADssN%2F7%2BGAElGtPXDEkpGyJMsdK1Lpp6dCRNOHzHJL1YtNM%2B7QXniIVI3%2B2JsFamimHxY96YFVrxuzqKqT3sc%2B37J6h1LOKmEylmFkDM0kb7MmpvllHgD25lAFuc7%2FO4lp6IW8fhlHQ96pA%3D%3D--s3PAvTqci%2FFXjJUC--NaRheVMi5A2UIpasscA2Zw%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.1254504066.1753909488; Path=/; Domain=github.com; Expires=Thu, 30 Jul 2026 21:04:48 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Thu, 30 Jul 2026 21:04:48 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: D55E:2C0AA0:32B90:4795C:688A88F0
nui.tree · MunifTanjim/nui.nvim Wiki · GitHub
Skip to content
Navigation Menu
{{ message }}
-
-
Notifications
You must be signed in to change notification settings - Fork 70
nui.tree
Hamidreza Ebtehaj edited this page Apr 14, 2023
·
8 revisions
Example from PR #49
Expand Example from PR #49
local NuiTree = require("nui.tree")
local Split = require("nui.split")
local NuiLine = require("nui.line")
local split = Split({
relative = "win",
position = "bottom",
size = 30,
})
split:mount()
-- quit
split:map("n", "q", function()
split:unmount()
end, { noremap = true })
local tree = NuiTree({
winid = split.winid,
nodes = {
NuiTree.Node({ text = "a" }),
NuiTree.Node({ text = "b" }, {
NuiTree.Node({ text = "b-1" }),
NuiTree.Node({ text = "b-2" }, {
NuiTree.Node({ text = "b-1-a" }),
NuiTree.Node({ text = "b-2-b" }),
}),
}),
NuiTree.Node({ text = "c" }, {
NuiTree.Node({ text = "c-1" }),
NuiTree.Node({ text = "c-2" }),
}),
},
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 " ", "SpecialChar")
else
line:append(" ")
end
line:append(node.text)
return line
end,
})
local map_options = { noremap = true, nowait = true }
-- print current node
split:map("n", "<CR>", function()
local node = tree:get_node()
print(vim.inspect(node))
end, map_options)
-- collapse current node
split:map("n", "h", function()
local node = tree:get_node()
if node:collapse() then
tree:render()
end
end, map_options)
-- collapse all nodes
split:map("n", "H", function()
local updated = false
for _, node in pairs(tree.nodes.by_id) do
updated = node:collapse() or updated
end
if updated then
tree:render()
end
end, map_options)
-- expand current node
split:map("n", "l", function()
local node = tree:get_node()
if node:expand() then
tree:render()
end
end, map_options)
-- expand all nodes
split:map("n", "L", function()
local updated = false
for _, node in pairs(tree.nodes.by_id) do
updated = node:expand() or updated
end
if updated then
tree:render()
end
end, map_options)
-- add new node under current node
split:map("n", "a", function()
local node = tree:get_node()
tree:add_node(
NuiTree.Node({ text = "d" }, {
NuiTree.Node({ text = "d-1" }),
}),
node:get_id()
)
tree:render()
end, map_options)
-- delete current node
split:map("n", "d", function()
local node = tree:get_node()
tree:remove_node(node:get_id())
tree:render()
end, map_options)
tree:render()
open_treesitter_query_files_browser()
-- or
open_treesitter_query_files_browser("c", "go", "lua", "rust")
Expand Treesitter Query Files Browser
local Popup = require("nui.popup")
local Line = require("nui.line")
local Text = require("nui.text")
local Tree = require("nui.tree")
local event = require("nui.utils.autocmd").event
local function read_file(filename)
local file, err = io.open(filename, "r")
if not file then
error(err)
end
local content = file:read("*a")
io.close(file)
return content
end
local function map(items, callback)
local result = {}
for i, item in ipairs(items) do
result[i] = callback(item, i)
end
return result
end
local function for_each(items, callback)
for i, item in ipairs(items) do
callback(item, i)
end
end
local mod = {}
local function list_query_sources(lang)
local sources = {}
local paths = vim.treesitter.get_query_files(lang, "*")
local is_seen = {}
for i, path in ipairs(paths) do
if not is_seen[path] then
is_seen[path] = true
sources[i] = {
lang = lang,
filepath = path,
plugin_name = vim.fn.fnamemodify(path, ":h:h:h:t"),
query_name = vim.fn.fnamemodify(path, ":t:r"),
}
end
end
return sources
end
local function check_query_file_health(node)
local query_content = read_file(node.filepath)
local ok, err = pcall(vim.treesitter.query.parse_query, node.lang, query_content)
if ok then
node.ok = true
node.err = nil
node.err_position = nil
else
node.ok = false
node.err = err
node.err_position = string.match(node.err, "position (%d+)")
end
end
local function get_query_source_nodes(langs)
local nodes = {}
for _, lang in ipairs(langs) do
local query_sources = list_query_sources(lang)
local children = map(query_sources, function(source)
source.id = lang .. "-" .. source.filepath
return Tree.Node(source)
end)
table.insert(nodes, Tree.Node({ text = lang }, children))
end
return nodes
end
function mod.open_treesitter_query_files_browser(...)
local langs = { ... }
if #langs == 0 then
langs = require("nvim-treesitter.info").installed_parsers()
end
local popup = Popup({
enter = true,
position = "50%",
size = {
width = "80%",
height = "60%",
},
border = {
style = "rounded",
text = {
top = "Treesitter Query Files",
},
},
buf_options = {
readonly = true,
modifiable = false,
},
win_options = {
winhighlight = "Normal:Normal,FloatBorder:Normal",
},
})
popup:mount()
popup:on({ event.BufWinLeave }, function()
vim.schedule(function()
popup:unmount()
end)
end, { once = true })
local tree = Tree({
winid = popup.winid,
nodes = get_query_source_nodes(langs),
prepare_node = function(node)
local line = Line()
if node:has_children() then
line:append(node:is_expanded() and " " or " ", "SpecialChar")
line:append("Language: " .. node.text)
return line
end
line:append(" [")
if node.ok then
line:append(Text("✓", "DiagnosticSignInfo"))
elseif node.err then
line:append(Text("×", "DiagnosticSignError"))
else
line:append(" ")
end
line:append("] ")
line:append(node.plugin_name .. " ::: " .. node.query_name, {
virt_text = { { " ::: " .. vim.fn.fnamemodify(node.filepath, ":~"), "Folded" } },
virt_text_pos = "eol",
})
return line
end,
})
local map_options = { remap = false, nowait = true }
-- exit
popup:map("n", { "q", "<esc>" }, function()
popup:unmount()
end, map_options)
-- collapse
popup:map("n", "h", function()
local node, linenr = tree:get_node()
if not node:has_children() then
node, linenr = tree:get_node(node:get_parent_id())
end
if node and node:collapse() then
vim.api.nvim_win_set_cursor(popup.winid, { linenr, 0 })
tree:render()
end
end, map_options)
-- expand
popup:map("n", "l", function()
local node, linenr = tree:get_node()
if not node:has_children() then
node, linenr = tree:get_node(node:get_parent_id())
end
if node and node:expand() then
if not node.checked then
node.checked = true
vim.schedule(function()
for _, n in ipairs(tree:get_nodes(node:get_id())) do
check_query_file_health(n)
end
tree:render()
end)
end
vim.api.nvim_win_set_cursor(popup.winid, { linenr, 0 })
tree:render()
end
end, map_options)
-- open
popup:map("n", "o", function()
local node = tree:get_node()
if not node.filepath then
return
end
vim.cmd(
string.format(
"tab drop %s | %s",
node.filepath,
node.err_position and string.gsub(string.format([[goto %s]], node.err_position), " ", " ") or ""
)
)
end, map_options)
-- refresh
popup:map("n", "r", function()
local node = tree:get_node()
vim.schedule(function()
if node:has_children() then
for_each(tree:get_nodes(node:get_id()), check_query_file_health)
else
check_query_file_health(node)
end
tree:render()
end)
end, map_options)
tree:render()
end
return mod
local function get_expanded_nodes(tree)
local nodes = {}
local function process(node)
if node:is_expanded() then
table.insert(nodes, node)
if node:has_children() then
for _, node in tree:get_nodes(node:get_id()) do
process(node)
end
end
end
end
for _, node in tree:get_nodes() do
process(node)
end
return nodes
end
Building upon the prior example, you can then collapse all nodes with:
local function collapse_all_nodes(tree)
local expanded = get_expanded_nodes(tree)
for _, id in ipairs(expanded) do
local node = tree:get_node(id)
node:collapse(id)
end
-- If you want to expand the root
-- local root = tree:get_nodes()[1]
-- root:expand()
end
You can’t perform that action at this time.