[AA] Extract SublistMerger to analysis-api-providers utils
- This utility is useful for other places where sublists need to be merged.
This commit is contained in:
committed by
Space Team
parent
2540343159
commit
52f025f39b
+47
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.analysis.providers.impl.util
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.utils.addIfNotNull
|
||||||
|
import org.jetbrains.kotlin.utils.addToStdlib.partitionIsInstance
|
||||||
|
|
||||||
|
/**
|
||||||
|
* With each call to [merge], [SublistMerger] can merge all elements of a specific (reified) type into a single element using a supplied
|
||||||
|
* constructor and then add it to [destination]. Unmerged elements are added to [destination] using [finish].
|
||||||
|
*
|
||||||
|
* The purpose of [SublistMerger] is to merge multiple different types of elements from a single origin list without the need for
|
||||||
|
* intermediate list management and [partitionIsInstance] boilerplate.
|
||||||
|
*/
|
||||||
|
public class SublistMerger<A : Any>(
|
||||||
|
initialElements: List<A>,
|
||||||
|
public val destination: MutableList<A>,
|
||||||
|
) {
|
||||||
|
public var remainingElements: List<A> = initialElements
|
||||||
|
|
||||||
|
public inline fun <reified R : A> merge(create: (List<R>) -> A?) {
|
||||||
|
val (specificElements, remainingElements) = this.remainingElements.partitionIsInstance<_, R>()
|
||||||
|
destination.addIfNotNull(create(specificElements))
|
||||||
|
this.remainingElements = remainingElements
|
||||||
|
}
|
||||||
|
|
||||||
|
public fun finish() {
|
||||||
|
destination.addAll(remainingElements)
|
||||||
|
remainingElements = emptyList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public fun <A : Any> List<A>.mergeInto(destination: MutableList<A>, f: SublistMerger<A>.() -> Unit) {
|
||||||
|
SublistMerger(this, destination).apply {
|
||||||
|
f()
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public fun <A : Any> List<A>.mergeWith(f: SublistMerger<A>.() -> Unit): List<A> =
|
||||||
|
mutableListOf<A>().also { destination -> mergeInto(destination, f) }
|
||||||
|
|
||||||
|
public inline fun <A : Any, reified R : A> List<A>.mergeOnly(crossinline create: (List<R>) -> A?): List<A> =
|
||||||
|
mergeWith { merge<R>(create) }
|
||||||
+2
-22
@@ -46,10 +46,9 @@ import org.jetbrains.kotlin.psi.KtFile
|
|||||||
import org.jetbrains.kotlin.resolve.jvm.modules.JavaModuleResolver
|
import org.jetbrains.kotlin.resolve.jvm.modules.JavaModuleResolver
|
||||||
import org.jetbrains.kotlin.scripting.compiler.plugin.FirScriptingSamWithReceiverExtensionRegistrar
|
import org.jetbrains.kotlin.scripting.compiler.plugin.FirScriptingSamWithReceiverExtensionRegistrar
|
||||||
import org.jetbrains.kotlin.scripting.definitions.findScriptDefinition
|
import org.jetbrains.kotlin.scripting.definitions.findScriptDefinition
|
||||||
import org.jetbrains.kotlin.utils.addIfNotNull
|
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.partitionIsInstance
|
|
||||||
import kotlin.script.experimental.host.ScriptingHostConfiguration
|
import kotlin.script.experimental.host.ScriptingHostConfiguration
|
||||||
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
|
import kotlin.script.experimental.jvm.defaultJvmScriptingHostConfiguration
|
||||||
|
import org.jetbrains.kotlin.analysis.providers.impl.util.mergeInto
|
||||||
|
|
||||||
@OptIn(PrivateSessionConstructor::class, SessionConfiguration::class)
|
@OptIn(PrivateSessionConstructor::class, SessionConfiguration::class)
|
||||||
internal abstract class LLFirAbstractSessionFactory(protected val project: Project) {
|
internal abstract class LLFirAbstractSessionFactory(protected val project: Project) {
|
||||||
@@ -503,29 +502,10 @@ internal abstract class LLFirAbstractSessionFactory(protected val project: Proje
|
|||||||
session: LLFirSession,
|
session: LLFirSession,
|
||||||
destination: MutableList<FirSymbolProvider>,
|
destination: MutableList<FirSymbolProvider>,
|
||||||
) {
|
) {
|
||||||
SymbolProviderMerger(this, destination).apply {
|
mergeInto(destination) {
|
||||||
merge<LLFirKotlinSymbolProvider> { LLFirCombinedKotlinSymbolProvider.merge(session, project, it) }
|
merge<LLFirKotlinSymbolProvider> { LLFirCombinedKotlinSymbolProvider.merge(session, project, it) }
|
||||||
merge<JavaSymbolProvider> { LLFirCombinedJavaSymbolProvider.merge(session, project, it) }
|
merge<JavaSymbolProvider> { LLFirCombinedJavaSymbolProvider.merge(session, project, it) }
|
||||||
merge<FirExtensionSyntheticFunctionInterfaceProvider> { LLFirCombinedSyntheticFunctionSymbolProvider.merge(session, it) }
|
merge<FirExtensionSyntheticFunctionInterfaceProvider> { LLFirCombinedSyntheticFunctionSymbolProvider.merge(session, it) }
|
||||||
finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private class SymbolProviderMerger(
|
|
||||||
symbolProviders: List<FirSymbolProvider>,
|
|
||||||
private val destination: MutableList<FirSymbolProvider>
|
|
||||||
) {
|
|
||||||
private var remainingSymbolProviders = symbolProviders
|
|
||||||
|
|
||||||
inline fun <reified A : FirSymbolProvider> merge(create: (List<A>) -> FirSymbolProvider?) {
|
|
||||||
val (specificSymbolProviders, remainingSymbolProviders) = remainingSymbolProviders.partitionIsInstance<_, A>()
|
|
||||||
destination.addIfNotNull(create(specificSymbolProviders))
|
|
||||||
this.remainingSymbolProviders = remainingSymbolProviders
|
|
||||||
}
|
|
||||||
|
|
||||||
fun finish() {
|
|
||||||
destination.addAll(remainingSymbolProviders)
|
|
||||||
remainingSymbolProviders = emptyList()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user