Skip to content

Commit f5e37cd

Browse files
author
Nikolas Dahn
committed
Fixed some bugs in delete subtree
1 parent 4a56329 commit f5e37cd

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

hkb_editor/gui/beh_editor.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,28 +1520,25 @@ def _delete_node_cascade(self, node: Node) -> None:
15201520
return
15211521

15221522
root_graph = self.beh.root_graph()
1523-
node_graph = self.beh.build_graph(node.id)
1524-
1525-
delete_list: list[str] = []
1523+
delete_list: list[str] = [node.id]
15261524
children = set(nx.descendants(root_graph, node.id))
1527-
for child, in_degree in root_graph.in_degree(children):
1528-
# Ignore any in-edges from parents in the node's subtree
1529-
for parent in root_graph.predecessors(child):
1530-
if parent in node_graph:
1531-
in_degree -= 1
15321525

1533-
if in_degree == 0:
1534-
delete_list.append(child)
1526+
for n in children:
1527+
for parent in root_graph.predecessors(n):
1528+
if parent != node.id and parent not in children:
1529+
# Found a parent outside the to be deleted subtree, keep this child
1530+
break
1531+
else:
1532+
delete_list.append(n)
15351533

15361534
self.logger.info(
15371535
f"Deleting {len(delete_list)} descendants of node {node.id} with no other parents"
15381536
)
15391537
with self.beh.transaction():
15401538
self._on_node_delete(node.id)
15411539

1542-
self.beh.delete_object(node.id)
1543-
for child in delete_list:
1544-
self.beh.delete_object(child)
1540+
for n in reversed(delete_list):
1541+
self.beh.delete_object(n)
15451542

15461543
self.regenerate()
15471544

@@ -1558,7 +1555,10 @@ def _on_node_delete(self, object_id: str) -> None:
15581555
first_parent = parent_id
15591556

15601557
parent = self.beh.objects[parent_id]
1561-
for path, ptr in parent.find_fields_by_class(HkbPointer):
1558+
references = list(parent.find_fields_by_class(HkbPointer))
1559+
1560+
# Reverse so array indices stay valid
1561+
for path, ptr in reversed(references):
15621562
if ptr.get_value() == object_id:
15631563
index_match = re.match(r"^(.*):([0-9]+)$", path)
15641564
if index_match:

0 commit comments

Comments
 (0)