From 5dbf3b6b4d00749ba06a05355285f25e5c6007b5 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Mon, 17 Apr 2017 18:37:18 +0300 Subject: [PATCH] Allow search usages for declarations when correct isEquivalentTo Now it's possible to do find usages for other languages when light elements are used. Revert efb36f4a494824176b1cdb83803ab5334192133f and revise fix of KT-7983: Don't consider local and non-local class with same fqn equivalent. #KT-7983 Fixed --- .../src/org/jetbrains/kotlin/psi/KtClass.kt | 23 +++++++++++++++---- .../kotlin/idea/references/referenceUtil.kt | 9 ++++---- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtClass.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtClass.kt index 284eae40711..8cecd18a304 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtClass.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtClass.kt @@ -50,14 +50,27 @@ open class KtClass : KtClassOrObject { fun isInner(): Boolean = hasModifier(KtTokens.INNER_KEYWORD) override fun isEquivalentTo(another: PsiElement?): Boolean { - if (super.isEquivalentTo(another)) { + if (this === another) { return true } - if (another is KtClass) { - val fq1 = getQualifiedName() - val fq2 = another.getQualifiedName() - return fq1 != null && fq2 != null && fq1 == fq2 + + if (another !is KtClass) { + return false } + + val fq1 = getQualifiedName() ?: return false + val fq2 = another.getQualifiedName() ?: return false + if (fq1 == fq2) { + val thisLocal = isLocal + if (thisLocal != another.isLocal) { + return false + } + + // For non-local classes same fqn is enough + // Consider different instances of local classes non-equivalent + return !thisLocal + } + return false } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/references/referenceUtil.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/references/referenceUtil.kt index 9dc57106298..b79930905b3 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/references/referenceUtil.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/references/referenceUtil.kt @@ -86,7 +86,11 @@ fun PsiReference.matchesTarget(candidateTarget: PsiElement): Boolean { } val targets = unwrappedTargets - if (unwrappedCandidate in targets) return true + + val manager = candidateTarget.manager + if (targets.any { manager.areElementsEquivalent(unwrappedCandidate, it) }) { + return true + } if (element is KtLabelReferenceExpression && (element.parent as? KtContainerNode)?.parent is KtReturnExpression) { targets.forEach { @@ -97,9 +101,6 @@ fun PsiReference.matchesTarget(candidateTarget: PsiElement): Boolean { } } - // TODO: Investigate why PsiCompiledElement identity changes - if (unwrappedCandidate is PsiCompiledElement && targets.any { it.isEquivalentTo(unwrappedCandidate) }) return true - if (this is KtReference) { return targets.any { it.isConstructorOf(unwrappedCandidate)