Skip to content

Commit

Permalink
fix: 🐛 index of actions will be changed in another coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanonymous-GitHub committed Jul 30, 2024
1 parent ea01090 commit 3a4cdf0
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand All @@ -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 <T : Action, N : Action> simplifyWith(
actions: MutableList<Action>,
flatActions: MutableList<Optional<Action>>,
nodeToIndexedSingleActions: Map<GumTree, Pair<Int, T>>,
crossinline createTreeActionFrom: (origin: T) -> N
) = coroutineScope {
Expand All @@ -54,26 +55,28 @@ 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)
}
}
}
}

override suspend fun generateActions(): List<Action> =
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()
Expand Down Expand Up @@ -110,6 +113,6 @@ class SimplifiedEditScriptGenerator(
}

listOf(insertionSimplifyJob, deletionSimplifyJob).joinAll()
return@coroutineScope actions
return@coroutineScope actions.filter { it.isPresent }.map { it.get() }
}
}

0 comments on commit 3a4cdf0

Please sign in to comment.