benjamin-hodgson / Sawmill

Simple tools for working with immutable trees

Home Page:https://www.benjamin.pizza/Sawmill/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add property/method to Cursor to retrieve list of parents from oldest to most recent

bjorg opened this issue · comments

When traversing the tree, I would like to access properties from parent nodes (e.g. inheritance). Since inner nodes can be shared across multiple trees, it means they cannot keep a reference to their parent (as they may have multiple). Cursor could be used to determine the list of parent nodes, but I didn't see a property/method for it.

Is there another way to achieve this?

Thanks for getting in touch! I think you can probably code this up yourself using Cursor.GetPath. Read the path to the current node into a list, return the cursor to the Top of the tree, and then follow the path downwards step by step to visit each of the ancestors.

However I wonder whether this is an XY problem. If you're somewhere inside a syntax tree, it's generally because you descended to that location from the root. This means you've already seen the current node's parents. So you should be able to collect whatever information you need "on the way down".