Fix navigation in IDE to synthetic methods in compiled Kotlin

For some synthetic methods from compiled Kotlin classes, it is
not possible to find the PsiElement, when navigating to declaration
in the IDE.

For other methods it works like this:
1. There is a SourceElement with a psi inside
2. Or for K1, the element is searched in the decompiled text
   (see ByDescriptorIndexer),
3. Or for K2, the element is searched by Stub taken from metadata
   (see FirDeserializedDeclarationSourceProvider)

However, these approaches do not apply to synthetic methods which have
no SourceElement and are not written into decompiled text and metadata.

These are methods values, valueOf, entries from enum and
copy, equals, hashCode, toString from data classes.

Therefore, it was decided to handle their cases separately
at the resolve stage.

Tests (in idea repository):
- org.jetbrains.kotlin.idea.resolve.ReferenceResolveWithLibTestGenerated#testDataClassSyntheticMethods
- org.jetbrains.kotlin.idea.resolve.ReferenceResolveWithLibTestGenerated#testEnumSyntheticMethods
- org.jetbrains.kotlin.idea.fir.resolve.FirReferenceResolveWithLibTestGenerated#testDataClassSyntheticMethods
- org.jetbrains.kotlin.idea.fir.resolve.FirReferenceResolveWithLibTestGenerated#testEnumSyntheticMethods

^KTIJ-24413 Fixed
This commit is contained in:
Roman Efremov
2023-02-09 13:22:20 +01:00
committed by Space Team
parent f640a7be2e
commit d120d83d60
4 changed files with 61 additions and 18 deletions
@@ -52,6 +52,12 @@ object ByDescriptorIndexer : DecompiledTextIndexer<String> {
return classOrObject?.primaryConstructor ?: classOrObject
}
if ((original as? CallableMemberDescriptor)?.mustNotBeWrittenToDecompiledText() == true &&
original.containingDeclaration is ClassDescriptor
) {
return getDeclarationForDescriptor(original.containingDeclaration, file)
}
val descriptorKey = original.toStringKey()
if (!file.isContentsLoaded && original is MemberDescriptor) {
@@ -34,6 +34,20 @@ fun DescriptorRendererOptions.defaultDecompilerRendererOptions() {
parameterNamesInFunctionalTypes = false // to support parameters names in decompiled text we need to load annotation arguments
}
internal fun CallableMemberDescriptor.mustNotBeWrittenToDecompiledText(): Boolean {
return when (kind) {
CallableMemberDescriptor.Kind.DECLARATION -> false
CallableMemberDescriptor.Kind.FAKE_OVERRIDE,
CallableMemberDescriptor.Kind.DELEGATION -> true
CallableMemberDescriptor.Kind.SYNTHESIZED -> {
// Of all synthesized functions, only `component*` functions are rendered (for historical reasons)
!DataClassDescriptorResolver.isComponentLike(name)
}
}
}
fun buildDecompiledText(
packageFqName: FqName,
descriptors: List<DeclarationDescriptor>,
@@ -56,20 +70,6 @@ fun buildDecompiledText(
textIndex.addToIndex(descriptor, TextRange(startOffset, endOffset))
}
fun CallableMemberDescriptor.isConsideredSynthetic(): Boolean {
return when (kind) {
CallableMemberDescriptor.Kind.DECLARATION -> false
CallableMemberDescriptor.Kind.FAKE_OVERRIDE,
CallableMemberDescriptor.Kind.DELEGATION -> true
CallableMemberDescriptor.Kind.SYNTHESIZED -> {
// Of all synthesized functions, only `component*` functions are rendered (for historical reasons)
!DataClassDescriptorResolver.isComponentLike(name)
}
}
}
fun appendDescriptor(descriptor: DeclarationDescriptor, indent: String, lastEnumEntry: Boolean? = null) {
val startOffset = builder.length
if (isEnumEntry(descriptor)) {
@@ -144,7 +144,7 @@ fun buildDecompiledText(
if (member == companionObject) {
continue
}
if (member is CallableMemberDescriptor && member.isConsideredSynthetic()) {
if (member is CallableMemberDescriptor && member.mustNotBeWrittenToDecompiledText()) {
continue
}
newlineExceptFirst()