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
val cacheService = KotlinCacheService.getInstance(project)
val moduleInfo = this.getNullableModuleInfo() ?: return null
@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? {
if (this is KtLightElement<*, *>) return this.getModuleInfoForLightElement()
if (this is KtLightElement<*, *>) return this.getModuleInfoForLightElement(onFailure)
val containingJetFile = (this as? KtElement)?.containingFile as? KtFile
val context = containingJetFile?.analysisContext
@@ -119,7 +119,7 @@ private fun getModuleInfoByVirtualFile(project: Project, virtualFile: VirtualFil
return NotUnderContentRootModuleInfo
}
private fun KtLightElement<*, *>.getModuleInfoForLightElement(): IdeaModuleInfo {
private fun KtLightElement<*, *>.getModuleInfoForLightElement(onFailure: (String) -> IdeaModuleInfo?): IdeaModuleInfo? {
val decompiledClass = this.getParentOfType<KtLightClassForDecompiledDeclaration>(strict = false)
if (decompiledClass != null) {
return getModuleInfoByVirtualFile(
@@ -131,7 +131,7 @@ private fun KtLightElement<*, *>.getModuleInfoForLightElement(): IdeaModuleInfo
val element = getOrigin() ?: when (this) {
is FakeLightClassForFileOfPackage -> this.getContainingFile()!!
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()
}