AA: populate index for declarations upfront
This commit is contained in:
committed by
Ilya Kirillov
parent
6a9ed82fdc
commit
5b631bff4d
+104
-47
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.analysis.providers.impl
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProvider
|
||||
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProviderFactory
|
||||
@@ -14,54 +15,120 @@ import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
|
||||
|
||||
public class KotlinStaticDeclarationProvider(
|
||||
scope: GlobalSearchScope,
|
||||
ktFiles: Collection<KtFile>
|
||||
) : KotlinDeclarationProvider() {
|
||||
private val filesInScope = ktFiles.filter { scope.contains(it.virtualFile) }
|
||||
|
||||
private fun filesByPackage(packageFqName: FqName) =
|
||||
filesInScope.asSequence()
|
||||
.filter { it.packageFqName == packageFqName }
|
||||
private val index = Index()
|
||||
|
||||
private class Index {
|
||||
val facadeFileMap: MutableMap<FqName, MutableSet<KtFile>> = mutableMapOf()
|
||||
val classMap: MutableMap<FqName, MutableSet<KtClassOrObject>> = mutableMapOf()
|
||||
val typeAliasMap: MutableMap<FqName, MutableSet<KtTypeAlias>> = mutableMapOf()
|
||||
val topLevelFunctionMap: MutableMap<FqName, MutableSet<KtNamedFunction>> = mutableMapOf()
|
||||
val topLevelPropertyMap: MutableMap<FqName, MutableSet<KtProperty>> = mutableMapOf()
|
||||
}
|
||||
|
||||
private class KtDeclarationRecorder(private val index: Index) : KtVisitorVoid() {
|
||||
|
||||
override fun visitElement(element: PsiElement) {
|
||||
element.acceptChildren(this)
|
||||
}
|
||||
|
||||
override fun visitKtFile(file: KtFile) {
|
||||
if (file.hasTopLevelCallables()) {
|
||||
index.facadeFileMap.computeIfAbsent(file.packageFqName) {
|
||||
mutableSetOf()
|
||||
}.add(file)
|
||||
}
|
||||
super.visitKtFile(file)
|
||||
}
|
||||
|
||||
override fun visitClassOrObject(classOrObject: KtClassOrObject) {
|
||||
classOrObject.getClassId()?.let { classId ->
|
||||
index.classMap.computeIfAbsent(classId.packageFqName) {
|
||||
mutableSetOf()
|
||||
}.add(classOrObject)
|
||||
}
|
||||
super.visitClassOrObject(classOrObject)
|
||||
}
|
||||
|
||||
override fun visitTypeAlias(typeAlias: KtTypeAlias) {
|
||||
typeAlias.getClassId()?.let { classId ->
|
||||
index.typeAliasMap.computeIfAbsent(classId.packageFqName) {
|
||||
mutableSetOf()
|
||||
}.add(typeAlias)
|
||||
}
|
||||
super.visitTypeAlias(typeAlias)
|
||||
}
|
||||
|
||||
override fun visitNamedFunction(function: KtNamedFunction) {
|
||||
if (function.isTopLevel) {
|
||||
val packageFqName = (function.parent as KtFile).packageFqName
|
||||
index.topLevelFunctionMap.computeIfAbsent(packageFqName) {
|
||||
mutableSetOf()
|
||||
}.add(function)
|
||||
}
|
||||
super.visitNamedFunction(function)
|
||||
}
|
||||
|
||||
override fun visitProperty(property: KtProperty) {
|
||||
if (property.isTopLevel) {
|
||||
val packageFqName = (property.parent as KtFile).packageFqName
|
||||
index.topLevelPropertyMap.computeIfAbsent(packageFqName) {
|
||||
mutableSetOf()
|
||||
}.add(property)
|
||||
}
|
||||
super.visitProperty(property)
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
val recorder = KtDeclarationRecorder(index)
|
||||
ktFiles
|
||||
.filter {
|
||||
scope.contains(it.virtualFile)
|
||||
}
|
||||
.forEach {
|
||||
it.accept(recorder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getClassesByClassId(classId: ClassId): Collection<KtClassOrObject> =
|
||||
filesByPackage(classId.packageFqName).flatMap { file ->
|
||||
file.collectDescendantsOfType<KtClassOrObject> { ktClass ->
|
||||
ktClass.getClassId() == classId
|
||||
}
|
||||
}.toList()
|
||||
index.classMap[classId.packageFqName]
|
||||
?.filter { it.getClassId() == classId }
|
||||
?: emptyList()
|
||||
|
||||
override fun getTypeAliasesByClassId(classId: ClassId): Collection<KtTypeAlias> =
|
||||
filesByPackage(classId.packageFqName).flatMap { file ->
|
||||
file.collectDescendantsOfType<KtTypeAlias> { typeAlias ->
|
||||
typeAlias.getClassId() == classId
|
||||
}
|
||||
}.toList()
|
||||
index.typeAliasMap[classId.packageFqName]
|
||||
?.filter { it.getClassId() == classId }
|
||||
?: emptyList()
|
||||
|
||||
override fun getClassNamesInPackage(packageFqName: FqName): Set<Name> =
|
||||
index.classMap[packageFqName]
|
||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
?: emptySet()
|
||||
|
||||
override fun getTypeAliasNamesInPackage(packageFqName: FqName): Set<Name> =
|
||||
filesByPackage(packageFqName)
|
||||
.flatMap { it.declarations }
|
||||
.filterIsInstance<KtTypeAlias>()
|
||||
.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
index.typeAliasMap[packageFqName]
|
||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
?: emptySet()
|
||||
|
||||
override fun getPropertyNamesInPackage(packageFqName: FqName): Set<Name> =
|
||||
filesByPackage(packageFqName)
|
||||
.flatMap { it.declarations }
|
||||
.filterIsInstance<KtProperty>()
|
||||
.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
index.topLevelPropertyMap[packageFqName]
|
||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
?: emptySet()
|
||||
|
||||
override fun getFunctionsNamesInPackage(packageFqName: FqName): Set<Name> =
|
||||
filesByPackage(packageFqName)
|
||||
.flatMap { it.declarations }
|
||||
.filterIsInstance<KtNamedFunction>()
|
||||
.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
index.topLevelFunctionMap[packageFqName]
|
||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
?: emptySet()
|
||||
|
||||
override fun getFacadeFilesInPackage(packageFqName: FqName): Collection<KtFile> =
|
||||
filesByPackage(packageFqName)
|
||||
.filter { file -> file.hasTopLevelCallables() }
|
||||
.toSet()
|
||||
index.facadeFileMap[packageFqName]
|
||||
?: emptyList()
|
||||
|
||||
override fun findFilesForFacade(facadeFqName: FqName): Collection<KtFile> {
|
||||
if (facadeFqName.shortNameOrSpecial().isSpecial) return emptyList()
|
||||
@@ -70,29 +137,19 @@ public class KotlinStaticDeclarationProvider(
|
||||
}
|
||||
|
||||
override fun getTopLevelProperties(callableId: CallableId): Collection<KtProperty> =
|
||||
filesByPackage(callableId.packageName)
|
||||
.flatMap { it.declarations }
|
||||
.filterIsInstance<KtProperty>()
|
||||
.filter { it.nameAsName == callableId.callableName }
|
||||
.toList()
|
||||
index.topLevelPropertyMap[callableId.packageName]
|
||||
?.filter { it.nameAsName == callableId.callableName }
|
||||
?: emptyList()
|
||||
|
||||
override fun getTopLevelFunctions(callableId: CallableId): Collection<KtNamedFunction> =
|
||||
filesByPackage(callableId.packageName)
|
||||
.flatMap { it.declarations }
|
||||
.filterIsInstance<KtNamedFunction>()
|
||||
.filter { it.nameAsName == callableId.callableName }
|
||||
.toList()
|
||||
index.topLevelFunctionMap[callableId.packageName]
|
||||
?.filter { it.nameAsName == callableId.callableName }
|
||||
?: emptyList()
|
||||
|
||||
|
||||
override fun getClassNamesInPackage(packageFqName: FqName): Set<Name> =
|
||||
filesByPackage(packageFqName)
|
||||
.flatMap { it.declarations }
|
||||
.filterIsInstance<KtClassOrObject>()
|
||||
.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
||||
}
|
||||
|
||||
public class KotlinStaticDeclarationProviderFactory(private val files: Collection<KtFile>) : KotlinDeclarationProviderFactory() {
|
||||
override fun createDeclarationProvider(searchScope: GlobalSearchScope): KotlinDeclarationProvider {
|
||||
return KotlinStaticDeclarationProvider(searchScope, files)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user