[Analysis API] simplify KotlinDeclarationProvider interface
This commit is contained in:
+3
-2
@@ -6,6 +6,7 @@
|
||||
package org.jetbrains.kotlin.analysis.api.fir
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals
|
||||
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
|
||||
import org.jetbrains.kotlin.analysis.api.components.*
|
||||
@@ -140,8 +141,8 @@ private constructor(
|
||||
internal val firSymbolProvider: FirSymbolProvider get() = useSiteSession.symbolProvider
|
||||
internal val targetPlatform: TargetPlatform get() = useSiteSession.moduleData.platform
|
||||
|
||||
internal val useSiteScopeDeclarationProvider: KotlinDeclarationProvider =
|
||||
project.createDeclarationProvider(analysisScopeProviderImpl.getAnalysisScope())
|
||||
val useSiteAnalisisScope: GlobalSearchScope = analysisScopeProviderImpl.getAnalysisScope()
|
||||
val useSiteScopeDeclarationProvider: KotlinDeclarationProvider = project.createDeclarationProvider(useSiteAnalisisScope)
|
||||
|
||||
fun getScopeSessionFor(session: FirSession): ScopeSession = withValidityAssertion { firResolveSession.getScopeSessionFor(session) }
|
||||
|
||||
|
||||
+15
-16
@@ -46,6 +46,7 @@ import org.jetbrains.kotlin.fir.scopes.impl.*
|
||||
import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope
|
||||
import org.jetbrains.kotlin.fir.symbols.ensureResolved
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.*
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
@@ -159,14 +160,7 @@ internal class KtFirScopeProvider(
|
||||
|
||||
override fun getPackageScope(packageSymbol: KtPackageSymbol): KtScope {
|
||||
return packageMemberScopeCache.getOrPut(packageSymbol) {
|
||||
KtFirPackageScope(
|
||||
packageSymbol.fqName,
|
||||
project,
|
||||
builder,
|
||||
token,
|
||||
GlobalSearchScope.allScope(project), // TODO
|
||||
analysisSession.targetPlatform,
|
||||
)
|
||||
createPackageScope(packageSymbol.fqName)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,19 +226,24 @@ internal class KtFirScopeProvider(
|
||||
return when (firScope) {
|
||||
is FirAbstractSimpleImportingScope -> KtFirNonStarImportingScope(firScope, builder, token)
|
||||
is FirAbstractStarImportingScope -> KtFirStarImportingScope(firScope, builder, analysisSession.useSiteScopeDeclarationProvider, token)
|
||||
is FirPackageMemberScope -> KtFirPackageScope(
|
||||
firScope.fqName,
|
||||
project,
|
||||
builder,
|
||||
token,
|
||||
GlobalSearchScope.allScope(project), // todo
|
||||
analysisSession.targetPlatform
|
||||
)
|
||||
is FirPackageMemberScope -> createPackageScope(firScope.fqName)
|
||||
is FirContainingNamesAwareScope -> KtFirDelegatingScope(firScope, builder, token)
|
||||
else -> TODO(firScope::class.toString())
|
||||
}
|
||||
}
|
||||
|
||||
private fun createPackageScope(fqName: FqName): KtFirPackageScope {
|
||||
return KtFirPackageScope(
|
||||
fqName,
|
||||
project,
|
||||
builder,
|
||||
token,
|
||||
analysisSession.useSiteAnalisisScope,
|
||||
analysisSession.useSiteScopeDeclarationProvider,
|
||||
analysisSession.targetPlatform
|
||||
)
|
||||
}
|
||||
|
||||
private fun convertToKtTypeScope(firScope: FirScope): KtTypeScope {
|
||||
return when (firScope) {
|
||||
is FirContainingNamesAwareScope -> KtFirDelegatingTypeScope(firScope, builder, token)
|
||||
|
||||
+4
-7
@@ -17,7 +17,7 @@ import org.jetbrains.kotlin.analysis.api.symbols.KtConstructorSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KtPackageSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeToken
|
||||
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
|
||||
import org.jetbrains.kotlin.analysis.providers.createDeclarationProvider
|
||||
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProvider
|
||||
import org.jetbrains.kotlin.analysis.providers.createPackageProvider
|
||||
import org.jetbrains.kotlin.fir.extensions.FirExtensionService
|
||||
import org.jetbrains.kotlin.fir.extensions.declarationGenerators
|
||||
@@ -34,9 +34,9 @@ internal class KtFirPackageScope(
|
||||
private val builder: KtSymbolByFirBuilder,
|
||||
override val token: KtLifetimeToken,
|
||||
private val searchScope: GlobalSearchScope,
|
||||
private val declarationProvider: KotlinDeclarationProvider,
|
||||
private val targetPlatform: TargetPlatform,
|
||||
) : KtScope {
|
||||
private val declarationsProvider = project.createDeclarationProvider(searchScope)
|
||||
private val packageProvider = project.createPackageProvider(searchScope)
|
||||
|
||||
private val firScope: FirPackageMemberScope by lazy(LazyThreadSafetyMode.PUBLICATION) {
|
||||
@@ -48,17 +48,14 @@ internal class KtFirPackageScope(
|
||||
|
||||
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
|
||||
hashSetOf<Name>().apply {
|
||||
addAll(declarationsProvider.getFunctionsNamesInPackage(fqName))
|
||||
addAll(declarationsProvider.getPropertyNamesInPackage(fqName))
|
||||
|
||||
addAll(declarationProvider.getTopLevelCallableNamesInPackage(fqName))
|
||||
addAll(collectGeneratedTopLevelCallables())
|
||||
}
|
||||
}
|
||||
|
||||
override fun getPossibleClassifierNames(): Set<Name> = withValidityAssertion {
|
||||
hashSetOf<Name>().apply {
|
||||
addAll(declarationsProvider.getClassNamesInPackage(fqName))
|
||||
addAll(declarationsProvider.getTypeAliasNamesInPackage(fqName))
|
||||
addAll(declarationProvider.getTopLevelKotlinClassLikeDeclarationNamesInPackage(fqName))
|
||||
|
||||
JavaPsiFacade.getInstance(project)
|
||||
.findPackage(fqName.asString())
|
||||
|
||||
+2
-4
@@ -51,8 +51,7 @@ internal class KtFirStarImportingScope(
|
||||
override fun getPossibleCallableNames(): Set<Name> = withValidityAssertion {
|
||||
imports.flatMapTo(hashSetOf()) { import: Import ->
|
||||
if (import.relativeClassName == null) { // top level callable
|
||||
declarationProvider.getFunctionsNamesInPackage(import.packageFqName) +
|
||||
declarationProvider.getPropertyNamesInPackage(import.packageFqName)
|
||||
declarationProvider.getTopLevelCallableNamesInPackage(import.packageFqName)
|
||||
} else { //member
|
||||
val classId = import.resolvedClassId ?: error("Class id should not be null as relativeClassName is not null")
|
||||
firScope.getStaticsScope(classId)?.getCallableNames().orEmpty()
|
||||
@@ -69,8 +68,7 @@ internal class KtFirStarImportingScope(
|
||||
override fun getPossibleClassifierNames(): Set<Name> = withValidityAssertion {
|
||||
imports.flatMapTo(hashSetOf()) { import ->
|
||||
if (import.relativeClassName == null) {
|
||||
declarationProvider.getClassNamesInPackage(import.packageFqName) +
|
||||
declarationProvider.getTypeAliasNamesInPackage(import.packageFqName)
|
||||
declarationProvider.getTopLevelKotlinClassLikeDeclarationNamesInPackage(import.packageFqName)
|
||||
} else {
|
||||
val classId = import.resolvedClassId ?: error("Class id should not be null as relativeClassName is not null")
|
||||
firScope.getStaticsScope(classId)?.getClassifierNames().orEmpty()
|
||||
|
||||
+3
-4
@@ -24,14 +24,13 @@ public abstract class KotlinDeclarationProvider {
|
||||
public abstract fun getAllClassesByClassId(classId: ClassId): Collection<KtClassOrObject>
|
||||
public abstract fun getAllTypeAliasesByClassId(classId: ClassId): Collection<KtTypeAlias>
|
||||
|
||||
public abstract fun getClassNamesInPackage(packageFqName: FqName): Set<Name>
|
||||
public abstract fun getTypeAliasNamesInPackage(packageFqName: FqName): Set<Name>
|
||||
public abstract fun getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName: FqName): Set<Name>
|
||||
|
||||
public abstract fun getTopLevelProperties(callableId: CallableId): Collection<KtProperty>
|
||||
public abstract fun getTopLevelFunctions(callableId: CallableId): Collection<KtNamedFunction>
|
||||
|
||||
public abstract fun getPropertyNamesInPackage(packageFqName: FqName): Set<Name>
|
||||
public abstract fun getFunctionsNamesInPackage(packageFqName: FqName): Set<Name>
|
||||
public abstract fun getTopLevelCallableNamesInPackage(packageFqName: FqName): Set<Name>
|
||||
|
||||
public abstract fun getFacadeFilesInPackage(packageFqName: FqName): Collection<KtFile>
|
||||
public abstract fun findFilesForFacade(facadeFqName: FqName): Collection<KtFile>
|
||||
}
|
||||
|
||||
+12
-28
@@ -51,37 +51,21 @@ public class KotlinStaticDeclarationProvider internal constructor(
|
||||
}
|
||||
?: emptyList()
|
||||
|
||||
override fun getClassNamesInPackage(packageFqName: FqName): Set<Name> =
|
||||
index.classMap[packageFqName]
|
||||
?.filter { ktClassOrObject ->
|
||||
ktClassOrObject.inScope
|
||||
}
|
||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
?: emptySet()
|
||||
|
||||
override fun getTypeAliasNamesInPackage(packageFqName: FqName): Set<Name> =
|
||||
index.typeAliasMap[packageFqName]
|
||||
?.filter { ktTypeAlias ->
|
||||
ktTypeAlias.inScope
|
||||
}
|
||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
?: emptySet()
|
||||
override fun getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName: FqName): Set<Name> {
|
||||
val classifiers = index.classMap[packageFqName].orEmpty() + index.typeAliasMap[packageFqName].orEmpty()
|
||||
return classifiers.filter { it.inScope }
|
||||
.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
}
|
||||
|
||||
override fun getPropertyNamesInPackage(packageFqName: FqName): Set<Name> =
|
||||
index.topLevelPropertyMap[packageFqName]
|
||||
?.filter { ktProperty ->
|
||||
ktProperty.inScope
|
||||
}
|
||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
?: emptySet()
|
||||
|
||||
override fun getFunctionsNamesInPackage(packageFqName: FqName): Set<Name> =
|
||||
index.topLevelFunctionMap[packageFqName]
|
||||
?.filter { ktNamedFunction ->
|
||||
ktNamedFunction.inScope
|
||||
}
|
||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
?: emptySet()
|
||||
override fun getTopLevelCallableNamesInPackage(packageFqName: FqName): Set<Name> {
|
||||
val callables = index.topLevelPropertyMap[packageFqName].orEmpty() + index.topLevelFunctionMap[packageFqName].orEmpty()
|
||||
return callables
|
||||
.filter { it.inScope }
|
||||
.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
}
|
||||
|
||||
|
||||
override fun getFacadeFilesInPackage(packageFqName: FqName): Collection<KtFile> =
|
||||
index.facadeFileMap[packageFqName]
|
||||
|
||||
+1
-2
@@ -6,7 +6,6 @@
|
||||
package org.jetbrains.kotlin.analysis.low.level.api.fir.providers
|
||||
|
||||
import org.jetbrains.kotlin.analysis.low.level.api.fir.LLFirModuleResolveComponents
|
||||
import org.jetbrains.kotlin.analysis.low.level.api.fir.file.builder.ModuleFileCache
|
||||
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProvider
|
||||
import org.jetbrains.kotlin.analysis.providers.KotlinPackageProvider
|
||||
import org.jetbrains.kotlin.fir.FirSession
|
||||
@@ -80,7 +79,7 @@ internal class LLFirProvider(
|
||||
|
||||
|
||||
override fun getClassNamesInPackage(fqName: FqName): Set<Name> =
|
||||
declarationProvider.getClassNamesInPackage(fqName)
|
||||
declarationProvider.getTopLevelKotlinClassLikeDeclarationNamesInPackage(fqName)
|
||||
|
||||
@NoMutableState
|
||||
private inner class SymbolProvider : FirSymbolProvider(session) {
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ class KotlinAsJavaFirSupport(private val project: Project) : KotlinAsJavaSupport
|
||||
packageFqName: FqName,
|
||||
searchScope: GlobalSearchScope
|
||||
): Collection<KtClassOrObject> = project.createDeclarationProvider(searchScope).run {
|
||||
getClassNamesInPackage(packageFqName).flatMap {
|
||||
getTopLevelKotlinClassLikeDeclarationNamesInPackage(packageFqName).flatMap {
|
||||
getAllClassesByClassId(ClassId.topLevel(packageFqName.child(it)))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user