diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/project/structure/CompositeKotlinDeclarationProvider.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/project/structure/CompositeKotlinDeclarationProvider.kt new file mode 100644 index 00000000000..aa17fcd390a --- /dev/null +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/project/structure/CompositeKotlinDeclarationProvider.kt @@ -0,0 +1,73 @@ +/* + * 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.low.level.api.fir.project.structure + +import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProvider +import org.jetbrains.kotlin.name.CallableId +import org.jetbrains.kotlin.name.ClassId +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.psi.* + +internal class CompositeKotlinDeclarationProvider +private constructor( + private val providers: List +) : KotlinDeclarationProvider() { + + override fun getClassLikeDeclarationByClassId(classId: ClassId): KtClassLikeDeclaration? { + return providers.firstNotNullOfOrNull { it.getClassLikeDeclarationByClassId(classId) } + } + + override fun getAllClassesByClassId(classId: ClassId): Collection { + return providers.flatMap { it.getAllClassesByClassId(classId) } + } + + override fun getAllTypeAliasesByClassId(classId: ClassId): Collection { + return providers.flatMap { it.getAllTypeAliasesByClassId(classId) } + } + + override fun getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName: FqName): Set { + return providers.flatMapTo(mutableSetOf()) { it.getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName) } + } + + override fun getTopLevelProperties(callableId: CallableId): Collection { + return providers.flatMapTo(mutableListOf()) { it.getTopLevelProperties(callableId) } + } + + override fun getTopLevelFunctions(callableId: CallableId): Collection { + return providers.flatMapTo(mutableListOf()) { it.getTopLevelFunctions(callableId) } + } + + override fun getTopLevelCallableFiles(callableId: CallableId): Collection { + return providers.flatMapTo(mutableListOf()) { it.getTopLevelCallableFiles(callableId) } + } + + override fun getTopLevelCallableNamesInPackage(packageFqName: FqName): Set { + return providers.flatMapTo(mutableSetOf()) { it.getTopLevelCallableNamesInPackage(packageFqName) } + } + + override fun findFilesForFacadeByPackage(packageFqName: FqName): Collection { + return providers.flatMapTo(mutableListOf()) { it.findFilesForFacadeByPackage(packageFqName) } + } + + override fun findFilesForFacade(facadeFqName: FqName): Collection { + return providers.flatMapTo(mutableListOf()) { it.findFilesForFacade(facadeFqName) } + } + + override fun findInternalFilesForFacade(facadeFqName: FqName): Collection { + return providers.flatMapTo(mutableListOf()) { it.findInternalFilesForFacade(facadeFqName) } + } + + companion object { + fun create(providers: List): KotlinDeclarationProvider { + return when (providers.size) { + 0 -> EmptyKotlinDeclarationProvider + 1 -> providers.single() + else -> CompositeKotlinDeclarationProvider(providers) + } + } + } +}