Skip to content

Commit 3926337

Browse files
authored
Fix traversal when deleting nodes (#3358)
* Fix deletion * Fix traversal over primary
1 parent a6f8329 commit 3926337

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

editor/src/messages/portfolio/document/utility_types/network_interface.rs

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4206,42 +4206,59 @@ impl NodeNetworkInterface {
42064206
continue;
42074207
};
42084208

4209-
for upstream_id in self.upstream_flow_back_from_nodes(vec![*node_id], network_path, FlowType::LayerChildrenUpstreamFlow) {
4210-
// This does a downstream traversal starting from the current node, and ending at either a node in the `delete_nodes` set or the output.
4211-
// If the traversal find as child node of a node in the `delete_nodes` set, then it is a sole dependent. If the output node is eventually reached, then it is not a sole dependent.
4212-
let mut stack = vec![OutputConnector::node(upstream_id, 0)];
4209+
// Perform an upstream traversal to try delete children for secondary inputs
4210+
let mut upstream_nodes = (1..self.number_of_inputs(node_id, network_path))
4211+
.filter_map(|input_index| self.upstream_output_connector(&InputConnector::node(*node_id, input_index), network_path).and_then(|oc| oc.node_id()))
4212+
.collect::<Vec<_>>();
4213+
while let Some(upstream_node) = upstream_nodes.pop() {
4214+
// Add the upstream nodes to the traversal
4215+
for input_connector in (0..self.number_of_inputs(&upstream_node, network_path)).map(|input_index| InputConnector::node(upstream_node, input_index)) {
4216+
if let Some(upstream_node) = self.upstream_output_connector(&input_connector, network_path).and_then(|oc| oc.node_id()) {
4217+
upstream_nodes.push(upstream_node);
4218+
}
4219+
}
4220+
// For each potential child perform a complete downstream traversal, ending at either a node in the `delete_nodes` set (excluding layer bottom inputs), the output, or a dead end.
4221+
// If the output node is eventually reached, then it is not a sole dependent and will not be deleted
4222+
let mut stack = vec![upstream_node];
42134223
let mut can_delete = true;
42144224
while let Some(current_node) = stack.pop() {
4215-
let current_node_id = current_node.node_id().expect("The current node in the delete stack cannot be the export");
4216-
let Some(downstream_nodes) = outward_wires.get(&current_node) else { continue };
4217-
for downstream_node in downstream_nodes {
4218-
if let InputConnector::Node { node_id: downstream_id, .. } = downstream_node {
4219-
if !delete_nodes.contains(downstream_id) {
4220-
can_delete = false;
4221-
break;
4225+
let mut is_dead_end = true;
4226+
for output_connector in (0..self.number_of_outputs(&current_node, network_path)).map(|output_index| OutputConnector::node(current_node, output_index)) {
4227+
let Some(downstream_nodes) = outward_wires.get(&output_connector) else { continue };
4228+
if !downstream_nodes.is_empty() {
4229+
is_dead_end = false
4230+
}
4231+
for downstream_node in downstream_nodes {
4232+
if let InputConnector::Node { node_id: downstream_id, input_index } = downstream_node {
4233+
// If the downstream node is not in the delete nodes set, then continue iterating
4234+
// If the downstream node is the bottom input of a layer then continue iterating
4235+
if !delete_nodes.contains(downstream_id) || (*input_index == 0 && self.is_layer(downstream_id, network_path)) {
4236+
stack.push(*downstream_id);
4237+
}
4238+
// If the traversal reaches the primary input of the node to delete then do not delete it
4239+
if node_id == downstream_id && *input_index == 0 {
4240+
can_delete = false;
4241+
stack = Vec::new();
4242+
break;
4243+
}
42224244
}
4223-
// Continue traversing over the downstream sibling, if the current node is a sibling to a node that will be deleted and it is a layer
4245+
// If the traversal reaches the export, then the current node is not a sole dependent and cannot be deleted
42244246
else {
4225-
for deleted_node_id in &nodes_to_delete {
4226-
let Some(downstream_node) = self.document_node(deleted_node_id, network_path) else { continue };
4227-
let Some(input) = downstream_node.inputs.first() else { continue };
4228-
4229-
if let NodeInput::Node { node_id, .. } = input
4230-
&& *node_id == current_node_id
4231-
{
4232-
stack.push(OutputConnector::node(*deleted_node_id, 0));
4233-
}
4234-
}
4247+
can_delete = false;
4248+
stack = Vec::new();
4249+
break;
42354250
}
42364251
}
4237-
// If the traversal reaches the export, then the current node is not a sole dependent
4238-
else {
4239-
can_delete = false;
4240-
}
4252+
}
4253+
// If there are no outward wires, then we have reached a dead end, and the node cannot be deleted
4254+
if is_dead_end {
4255+
can_delete = false;
4256+
stack = Vec::new();
42414257
}
42424258
}
4259+
42434260
if can_delete {
4244-
delete_nodes.insert(upstream_id);
4261+
delete_nodes.insert(upstream_node);
42454262
}
42464263
}
42474264
}

0 commit comments

Comments
 (0)