Skip to content

Commit ef30166

Browse files
committed
Fix deletion
1 parent 7afbeaa commit ef30166

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
@@ -4213,42 +4213,53 @@ impl NodeNetworkInterface {
42134213
continue;
42144214
};
42154215

4216-
for upstream_id in self.upstream_flow_back_from_nodes(vec![*node_id], network_path, FlowType::LayerChildrenUpstreamFlow) {
4217-
// This does a downstream traversal starting from the current node, and ending at either a node in the `delete_nodes` set or the output.
4218-
// 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.
4219-
let mut stack = vec![OutputConnector::node(upstream_id, 0)];
4216+
// Perform an upstream traversal to try delete children for secondary inputs
4217+
let mut upstream_nodes = (1..self.number_of_inputs(node_id, network_path))
4218+
.filter_map(|input_index| self.upstream_output_connector(&InputConnector::node(*node_id, input_index), network_path).and_then(|oc| oc.node_id()))
4219+
.collect::<Vec<_>>();
4220+
while let Some(upstream_node) = upstream_nodes.pop() {
4221+
// Add the upstream nodes to the traversal
4222+
for input_connector in (0..self.number_of_inputs(&upstream_node, network_path)).map(|input_index| InputConnector::node(upstream_node, input_index)) {
4223+
if let Some(upstream_node) = self.upstream_output_connector(&input_connector, network_path).and_then(|oc| oc.node_id()) {
4224+
upstream_nodes.push(upstream_node);
4225+
}
4226+
}
4227+
// 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.
4228+
// If the output node is eventually reached, then it is not a sole dependent and will not be deleted
4229+
let mut stack = vec![upstream_node];
42204230
let mut can_delete = true;
42214231
while let Some(current_node) = stack.pop() {
4222-
let current_node_id = current_node.node_id().expect("The current node in the delete stack cannot be the export");
4223-
let Some(downstream_nodes) = outward_wires.get(&current_node) else { continue };
4224-
for downstream_node in downstream_nodes {
4225-
if let InputConnector::Node { node_id: downstream_id, .. } = downstream_node {
4226-
if !delete_nodes.contains(downstream_id) {
4227-
can_delete = false;
4228-
break;
4232+
let mut is_dead_end = true;
4233+
for output_connector in (0..self.number_of_outputs(&current_node, network_path)).map(|output_index| OutputConnector::node(current_node, output_index)) {
4234+
let Some(downstream_nodes) = outward_wires.get(&output_connector) else { continue };
4235+
if !downstream_nodes.is_empty() {
4236+
is_dead_end = false
4237+
}
4238+
for downstream_node in downstream_nodes {
4239+
if let InputConnector::Node { node_id: downstream_id, input_index } = downstream_node {
4240+
// If the downstream node is not in the delete nodes set, then continue iterating
4241+
// If the downstream node is the bottom input of a layer then continue iterating
4242+
if !delete_nodes.contains(downstream_id) || (*input_index == 0 && self.is_layer(downstream_id, network_path)) {
4243+
stack.push(*downstream_id);
4244+
}
42294245
}
4230-
// 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
4246+
// If the traversal reaches the export, then the current node is not a sole dependent and cannot be deleted
42314247
else {
4232-
for deleted_node_id in &nodes_to_delete {
4233-
let Some(downstream_node) = self.document_node(deleted_node_id, network_path) else { continue };
4234-
let Some(input) = downstream_node.inputs.first() else { continue };
4235-
4236-
if let NodeInput::Node { node_id, .. } = input
4237-
&& *node_id == current_node_id
4238-
{
4239-
stack.push(OutputConnector::node(*deleted_node_id, 0));
4240-
}
4241-
}
4248+
can_delete = false;
4249+
stack = Vec::new();
4250+
break;
42424251
}
42434252
}
4244-
// If the traversal reaches the export, then the current node is not a sole dependent
4245-
else {
4246-
can_delete = false;
4247-
}
4253+
}
4254+
// If there are no outward wires, then we have reached a dead end, and the node cannot be deleted
4255+
if is_dead_end {
4256+
can_delete = false;
4257+
stack = Vec::new();
42484258
}
42494259
}
4260+
42504261
if can_delete {
4251-
delete_nodes.insert(upstream_id);
4262+
delete_nodes.insert(upstream_node);
42524263
}
42534264
}
42544265
}

0 commit comments

Comments
 (0)