buildRef static method
Builds the reference to element by walking up its parents.
The named elements up to the first unnamed one (an instance's hidden root, which makes the reference absolute) give the levels; the last element visited gives the instance name and context type. Same result as prepending one step per element with TreeReference.parent, in one list.
Implementation
static TreeReference buildRef(TreeElement? element) {
if (element == null) return const TreeReference.self();
final levels = <TreeReferenceLevel>[];
var top = element;
for (TreeElement? e = element; e != null; e = e.parent) {
top = e;
final elementName = e.name;
if (elementName == null) break;
levels.add(TreeReferenceLevel(elementName, e.multiplicity));
}
final instanceName = top.instanceName;
final absolute = top.name == null;
return TreeReference.ofLevels(
levels.reversed.toList(),
refLevel: absolute ? TreeReference.refAbsolute : 0,
contextType: instanceName != null
? ReferenceContext.instance
: absolute
? ReferenceContext.absolute
: ReferenceContext.inherited,
instanceName: instanceName,
);
}