expandPath method
the container of that wire: the container is revealed via
expandChild, then expandWire is called on it. When null the
last segment is treated as the final target itself (module
search).
Returns true if at least one graph mutation occurred.
Implementation
bool expandPath(List<String> pathSegments, {String? targetWireName}) {
if (pathSegments.isEmpty) {
return false;
}
var changed = false;
// Locate the starting parent — the node that contains the first
// path segment as a child (visible or hidden).
//
// In the real app the structure is:
// root → `topModule` (topModule is partially/blocks-only expanded)
// topModule → CPU, Memory, ...
//
// The path segments start at "CPU", so we need to descend through
// root's visible children to find who actually owns "CPU".
final firstName = pathSegments.first;
String? currentParentId;
// Check root itself first (matches the unit-test fixture).
if (_findChildIdByName(root.children, firstName) != null ||
_findChildIdByName(root.hiddenChildren ?? [], firstName) != null) {
currentParentId = root.id;
} else {
// Check root's visible children (e.g. the top module).
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 false;
}
for (var i = 0; i < pathSegments.length; i++) {
final name = pathSegments[i];
final isLast = i == pathSegments.length - 1;
final parent = nodeMap[currentParentId];
if (parent == null) {
break;
}
// Try to find the child among visible children first.
var childNodeId = _findChildIdByName(parent.children, name);
// Also check partially-expanded children: they live in
// hiddenChildren but are conceptually visible via partialChildIds.
if (childNodeId == null && parent.isPartiallyExpanded) {
final candidate = _findChildIdByName(parent.hiddenChildren ?? [], name);
if (candidate != null && parent.partialChildIds!.contains(candidate)) {
childNodeId = candidate;
}
}
// If not visible, it's in hiddenChildren — expandChild will reveal it.
if (childNodeId == null) {
if (expandChild(currentParentId!, name)) {
changed = true;
}
// After expandChild the child's ID is now in partialChildIds.
// Re-search hidden children to get the ID.
childNodeId = _findChildIdByName(parent.hiddenChildren ?? [], name);
}
if (childNodeId == null) {
break;
}
if (isLast) {
if (targetWireName != null) {
// Wire search: the last segment is the wire's container.
// Ensure the container is visible, then reveal the wire inside it.
if (expandWire(childNodeId, targetWireName)) {
changed = true;
}
} else {
// Module search: ensure the target itself is visible in its
// parent. expandChild is additive/idempotent.
if (expandChild(currentParentId!, name)) {
changed = true;
}
}
} else {
// Intermediate: reveal the *next* segment inside this child.
// First, make sure *this* child is visible in the parent.
if (expandChild(currentParentId!, name)) {
changed = true;
}
}
currentParentId = childNodeId;
}
return changed;
}