FIR IDE: move declaration/package providers to separate module

This commit is contained in:
Ilya Kirillov
2021-08-20 16:36:57 +03:00
parent a54b769ec4
commit abe2311372
36 changed files with 211 additions and 173 deletions
@@ -0,0 +1,20 @@
plugins {
kotlin("jvm")
id("jps-compatible")
}
dependencies {
implementation(project(":compiler:psi"))
implementation(project(":compiler:frontend.java"))
implementation(project(":core:compiler.common"))
implementation(intellijCoreDep()) { includeJars("intellij-core", rootProject = rootProject) }
}
sourceSets {
"main" { projectDefault() }
"test" { none() }
}
kotlin {
explicitApi()
}
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2021 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
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
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.*
public abstract class KotlinDeclarationProvider {
public abstract fun getClassesByClassId(classId: ClassId): Collection<KtClassOrObject>
public abstract fun getTypeAliasesByClassId(classId: ClassId): Collection<KtTypeAlias>
public abstract fun getClassNamesInPackage(packageFqName: FqName): Set<Name>
public abstract fun getTypeAliasNamesInPackage(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 getFacadeFilesInPackage(packageFqName: FqName): Collection<KtFile>
public abstract fun findFilesForFacade(facadeFqName: FqName): Collection<KtFile>
}
public abstract class KotlinDeclarationProviderFactory {
public abstract fun createDeclarationProvider(searchScope: GlobalSearchScope): KotlinDeclarationProvider
}
public fun Project.createDeclarationProvider(searchScope: GlobalSearchScope): KotlinDeclarationProvider =
ServiceManager.getService(this, KotlinDeclarationProviderFactory::class.java)
.createDeclarationProvider(searchScope)
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2021 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
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.ModificationTracker
import org.jetbrains.annotations.TestOnly
import org.jetbrains.kotlin.analyzer.ModuleSourceInfoBase
public abstract class KotlinModificationTrackerFactory {
public abstract fun createProjectWideOutOfBlockModificationTracker(): ModificationTracker
public abstract fun createModuleWithoutDependenciesOutOfBlockModificationTracker(moduleInfo: ModuleSourceInfoBase): ModificationTracker
public abstract fun createLibrariesModificationTracker(): ModificationTracker
@TestOnly
public abstract fun incrementModificationsCount()
}
public fun Project.createProjectWideOutOfBlockModificationTracker(): ModificationTracker =
ServiceManager.getService(this, KotlinModificationTrackerFactory::class.java)
.createProjectWideOutOfBlockModificationTracker()
public fun Project.createLibrariesModificationTracker(): ModificationTracker =
ServiceManager.getService(this, KotlinModificationTrackerFactory::class.java)
.createLibrariesModificationTracker()
public fun ModuleSourceInfoBase.createModuleWithoutDependenciesOutOfBlockModificationTracker(project: Project): ModificationTracker =
ServiceManager.getService(project, KotlinModificationTrackerFactory::class.java)
.createModuleWithoutDependenciesOutOfBlockModificationTracker(this)
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2021 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
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
public abstract class KotlinPackageProvider {
public abstract fun isPackageExists(packageFqName: FqName): Boolean
public abstract fun getKotlinSubPackageFqNames(packageFqName: FqName): Set<Name>
}
public abstract class KotlinPackageProviderFactory {
public abstract fun createPackageProvider(searchScope: GlobalSearchScope): KotlinPackageProvider
}
public fun Project.createPackageProvider(searchScope: GlobalSearchScope): KotlinPackageProvider =
ServiceManager.getService(this, KotlinPackageProviderFactory::class.java)
.createPackageProvider(searchScope)
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2021 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
import com.intellij.openapi.util.ModificationTracker
import com.intellij.openapi.util.SimpleModificationTracker
import org.jetbrains.annotations.TestOnly
import org.jetbrains.kotlin.analysis.providers.KotlinModificationTrackerFactory
import org.jetbrains.kotlin.analyzer.ModuleSourceInfoBase
public class KotlinStaticModificationTrackerFactory : KotlinModificationTrackerFactory() {
private val projectWide = SimpleModificationTracker()
private val library = SimpleModificationTracker()
private val forModule = mutableMapOf<ModuleSourceInfoBase, SimpleModificationTracker>()
override fun createProjectWideOutOfBlockModificationTracker(): ModificationTracker {
return projectWide
}
override fun createModuleWithoutDependenciesOutOfBlockModificationTracker(moduleInfo: ModuleSourceInfoBase): ModificationTracker {
return forModule.getOrPut(moduleInfo) { SimpleModificationTracker() }
}
override fun createLibrariesModificationTracker(): ModificationTracker {
return library
}
@TestOnly
override fun incrementModificationsCount() {
projectWide.incModificationCount()
library.incModificationCount()
forModule.values.forEach { it.incModificationCount() }
}
}
@@ -0,0 +1,98 @@
/*
* Copyright 2010-2021 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
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProvider
import org.jetbrains.kotlin.analysis.providers.KotlinDeclarationProviderFactory
import org.jetbrains.kotlin.fileClasses.javaFileFacadeFqName
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.*
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 }
override fun getClassesByClassId(classId: ClassId): Collection<KtClassOrObject> =
filesByPackage(classId.packageFqName).flatMap { file ->
file.collectDescendantsOfType<KtClassOrObject> { ktClass ->
ktClass.getClassId() == classId
}
}.toList()
override fun getTypeAliasesByClassId(classId: ClassId): Collection<KtTypeAlias> =
filesByPackage(classId.packageFqName).flatMap { file ->
file.collectDescendantsOfType<KtTypeAlias> { typeAlias ->
typeAlias.getClassId() == classId
}
}.toList()
override fun getTypeAliasNamesInPackage(packageFqName: FqName): Set<Name> =
filesByPackage(packageFqName)
.flatMap { it.declarations }
.filterIsInstance<KtTypeAlias>()
.mapNotNullTo(mutableSetOf()) { it.nameAsName }
override fun getPropertyNamesInPackage(packageFqName: FqName): Set<Name> =
filesByPackage(packageFqName)
.flatMap { it.declarations }
.filterIsInstance<KtProperty>()
.mapNotNullTo(mutableSetOf()) { it.nameAsName }
override fun getFunctionsNamesInPackage(packageFqName: FqName): Set<Name> =
filesByPackage(packageFqName)
.flatMap { it.declarations }
.filterIsInstance<KtNamedFunction>()
.mapNotNullTo(mutableSetOf()) { it.nameAsName }
override fun getFacadeFilesInPackage(packageFqName: FqName): Collection<KtFile> =
filesByPackage(packageFqName)
.filter { file -> file.hasTopLevelCallables() }
.toSet()
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> =
filesByPackage(callableId.packageName)
.flatMap { it.declarations }
.filterIsInstance<KtProperty>()
.filter { it.nameAsName == callableId.callableName }
.toList()
override fun getTopLevelFunctions(callableId: CallableId): Collection<KtNamedFunction> =
filesByPackage(callableId.packageName)
.flatMap { it.declarations }
.filterIsInstance<KtNamedFunction>()
.filter { it.nameAsName == callableId.callableName }
.toList()
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)
}
}
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2021 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
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.analysis.providers.KotlinPackageProvider
import org.jetbrains.kotlin.analysis.providers.KotlinPackageProviderFactory
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtFile
public class KotlinStaticPackageProvider(
scope: GlobalSearchScope,
files: Collection<KtFile>
) : KotlinPackageProvider() {
private val packageToSubPackageNames: Map<FqName, Set<Name>> = run {
val filesInScope = files.filter { scope.contains(it.virtualFile) }
val packages = mutableMapOf<FqName, MutableSet<Name>>()
filesInScope.forEach { file ->
var currentPackage = FqName.ROOT
for (subPackage in file.packageFqName.pathSegments()) {
packages.getOrPut(currentPackage) { mutableSetOf() } += subPackage
currentPackage = currentPackage.child(subPackage)
}
packages.computeIfAbsent(currentPackage) { mutableSetOf() }
}
packages
}
override fun isPackageExists(packageFqName: FqName): Boolean {
return packageFqName in packageToSubPackageNames
}
override fun getKotlinSubPackageFqNames(packageFqName: FqName): Set<Name> {
return packageToSubPackageNames[packageFqName] ?: emptySet()
}
}
public class KotlinStaticPackageProviderFactory(private val files: Collection<KtFile>) : KotlinPackageProviderFactory() {
override fun createPackageProvider(searchScope: GlobalSearchScope): KotlinPackageProvider {
return KotlinStaticPackageProvider(searchScope, files)
}
}