Skip to content

Commit 0a218e7

Browse files
committed
fix(model-sync-lib): fixed issue when moving nodes across parents
1 parent 8df10e9 commit 0a218e7

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

model-sync-lib/src/main/kotlin/org/modelix/model/sync/ModelImporter.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ModelImporter(private val root: INode) {
1212
private lateinit var originalIdToRef: MutableMap<String, INodeReference>
1313
private lateinit var originalIdToSpec: MutableMap<String, NodeData>
1414
private lateinit var originalIdToParentSpec: MutableMap<String, NodeData>
15-
private lateinit var existingNodeIds: MutableSet<String>
15+
private lateinit var originalIdToExisting: MutableMap<String, INode>
1616

1717
fun import(jsonFile: File) {
1818
require(jsonFile.exists())
@@ -23,8 +23,8 @@ class ModelImporter(private val root: INode) {
2323
}
2424

2525
fun import(data: ModelData) {
26-
existingNodeIds = mutableSetOf()
27-
collectExistingNodeIds(root)
26+
originalIdToExisting = mutableMapOf()
27+
buildExistingIndex(root)
2828

2929
originalIdToSpec = mutableMapOf()
3030
buildSpecIndex(data.root)
@@ -41,9 +41,9 @@ class ModelImporter(private val root: INode) {
4141
syncAllChildOrders(root, data.root)
4242
}
4343

44-
private fun collectExistingNodeIds(root: INode) {
45-
root.originalId()?.let { existingNodeIds.add(it) }
46-
root.allChildren.forEach { collectExistingNodeIds(it) }
44+
private fun buildExistingIndex(root: INode) {
45+
root.originalId()?.let { originalIdToExisting[it] = root }
46+
root.allChildren.forEach { buildExistingIndex(it) }
4747
}
4848

4949
private fun buildParentSpecIndex(nodeData: NodeData) {
@@ -94,7 +94,7 @@ class ModelImporter(private val root: INode) {
9494
val existingIds = node.allChildren.map { it.originalId() }.toSet()
9595
val missingNodes = specifiedNodes.filter { !existingIds.contains(it.originalId()) }
9696

97-
val toBeMovedHere = missingNodes.filter { existingNodeIds.contains(it.originalId()) }.toSet()
97+
val toBeMovedHere = missingNodes.filter { originalIdToExisting.containsKey(it.originalId()) }.toSet()
9898
val toBeAdded = missingNodes.subtract(toBeMovedHere)
9999

100100
toBeAdded.forEach {
@@ -104,9 +104,10 @@ class ModelImporter(private val root: INode) {
104104
}
105105

106106
toBeMovedHere.forEach {
107-
val target = originalIdToRef[it.originalId()]?.resolveNode(node.getArea())
108-
if (target != null) {
109-
node.moveChild(it.role, -1, target)
107+
val actualNode = originalIdToExisting[it.originalId()]
108+
val targetIndex = it.getIndexWithinRole(nodeData)
109+
if (actualNode != null) {
110+
node.moveChild(it.role, targetIndex, actualNode)
110111
}
111112
}
112113

0 commit comments

Comments
 (0)