AA: null check containing file for PsiElement

This commit is contained in:
Jinseong Jeon
2022-06-12 22:35:37 -07:00
committed by Ilya Kirillov
parent 814920b344
commit 8c3df04c8e
@@ -15,9 +15,15 @@ internal class KtModuleProviderImpl(
internal val mainModules: List<KtModule>,
) : ProjectStructureProvider() {
override fun getKtModuleForKtElement(element: PsiElement): KtModule {
val containingFile = element.containingFile.virtualFile
val containingFileAsPsiFile = element.containingFile
?: error("Can't get containing PsiFile for ${element.text}")
// If an [element] is created on the fly, e.g., via [KtPsiFactory],
// its containing [PsiFile] may not have [VirtualFile].
// That also means the [element] is not bound to any [KtModule] either.
val containingFileAsVirtualFile = containingFileAsPsiFile.virtualFile
?: error("Can't get containing VirtualFile for ${element.text} in $containingFileAsPsiFile")
return mainModules.first { module ->
containingFile in module.contentScope
containingFileAsVirtualFile in module.contentScope
}
}