IJPL-163538 replace lists with sequences in ActionAsyncProvider.collectMatchedActions

(cherry picked from commit f9a8bee4fac31b62723605feeb9e6ed8813ea5a8)

IJ-MR-146410

(cherry picked from commit fa5b5e45182850d33e42e9f9dfb5f3b2bb26c543)


(cherry picked from commit 4466b034448da0324b0ef827c7095ba361ef9561)

IJ-CR-153899

GitOrigin-RevId: b2757e75f8494adb6f4071a1be7f912628eab4de
This commit is contained in:
Mikhail Sokolov
2024-10-11 14:03:57 +02:00
committed by intellij-monorepo-bot
parent f0bbdc36e0
commit 3d6e4ca225

View File

@@ -30,9 +30,11 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.channels.toList
import java.util.concurrent.ConcurrentHashMap
import kotlin.coroutines.coroutineContext
import kotlin.sequences.forEach
private val LOG = logger<ActionAsyncProvider>()
@@ -139,36 +141,34 @@ internal class ActionAsyncProvider(private val model: GotoActionModel) {
private suspend fun collectMatchedActions(pattern: String, allIds: Collection<String>, weightMatcher: MinusculeMatcher, unmatchedIdsChannel: SendChannel<String>): List<MatchedAction> = coroutineScope {
val matcher = buildMatcher(pattern)
data class ActionWithID(val action: AnAction, val id: String?)
fun List<AnAction>.withIDs() = map { ActionWithID(it, actionManager.getId(it)) }
val mainActions: List<ActionWithID> = allIds.mapNotNull {
val mainActions: Sequence<AnAction> = allIds.asSequence().mapNotNull {
val action = actionManager.getActionOrStub(it) ?: return@mapNotNull null
if (action is ActionGroup && !action.isSearchable) return@mapNotNull null
return@mapNotNull ActionWithID(action, it)
return@mapNotNull action
}
val extendedActions: List<ActionWithID> = model.dataContext.getData(QuickActionProvider.KEY)?.getActions(true)?.withIDs() ?: emptyList()
val allActions: List<ActionWithID> = mainActions +
extendedActions +
extendedActions.flatMap { (it.action as? ActionGroup)?.let { model.updateSession.children(it).withIDs() } ?: emptyList() }
val actions = allActions.mapNotNullConcurrent { actionWithID ->
runCatching {
val mode = model.actionMatches(pattern, matcher, actionWithID.action)
if (mode != MatchMode.NONE) {
val weight = calcElementWeight(actionWithID, pattern, weightMatcher)
return@runCatching(MatchedAction(actionWithID.action, mode, weight))
val extendedActions: Sequence<AnAction> = model.dataContext.getData(QuickActionProvider.KEY)?.getActions(true)?.asSequence() ?: emptySequence<AnAction>()
val allActions: Sequence<AnAction> = mainActions + extendedActions + extendedActions.flatMap { (it as? ActionGroup)?.let { model.updateSession.children(it) } ?: emptyList() }
val matchedActions = produce(capacity = Channel.UNLIMITED) {
allActions.forEach { action ->
launch {
runCatching {
val mode = model.actionMatches(pattern, matcher, action)
if (mode != MatchMode.NONE) {
val weight = calcElementWeight(action, pattern, weightMatcher)
send(MatchedAction(action, mode, weight))
}
else {
if (action is ActionStubBase) actionManager.getId(action)?.let { unmatchedIdsChannel.send(it) }
}
}.getOrLogException(LOG)
}
else {
if (actionWithID.action is ActionStubBase) actionWithID.id?.let { unmatchedIdsChannel.send(it) }
return@runCatching null
}
}.getOrLogException(LOG)
}
}
}.toList()
unmatchedIdsChannel.close()
val comparator = Comparator.comparing<MatchedAction, Int> { it.weight ?: 0 }.reversed()
return@coroutineScope actions.sortedWith(comparator)
return@coroutineScope matchedActions.sortedWith(comparator)
}
private fun CoroutineScope.processUnmatchedStubs(nonMatchedIds: ReceiveChannel<String>,