DecompiledNavigationUtils -> findDecompiledDeclaration()

This commit is contained in:
Pavel V. Talanov
2015-12-10 12:23:31 +03:00
parent d89b609960
commit 01b79f640f
2 changed files with 90 additions and 99 deletions
@@ -18,16 +18,10 @@ package org.jetbrains.kotlin.idea.codeInsight
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.idea.decompiler.navigation.findDecompiledDeclaration
import org.jetbrains.kotlin.idea.decompiler.navigation.DecompiledNavigationUtils
import org.jetbrains.kotlin.idea.references.BuiltInsReferenceResolver import org.jetbrains.kotlin.idea.references.BuiltInsReferenceResolver
import com.intellij.psi.JavaPsiFacade import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.idea.search.allScope
import org.jetbrains.kotlin.utils.addIfNotNull
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor.Kind.*
import java.util.*
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.utils.addToStdlib.sequenceOfLazyValues import org.jetbrains.kotlin.utils.addToStdlib.sequenceOfLazyValues
public object DescriptorToSourceUtilsIde { public object DescriptorToSourceUtilsIde {
@@ -52,7 +46,7 @@ public object DescriptorToSourceUtilsIde {
// therefore we put both source declaration and decompiled declaration to stream, and afterwards we filter it in getAllDeclarations // therefore we put both source declaration and decompiled declaration to stream, and afterwards we filter it in getAllDeclarations
sequenceOfLazyValues( sequenceOfLazyValues(
{ DescriptorToSourceUtils.getSourceFromDescriptor(effectiveReferenced) }, { DescriptorToSourceUtils.getSourceFromDescriptor(effectiveReferenced) },
{ BuiltInsReferenceResolver.resolveBuiltInSymbol(project, effectiveReferenced) ?: DecompiledNavigationUtils.getDeclarationFromDecompiledClassFile(project, effectiveReferenced) } { BuiltInsReferenceResolver.resolveBuiltInSymbol(project, effectiveReferenced) ?: findDecompiledDeclaration(project, effectiveReferenced) }
) )
}.filterNotNull() }.filterNotNull()
} }
@@ -39,9 +39,7 @@ import org.jetbrains.kotlin.serialization.js.KotlinJavascriptPackageFragment
import org.jetbrains.kotlin.types.ErrorUtils import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils
object DecompiledNavigationUtils { fun findDecompiledDeclaration(
fun getDeclarationFromDecompiledClassFile(
project: Project, project: Project,
referencedDescriptor: DeclarationDescriptor): KtDeclaration? { referencedDescriptor: DeclarationDescriptor): KtDeclaration? {
if (isLocal(referencedDescriptor)) return null if (isLocal(referencedDescriptor)) return null
@@ -54,21 +52,21 @@ object DecompiledNavigationUtils {
} }
return psiFile.getDeclarationForDescriptor(referencedDescriptor) return psiFile.getDeclarationForDescriptor(referencedDescriptor)
} }
private fun isLocal(descriptor: DeclarationDescriptor): Boolean { private fun isLocal(descriptor: DeclarationDescriptor): Boolean {
if (descriptor is ParameterDescriptor) { if (descriptor is ParameterDescriptor) {
return isLocal(descriptor.containingDeclaration) return isLocal(descriptor.containingDeclaration)
} }
else { else {
return DescriptorUtils.isLocal(descriptor) return DescriptorUtils.isLocal(descriptor)
} }
} }
/* /*
Find virtual file which contains the declaration of descriptor we're navigating to. Find virtual file which contains the declaration of descriptor we're navigating to.
*/ */
private fun findVirtualFileContainingDescriptor( private fun findVirtualFileContainingDescriptor(
project: Project, project: Project,
referencedDescriptor: DeclarationDescriptor): VirtualFile? { referencedDescriptor: DeclarationDescriptor): VirtualFile? {
if (ErrorUtils.isError(referencedDescriptor)) return null if (ErrorUtils.isError(referencedDescriptor)) return null
@@ -87,16 +85,16 @@ object DecompiledNavigationUtils {
val fileFinder = virtualFileFinderFactory.create(scopeToSearchIn) val fileFinder = virtualFileFinderFactory.create(scopeToSearchIn)
return fileFinder.findVirtualFileWithHeader(containerClassId) return fileFinder.findVirtualFileWithHeader(containerClassId)
} }
private fun isFromKotlinJavasriptMetadata(referencedDescriptor: DeclarationDescriptor): Boolean { private fun isFromKotlinJavasriptMetadata(referencedDescriptor: DeclarationDescriptor): Boolean {
val packageFragmentDescriptor = DescriptorUtils.getParentOfType(referencedDescriptor, PackageFragmentDescriptor::class.java, false) val packageFragmentDescriptor = DescriptorUtils.getParentOfType(referencedDescriptor, PackageFragmentDescriptor::class.java, false)
return packageFragmentDescriptor is KotlinJavascriptPackageFragment return packageFragmentDescriptor is KotlinJavascriptPackageFragment
} }
//TODO: navigate to inner classes //TODO: navigate to inner classes
//TODO: should we construct proper SourceElement's for decompiled parts / facades? //TODO: should we construct proper SourceElement's for decompiled parts / facades?
private fun getContainerClassId(project: Project, referencedDescriptor: DeclarationDescriptor): ClassId? { private fun getContainerClassId(project: Project, referencedDescriptor: DeclarationDescriptor): ClassId? {
val deserializedCallableContainer = DescriptorUtils.getParentOfType(referencedDescriptor, DeserializedCallableMemberDescriptor::class.java, true) val deserializedCallableContainer = DescriptorUtils.getParentOfType(referencedDescriptor, DeserializedCallableMemberDescriptor::class.java, true)
if (deserializedCallableContainer != null) { if (deserializedCallableContainer != null) {
return getContainerClassId(project, deserializedCallableContainer) return getContainerClassId(project, deserializedCallableContainer)
@@ -131,5 +129,4 @@ object DecompiledNavigationUtils {
return containerDescriptor.classId return containerDescriptor.classId
} }
return null return null
}
} }