resolvePathToNodeId method
Batch-expand an entire hierarchy path in a single pass.
Walks pathSegments from the top-level expanded node downward,
calling expandChild at each intermediate level and optionally
expandWire at the final level. All graph mutations happen
before any serialisation or layout, so the caller only needs a
single toJsGraph() → ELK → setState() cycle afterwards.
pathSegments are the instance names along the path (e.g.
'CPU', 'ALU', 'adder_0').
If targetWireName is non-null the last segment is treated as
Resolve a list of instance-name path segments to the node address
(ID) of the last segment's node.
Walks the graph from the root using the same parent-resolution
logic as expandPath but without mutating the graph. Returns
null if any segment cannot be found.
Implementation
String? resolvePathToNodeId(List<String> pathSegments) {
if (pathSegments.isEmpty) {
return null;
}
final firstName = pathSegments.first;
String? currentParentId;
if (_findChildIdByName(root.children, firstName) != null ||
_findChildIdByName(root.hiddenChildren ?? [], firstName) != null) {
currentParentId = root.id;
} else {
for (final child in root.children) {
if (_findChildIdByName(child.children, firstName) != null ||
_findChildIdByName(child.hiddenChildren ?? [], firstName) != null) {
currentParentId = child.id;
break;
}
}
}
if (currentParentId == null) {
return null;
}
String? lastNodeId;
for (final name in pathSegments) {
final parent = nodeMap[currentParentId];
if (parent == null) {
return null;
}
final childId = _findChildIdByName(parent.children, name) ??
_findChildIdByName(parent.hiddenChildren ?? [], name);
if (childId == null) {
return null;
}
lastNodeId = childId;
currentParentId = childId;
}
return lastNodeId;
}