grapp-dev / nui-components.nvim

A feature-rich and highly customizable library for creating user interfaces in Neovim.
https://nui-components.grapp.dev
MIT License
330 stars 7 forks source link

Rerender crashes when updating Tree’s data. #44

Open gregorias opened 4 months ago

gregorias commented 4 months ago

Description

In a tree component, when I set the data property to a signal value and update it, no changes happen.

This behaviour is associated with an error thrown in https://github.com/grapp-dev/nui-components.nvim/blob/caecfe2089e5ffbe99c2b0e0468da91990263a90/lua/nui-components/tree.lua#L256

The error is: "...ek/.local/share/nvim/lazy/nui.nvim/lua/nui/tree/init.lua:450: attempt to index field 'linenr' (a nil value)" and it comes from Nui’s tree component.

It might be that NuiComponents incorrectly modifies the _ property and removes the linenr property.

Repro

local n = require("nui-components")

local my_menu = function(items_signal)
  return n.tree({
    flex = 1,
    border_label = " Options ",
    data = items_signal.items:map(function(items_val)
            local nodes = {}
            for _, item in pairs(items_val) do
                table.insert(nodes, n.node({ text = item }))
            end
            return nodes
    end),
    prepare_node = function(node, line, component)
      line:append(node.text)
      return line
    end,
  })
end

local my_dashboard = function()
  local renderer = n.create_renderer({
    width = 60,
    height = 12,
  })
  local base_items = { "a", "ab", "abc" }
  local items_signal = n.create_signal({ items = base_items })
  local menu_component = my_menu(items_signal)

  local body = function()
    return n.rows(
      menu_component,
      n.text_input({
        autofocus = true,
        max_lines = 1,
        on_change = function(value)
          local new_items = {}
          for _, item in pairs(items_signal.items:get_value()) do
            if item:find(value) then
              table.insert(new_items, item)
            end
          end
          items_signal.items = new_items
        end,
      })
    )
  end

  renderer:render(body)
end

my_dashboard()

Current Behavior

When writing filter text, the tree component remains unchanged.

Expected Behavior

When writing filter text, the tree component updates with new entries.

NuiComponents version

1.5.2

Neovim version

0.10