Skip to content

Commit c1ac787

Browse files
adamgerhantKeavon
authored andcommitted
Fix deletion
1 parent a6f8329 commit c1ac787

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

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

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4206,42 +4206,53 @@ 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+
}
42224238
}
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
4239+
// If the traversal reaches the export, then the current node is not a sole dependent and cannot be deleted
42244240
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-
}
4241+
can_delete = false;
4242+
stack = Vec::new();
4243+
break;
42354244
}
42364245
}
4237-
// If the traversal reaches the export, then the current node is not a sole dependent
4238-
else {
4239-
can_delete = false;
4240-
}
4246+
}
4247+
// If there are no outward wires, then we have reached a dead end, and the node cannot be deleted
4248+
if is_dead_end {
4249+
can_delete = false;
4250+
stack = Vec::new();
42414251
}
42424252
}
4253+
42434254
if can_delete {
4244-
delete_nodes.insert(upstream_id);
4255+
delete_nodes.insert(upstream_node);
42454256
}
42464257
}
42474258
}

0 commit comments

Comments
 (0)