Skip to content

Commit

Permalink
Merge pull request #6 from willothy/tree-fix
Browse files Browse the repository at this point in the history
Tree fixes for nested node handling (scroll and id generation)
  • Loading branch information
mobily committed Mar 28, 2024
2 parents 2196941 + d385bcf commit ca7d5fc
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions lua/nui-components/tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,36 @@ local fn = require("nui-components.utils.fn")

local function focus_item(instance, direction, current_linenr)
local actions = instance:get_actions()
---@type NuiTree
local tree = instance:get_tree()

local curr_linenr = current_linenr or vim.api.nvim_win_get_cursor(instance.winid)[1]
local next_linenr = nil

local function tree_height(node_id)
local height = 1
local node = tree:get_node(node_id)
if node and node:has_children() and node:is_expanded() then
for _, child in ipairs(node:get_child_ids()) do
height = height + tree_height(child)
end
end
return height
end

local height = fn.ireduce(tree:get_nodes(), function(acc, node)
return acc + tree_height(node._id)
end, 0)

if direction == "next" then
if curr_linenr == instance:get_max_lines() then
if curr_linenr == height then
next_linenr = 1
else
next_linenr = curr_linenr + 1
end
elseif direction == "prev" then
if curr_linenr == 1 then
next_linenr = instance:get_max_lines()
next_linenr = height
else
next_linenr = curr_linenr - 1
end
Expand Down Expand Up @@ -257,16 +273,15 @@ function Tree:on_mount()
self._private.tree = NuiTree({
bufnr = self.bufnr,
ns_id = self.ns_id,
nodes = fn.imap(props.data, function(node)
if node.id then
node._id = node.id
return node
end

node._id = tostring(math.random())
return node
end),
nodes = props.data,
get_node_id = function(node)
if node._id == nil then
if node.id then
node._id = node.id
else
node._id = tostring(math.random())
end
end
return node._id
end,
prepare_node = actions.prepare_node,
Expand Down

0 comments on commit ca7d5fc

Please sign in to comment.