[LL API] Pass a contextual module to ProjectStructureProvider

In certain cases, it's impossible to determine which module owns a
particular file without knowing the analysis context. For instance,
the file might be a part of a physical module, and be also included into
a virtual ad-hoc module (to be analyzed in separate, e.g. a VCS diff).

The new API allows to pass a contextual module. Basically it means
"give me a module for this element, implying that we are now analyzing a
contextual module".

^KT-57559 Fixed
This commit is contained in:
Yan Zhulanow
2023-04-18 17:19:14 +09:00
committed by Space Team
parent 5e04c0d4f7
commit c90d094af6
4 changed files with 31 additions and 10 deletions
@@ -12,11 +12,13 @@ import org.jetbrains.kotlin.serialization.deserialization.builtins.BuiltInSerial
class KtStaticModuleProvider(
private val builtinsModule: KtBuiltinsModule,
val projectStructure: KtModuleProjectStructure,
private val projectStructure: KtModuleProjectStructure,
) : ProjectStructureProvider() {
val allModules: List<KtModule>
get() = projectStructure.allKtModules()
@OptIn(KtModuleStructureInternals::class)
override fun getKtModuleForKtElement(element: PsiElement): KtModule {
override fun getModule(element: PsiElement, contextualModule: KtModule?): KtModule {
val containingFileAsPsiFile = element.containingFile
val containingFileAsVirtualFile = containingFileAsPsiFile.virtualFile
if (containingFileAsVirtualFile.extension == BuiltInSerializerProtocol.BUILTINS_FILE_EXTENSION) {
@@ -30,8 +32,7 @@ class KtStaticModuleProvider(
}?.let { return it }
return projectStructure.mainModules
.first { module ->
element in module.ktModule.contentScope
}.ktModule
.first { module -> element in module.ktModule.contentScope }
.ktModule
}
}