1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-03 07:00:05 +08:00
SpaceVim/bundle/nui.nvim/lua/nui/tree/util.lua
2023-05-30 21:09:18 +08:00

71 lines
1.3 KiB
Lua

local NuiLine = require("nui.line")
local mod = {}
---@param node table NuiTreeNode
---@return string node_id id
function mod.default_get_node_id(node)
if node.id then
return "-" .. node.id
end
if node.text then
local texts = node.text
if type(node.text) ~= "table" or node.text.content then
texts = { node.text }
end
return string.format(
"%s-%s-%s",
node._parent_id or "",
node._depth,
table.concat(
vim.tbl_map(function(text)
if type(text) == "string" then
return text
end
return text:content()
end, texts),
"-"
)
)
end
return "-" .. math.random()
end
---@param node table NuiTreeNode
---@return table[] lines NuiLine[]
function mod.default_prepare_node(node)
if not node.text then
error("missing node.text")
end
local texts = node.text
if type(node.text) ~= "table" or node.text.content then
texts = { node.text }
end
local lines = {}
for i, text in ipairs(texts) do
local line = NuiLine()
line:append(string.rep(" ", node._depth - 1))
if i == 1 and node:has_children() then
line:append(node:is_expanded() and "" or "")
else
line:append(" ")
end
line:append(text)
table.insert(lines, line)
end
return lines
end
return mod