MunifTanjim / nui.nvim

UI Component Library for Neovim.
MIT License
1.62k stars 57 forks source link

[Feature] sort node children #339

Closed pysan3 closed 7 months ago

pysan3 commented 8 months ago

Hi, thanks again for the awesome library.

Is there a way to sort node children?

I'd imagine an API similar to table.sort but I couldn't find anything.

local node = ...

---@param a NuiTreeNode
---@param b NuiTreeNode
node:sort_children(function (a, b)
  -- sort by string length of id.
  return a:get_id():len() < b:get_id():len()
end)

tree:render()
MunifTanjim commented 8 months ago

You can get the current nodes using tree:get_nodes, sort it, and then set the sorted nodes using tree:set_nodes.

pysan3 commented 8 months ago

I see but that runs initialize_nodes which runs a recursion thru children, which is kind of inefficient IMO.

I just want to rearrange the children of a single node in (or not in) a tree.

As sorting does not change or modify any node, tree.nodes.by_id does not need to change if I'm not mistaken.

MunifTanjim commented 7 months ago

Would be nice if we can optimize the current methods instead of adding a new one. I'm very hesitatnt on adding new methods... the philosophy of nui.nvim always has been to provide very general minimal APIs so that users can build on top of that.

pysan3 commented 7 months ago

OK, yes I now started to think I should implement an application specific sorter anyways so that it can be more optimized than a general API from nui.

Thanks for your opinions. Closing!