AA: populate index in factory, not provider
This commit is contained in:
committed by
Ilya Kirillov
parent
5b631bff4d
commit
6797ac7cf2
+97
-72
@@ -16,22 +16,104 @@ import org.jetbrains.kotlin.name.FqName
|
|||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
|
||||||
public class KotlinStaticDeclarationProvider(
|
public class KotlinStaticDeclarationIndex {
|
||||||
scope: GlobalSearchScope,
|
internal val facadeFileMap: MutableMap<FqName, MutableSet<KtFile>> = mutableMapOf()
|
||||||
ktFiles: Collection<KtFile>
|
internal val classMap: MutableMap<FqName, MutableSet<KtClassOrObject>> = mutableMapOf()
|
||||||
|
internal val typeAliasMap: MutableMap<FqName, MutableSet<KtTypeAlias>> = mutableMapOf()
|
||||||
|
internal val topLevelFunctionMap: MutableMap<FqName, MutableSet<KtNamedFunction>> = mutableMapOf()
|
||||||
|
internal val topLevelPropertyMap: MutableMap<FqName, MutableSet<KtProperty>> = mutableMapOf()
|
||||||
|
}
|
||||||
|
|
||||||
|
public class KotlinStaticDeclarationProvider internal constructor(
|
||||||
|
private val index: KotlinStaticDeclarationIndex,
|
||||||
|
private val scope: GlobalSearchScope,
|
||||||
) : KotlinDeclarationProvider() {
|
) : KotlinDeclarationProvider() {
|
||||||
|
|
||||||
private val index = Index()
|
private val KtElement.inScope: Boolean
|
||||||
|
get() = containingKtFile.virtualFile in scope
|
||||||
|
|
||||||
private class Index {
|
override fun getClassesByClassId(classId: ClassId): Collection<KtClassOrObject> =
|
||||||
val facadeFileMap: MutableMap<FqName, MutableSet<KtFile>> = mutableMapOf()
|
index.classMap[classId.packageFqName]
|
||||||
val classMap: MutableMap<FqName, MutableSet<KtClassOrObject>> = mutableMapOf()
|
?.filter { ktClassOrObject ->
|
||||||
val typeAliasMap: MutableMap<FqName, MutableSet<KtTypeAlias>> = mutableMapOf()
|
ktClassOrObject.getClassId() == classId && ktClassOrObject.inScope
|
||||||
val topLevelFunctionMap: MutableMap<FqName, MutableSet<KtNamedFunction>> = mutableMapOf()
|
}
|
||||||
val topLevelPropertyMap: MutableMap<FqName, MutableSet<KtProperty>> = mutableMapOf()
|
?: emptyList()
|
||||||
|
|
||||||
|
override fun getTypeAliasesByClassId(classId: ClassId): Collection<KtTypeAlias> =
|
||||||
|
index.typeAliasMap[classId.packageFqName]
|
||||||
|
?.filter { ktTypeAlias ->
|
||||||
|
ktTypeAlias.getClassId() == classId && ktTypeAlias.inScope
|
||||||
|
}
|
||||||
|
?: 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 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 getFacadeFilesInPackage(packageFqName: FqName): Collection<KtFile> =
|
||||||
|
index.facadeFileMap[packageFqName]
|
||||||
|
?.filter { ktFile ->
|
||||||
|
ktFile.virtualFile in scope
|
||||||
|
}
|
||||||
|
?: emptyList()
|
||||||
|
|
||||||
|
override fun findFilesForFacade(facadeFqName: FqName): Collection<KtFile> {
|
||||||
|
if (facadeFqName.shortNameOrSpecial().isSpecial) return emptyList()
|
||||||
|
return getFacadeFilesInPackage(facadeFqName.parent()) //TODO Not work correctly for classes with JvmPackageName
|
||||||
|
.filter { it.javaFileFacadeFqName == facadeFqName }
|
||||||
}
|
}
|
||||||
|
|
||||||
private class KtDeclarationRecorder(private val index: Index) : KtVisitorVoid() {
|
override fun getTopLevelProperties(callableId: CallableId): Collection<KtProperty> =
|
||||||
|
index.topLevelPropertyMap[callableId.packageName]
|
||||||
|
?.filter { ktProperty ->
|
||||||
|
ktProperty.nameAsName == callableId.callableName && ktProperty.inScope
|
||||||
|
}
|
||||||
|
?: emptyList()
|
||||||
|
|
||||||
|
override fun getTopLevelFunctions(callableId: CallableId): Collection<KtNamedFunction> =
|
||||||
|
index.topLevelFunctionMap[callableId.packageName]
|
||||||
|
?.filter { ktNamedFunction ->
|
||||||
|
ktNamedFunction.nameAsName == callableId.callableName && ktNamedFunction.inScope
|
||||||
|
}
|
||||||
|
?: emptyList()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class KotlinStaticDeclarationProviderFactory(
|
||||||
|
files: Collection<KtFile>
|
||||||
|
) : KotlinDeclarationProviderFactory() {
|
||||||
|
|
||||||
|
private val index = KotlinStaticDeclarationIndex()
|
||||||
|
|
||||||
|
private class KtDeclarationRecorder(private val index: KotlinStaticDeclarationIndex) : KtVisitorVoid() {
|
||||||
|
|
||||||
override fun visitElement(element: PsiElement) {
|
override fun visitElement(element: PsiElement) {
|
||||||
element.acceptChildren(this)
|
element.acceptChildren(this)
|
||||||
@@ -87,69 +169,12 @@ public class KotlinStaticDeclarationProvider(
|
|||||||
|
|
||||||
init {
|
init {
|
||||||
val recorder = KtDeclarationRecorder(index)
|
val recorder = KtDeclarationRecorder(index)
|
||||||
ktFiles
|
files.forEach {
|
||||||
.filter {
|
it.accept(recorder)
|
||||||
scope.contains(it.virtualFile)
|
}
|
||||||
}
|
|
||||||
.forEach {
|
|
||||||
it.accept(recorder)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getClassesByClassId(classId: ClassId): Collection<KtClassOrObject> =
|
|
||||||
index.classMap[classId.packageFqName]
|
|
||||||
?.filter { it.getClassId() == classId }
|
|
||||||
?: emptyList()
|
|
||||||
|
|
||||||
override fun getTypeAliasesByClassId(classId: ClassId): Collection<KtTypeAlias> =
|
|
||||||
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> =
|
|
||||||
index.typeAliasMap[packageFqName]
|
|
||||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
|
||||||
?: emptySet()
|
|
||||||
|
|
||||||
override fun getPropertyNamesInPackage(packageFqName: FqName): Set<Name> =
|
|
||||||
index.topLevelPropertyMap[packageFqName]
|
|
||||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
|
||||||
?: emptySet()
|
|
||||||
|
|
||||||
override fun getFunctionsNamesInPackage(packageFqName: FqName): Set<Name> =
|
|
||||||
index.topLevelFunctionMap[packageFqName]
|
|
||||||
?.mapNotNullTo(mutableSetOf()) { it.nameAsName }
|
|
||||||
?: emptySet()
|
|
||||||
|
|
||||||
override fun getFacadeFilesInPackage(packageFqName: FqName): Collection<KtFile> =
|
|
||||||
index.facadeFileMap[packageFqName]
|
|
||||||
?: emptyList()
|
|
||||||
|
|
||||||
override fun findFilesForFacade(facadeFqName: FqName): Collection<KtFile> {
|
|
||||||
if (facadeFqName.shortNameOrSpecial().isSpecial) return emptyList()
|
|
||||||
return getFacadeFilesInPackage(facadeFqName.parent()) //TODO Not work correctly for classes with JvmPackageName
|
|
||||||
.filter { it.javaFileFacadeFqName == facadeFqName }
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getTopLevelProperties(callableId: CallableId): Collection<KtProperty> =
|
|
||||||
index.topLevelPropertyMap[callableId.packageName]
|
|
||||||
?.filter { it.nameAsName == callableId.callableName }
|
|
||||||
?: emptyList()
|
|
||||||
|
|
||||||
override fun getTopLevelFunctions(callableId: CallableId): Collection<KtNamedFunction> =
|
|
||||||
index.topLevelFunctionMap[callableId.packageName]
|
|
||||||
?.filter { it.nameAsName == callableId.callableName }
|
|
||||||
?: emptyList()
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class KotlinStaticDeclarationProviderFactory(private val files: Collection<KtFile>) : KotlinDeclarationProviderFactory() {
|
|
||||||
override fun createDeclarationProvider(searchScope: GlobalSearchScope): KotlinDeclarationProvider {
|
override fun createDeclarationProvider(searchScope: GlobalSearchScope): KotlinDeclarationProvider {
|
||||||
return KotlinStaticDeclarationProvider(searchScope, files)
|
return KotlinStaticDeclarationProvider(index, searchScope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user