From 3a4cdf0acb4e37ff3e71ca6e128461d1190e7ee5 Mon Sep 17 00:00:00 2001 From: Xanonymous Date: Tue, 30 Jul 2024 18:00:01 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20index=20of=20actions=20wi?= =?UTF-8?q?ll=20be=20changed=20in=20another=20coroutine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SimplifiedEditScriptGenerator.kt | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/core/src/main/kotlin/tw/xcc/gumtree/editscript/SimplifiedEditScriptGenerator.kt b/core/src/main/kotlin/tw/xcc/gumtree/editscript/SimplifiedEditScriptGenerator.kt index cd4a159..8104c63 100644 --- a/core/src/main/kotlin/tw/xcc/gumtree/editscript/SimplifiedEditScriptGenerator.kt +++ b/core/src/main/kotlin/tw/xcc/gumtree/editscript/SimplifiedEditScriptGenerator.kt @@ -14,6 +14,7 @@ import tw.xcc.gumtree.model.operations.SingleDeleteAction import tw.xcc.gumtree.model.operations.SingleInsertAction import tw.xcc.gumtree.model.operations.TreeDeleteAction import tw.xcc.gumtree.model.operations.TreeInsertAction +import java.util.Optional /** * A second-stage edit script generator that simplifies the edit script generated by the [ChawatheScriptGenerator]. @@ -40,10 +41,10 @@ class SimplifiedEditScriptGenerator( /** * Replace single tree [Action] with another tree [Action] - * if the node and all of its descendents are also in the [actions] list. + * if the node and all of its descendents are also in the [flatActions] list. * */ private suspend inline fun simplifyWith( - actions: MutableList, + flatActions: MutableList>, nodeToIndexedSingleActions: Map>, crossinline createTreeActionFrom: (origin: T) -> N ) = coroutineScope { @@ -54,13 +55,12 @@ class SimplifiedEditScriptGenerator( if (nodeAsKeys.contains(nodeParent) && nodeAsKeys.containsAll(descendentsOf(nodeParent))) { lock.withLock { - actions.removeAt(indexInOriginActions) + flatActions[indexInOriginActions] = Optional.empty() } } else if (node.childCount() > 0 && nodeAsKeys.containsAll(descendentsOf(node))) { val treeAction = createTreeActionFrom(originAction) lock.withLock { - actions.add(indexInOriginActions, treeAction) - actions.removeAt(indexInOriginActions + 1) + flatActions[indexInOriginActions] = Optional.of(treeAction) } } } @@ -68,12 +68,15 @@ class SimplifiedEditScriptGenerator( override suspend fun generateActions(): List = coroutineScope { - val actions = baseGenerator.generateActions().toMutableList() + val actions = + baseGenerator.generateActions() + .map { Optional.of(it) } + .toMutableList() val simplifiableActions = actions .flatMapIndexed { originIdx, action -> - when (action) { + when (action.get()) { is SingleInsertAction -> listOf(originIdx to action) is SingleDeleteAction -> listOf(originIdx to action) else -> emptyList() @@ -110,6 +113,6 @@ class SimplifiedEditScriptGenerator( } listOf(insertionSimplifyJob, deletionSimplifyJob).joinAll() - return@coroutineScope actions + return@coroutineScope actions.filter { it.isPresent }.map { it.get() } } } \ No newline at end of file