[AA] Standalone Analysis API: Introduce KtStaticProjectStructureProvider

- `registerServicesForProjectEnvironment` relied on lists of all modules
  and source files, which the standalone project structure providers
  would have already been able to provide with the right interface.
- In `StandaloneAnalysisAPISessionBuilder.build`, to get all modules and
  source files, `projectStructureProvider` had to be hard-casted to
  `KtModuleProviderImpl`.
- This commit introduces `KtStaticProjectStructureProvider`, a shared
  interface for project structure providers with static module
  structures that know about their modules and source files.
This commit is contained in:
Marco Pennekamp
2023-05-04 21:15:44 +02:00
committed by Space Team
parent e3d803de8b
commit 7fd8e4d0fd
9 changed files with 72 additions and 41 deletions
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.analysis.api.standalone.base.project.structure
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFileSystemItem
import org.jetbrains.kotlin.analysis.project.structure.*
import org.jetbrains.kotlin.psi.psiUtil.contains
import org.jetbrains.kotlin.serialization.deserialization.builtins.BuiltInSerializerProtocol
@@ -14,10 +15,7 @@ import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
class KtStaticModuleProvider(
private val builtinsModule: KtBuiltinsModule,
private val projectStructure: KtModuleProjectStructure,
) : ProjectStructureProvider() {
val allModules: List<KtModule>
get() = projectStructure.allKtModules()
) : KtStaticProjectStructureProvider() {
@OptIn(KtModuleStructureInternals::class)
override fun getModule(element: PsiElement, contextualModule: KtModule?): KtModule {
val containingFileAsPsiFile = element.containingFile
@@ -38,7 +36,11 @@ class KtStaticModuleProvider(
?: throw KotlinExceptionWithAttachments("Cannot find KtModule; see the attachment for more details.")
.withAttachment(
containingFileAsVirtualFile.path,
allModules.joinToString(separator = System.lineSeparator()) { it.asDebugString() }
allKtModules.joinToString(separator = System.lineSeparator()) { it.asDebugString() }
)
}
override val allKtModules: List<KtModule> = projectStructure.allKtModules()
override val allSourceFiles: List<PsiFileSystemItem> = projectStructure.allSourceFiles()
}
@@ -0,0 +1,28 @@
/*
* 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.api.standalone.base.project.structure
import com.intellij.psi.PsiFileSystemItem
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.analysis.project.structure.KtNotUnderContentRootModule
import org.jetbrains.kotlin.analysis.project.structure.ProjectStructureProvider
/**
* A [ProjectStructureProvider] with a static module structure.
*
* Static project structure providers may still create [KtNotUnderContentRootModule]s on the fly, because files which don't belong to any
* of the pre-registered modules are by definition not part of the *static* module structure.
*/
public abstract class KtStaticProjectStructureProvider : ProjectStructureProvider() {
/**
* All [KtModule]s registered with the project structure provider, excluding [KtNotUnderContentRootModule]s and the built-ins module.
*
* [allKtModules] may be used by other services to pre-build caches based on the full module structure.
*/
public abstract val allKtModules: List<KtModule>
public abstract val allSourceFiles: List<PsiFileSystemItem>
}
@@ -162,9 +162,7 @@ object StandaloneProjectFactory {
fun registerServicesForProjectEnvironment(
environment: KotlinCoreProjectEnvironment,
projectStructureProvider: ProjectStructureProvider,
modules: List<KtModule>,
sourceFiles: List<PsiFileSystemItem>,
projectStructureProvider: KtStaticProjectStructureProvider,
languageVersionSettings: LanguageVersionSettings = latestLanguageVersionSettings,
jdkHome: Path? = null,
) {
@@ -181,7 +179,13 @@ object StandaloneProjectFactory {
}
project.registerService(ProjectStructureProvider::class.java, projectStructureProvider)
initialiseVirtualFileFinderServices(environment, modules, sourceFiles, languageVersionSettings, jdkHome)
initialiseVirtualFileFinderServices(
environment,
projectStructureProvider.allKtModules,
projectStructureProvider.allSourceFiles,
languageVersionSettings,
jdkHome,
)
}
private fun initialiseVirtualFileFinderServices(