Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree fixes for nested node handling (scroll and id generation) #6

Merged
merged 1 commit into from
Mar 28, 2024

Conversation

willothy
Copy link
Collaborator

Fixes issues with the tree component throwing errors when provided a list of nodes with more than 1 level of depth.

  1. Lazily generate ids with get_node_id instead of generating them only for top-level nodes
  2. Recursively calculate tree height on scroll to take into account expanded/contracted nodes

fix(tree): id generation for non-root nodes
@mobily
Copy link
Contributor

mobily commented Mar 27, 2024

hey @willothy, thanks for contributing! I'm wondering if you could provide the code that generates the bug. I plan on writing some unit tests, and it would be great to cover that case.

@willothy
Copy link
Collaborator Author

willothy commented Mar 27, 2024

Any configuration of a tree with more than one level of nodes caused errors for me, are you not able to reproduce?

I don't have the original code but I can come up with a small example

@mobily
Copy link
Contributor

mobily commented Mar 27, 2024

Any configuration of a tree with more than one level of nodes

I probably haven't tested that case.

I don't have the original code but I can come up with a small example

that would be helpful!

@willothy
Copy link
Collaborator Author

willothy commented Mar 27, 2024

This can reproduce the error for me:

local NC = require("nui-components")

local body = function()
  return NC.tree({
    size = 5,
    border_label = "Results",
    data = {
      NC.node({
        text = "Result 1",
      }),
      NC.node({
        text = "Result 2",
      }, {
        NC.node({
          text = "Result 3",
        }),
        NC.node({
          text = "Result 4",
        }),
      }),
    },
    prepare_node = function(node, line, component)
      local depth = node:get_depth()
      line:append(string.rep(" ", (depth - 1) * 2))
      if node:has_children() then
        if node:is_expanded() then
          line:append("v ")
        else
          line:append("> ")
        end
      else
        line:append("- ")
      end
      line:append(node.text)
      return line
    end,
  })
end

local view = NC.create_renderer({
  width = 40,
  height = 5,
})

view:render(body)

@mobily mobily self-requested a review March 28, 2024 07:39
@mobily mobily merged commit ca7d5fc into grapp-dev:main Mar 28, 2024
@mobily mobily added the bugfix label Mar 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants