Avoid throwing on trying to obtain module info by some unexpected light element

This commit is contained in:
Pavel V. Talanov
2016-01-14 16:06:23 +03:00
parent 7ddac1945e
commit 06d0a7c17c
2 changed files with 5 additions and 4 deletions
@@ -89,8 +89,9 @@ private fun PsiElement.getJavaDescriptorResolver(resolutionFacade: ResolutionFac
if (!ProjectRootsUtil.isInProjectOrLibraryClassFile(this)) return null if (!ProjectRootsUtil.isInProjectOrLibraryClassFile(this)) return null
val cacheService = KotlinCacheService.getInstance(project) val cacheService = KotlinCacheService.getInstance(project)
val moduleInfo = this.getNullableModuleInfo() ?: return null
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
return (cacheService as? KotlinCacheServiceImpl)?.getProjectService(JvmPlatform, this.getModuleInfo(), JavaDescriptorResolver::class.java) return (cacheService as? KotlinCacheServiceImpl)?.getProjectService(JvmPlatform, moduleInfo, JavaDescriptorResolver::class.java)
} }
} }
@@ -42,7 +42,7 @@ fun PsiElement.getNullableModuleInfo(): IdeaModuleInfo? = this.getModuleInfo { r
} }
private fun PsiElement.getModuleInfo(onFailure: (String) -> IdeaModuleInfo?): IdeaModuleInfo? { private fun PsiElement.getModuleInfo(onFailure: (String) -> IdeaModuleInfo?): IdeaModuleInfo? {
if (this is KtLightElement<*, *>) return this.getModuleInfoForLightElement() if (this is KtLightElement<*, *>) return this.getModuleInfoForLightElement(onFailure)
val containingJetFile = (this as? KtElement)?.containingFile as? KtFile val containingJetFile = (this as? KtElement)?.containingFile as? KtFile
val context = containingJetFile?.analysisContext val context = containingJetFile?.analysisContext
@@ -119,7 +119,7 @@ private fun getModuleInfoByVirtualFile(project: Project, virtualFile: VirtualFil
return NotUnderContentRootModuleInfo return NotUnderContentRootModuleInfo
} }
private fun KtLightElement<*, *>.getModuleInfoForLightElement(): IdeaModuleInfo { private fun KtLightElement<*, *>.getModuleInfoForLightElement(onFailure: (String) -> IdeaModuleInfo?): IdeaModuleInfo? {
val decompiledClass = this.getParentOfType<KtLightClassForDecompiledDeclaration>(strict = false) val decompiledClass = this.getParentOfType<KtLightClassForDecompiledDeclaration>(strict = false)
if (decompiledClass != null) { if (decompiledClass != null) {
return getModuleInfoByVirtualFile( return getModuleInfoByVirtualFile(
@@ -131,7 +131,7 @@ private fun KtLightElement<*, *>.getModuleInfoForLightElement(): IdeaModuleInfo
val element = getOrigin() ?: when (this) { val element = getOrigin() ?: when (this) {
is FakeLightClassForFileOfPackage -> this.getContainingFile()!! is FakeLightClassForFileOfPackage -> this.getContainingFile()!!
is KtLightClassForFacade -> this.files.first() is KtLightClassForFacade -> this.files.first()
else -> throw IllegalStateException("Unknown light class without origin is referenced by IDE lazy resolve: $javaClass") else -> return onFailure("Light element without origin is referenced by resolve:\n$this\n${this.getDelegate().text}")
} }
return element.getModuleInfo() return element.getModuleInfo()
} }