[Analysis API] add method for implicit companion object reference detection

Add a new method `isImplicitReferenceToCompanion` to determine
if the given reference is an implicit reference to a companion object.

The method is needed for the rename refactoring in IJ.

^KTIJ-25863
This commit is contained in:
Ilya Kirillov
2023-06-22 11:13:14 +02:00
committed by Space Team
parent f5bbf2b4fe
commit 8b0eb0488f
26 changed files with 553 additions and 1 deletions
@@ -17,4 +17,25 @@ public interface KtReferenceResolveMixIn : KtAnalysisSessionMixIn {
public fun KtReference.resolveToSymbol(): KtSymbol? = withValidityAssertion {
return resolveToSymbols().singleOrNull()
}
/**
* Checks if the reference is an implicit reference to a companion object via the containing class.
*
* Example:
* ```
* class A {
* companion object {
* fun foo() {}
* }
* }
* ```
*
* For the case provided, inside the call `A.foo()`,
* the `A` is an implicit reference to the companion object, so `isImplicitReferenceToCompanion` returns `true`
*
* @return `true` if the reference is an implicit reference to a companion object, `false` otherwise.
*/
public fun KtReference.isImplicitReferenceToCompanion(): Boolean = withValidityAssertion {
analysisSession.referenceResolveProvider.isImplicitReferenceToCompanion(this)
}
}
@@ -10,4 +10,6 @@ import org.jetbrains.kotlin.idea.references.KtReference
public abstract class KtReferenceResolveProvider {
public abstract fun resolveToSymbols(reference: KtReference): Collection<KtSymbol>
public abstract fun isImplicitReferenceToCompanion(reference: KtReference): Boolean
}
@@ -0,0 +1,9 @@
class AA {
companion object {
fun x() = 10
}
}
fun main() {
A<caret>A.Companion.x()
}
@@ -0,0 +1,9 @@
class AA {
companion object BB {
fun x() = 10
}
}
fun main() {
A<caret>A.BB.x()
}
@@ -0,0 +1,9 @@
class AA {
companion object {
fun x() = 10
}
}
fun main() {
A<caret>A()
}
@@ -0,0 +1 @@
isImplicitCompanionReference: false
@@ -0,0 +1,10 @@
class AA {
companion object {
operator fun invoke() {}
}
}
fun main() {
A<caret>A()
}
@@ -0,0 +1,9 @@
class AA {
companion object {
fun x() = 10
}
}
fun main() {
AA.Com<caret>panion.x()
}
@@ -0,0 +1,9 @@
class AA {
companion object BB {
fun x() = 10
}
}
fun main() {
AA.B<caret>B.x()
}
@@ -0,0 +1,8 @@
class AA {
companion object {
fun x() = 10
}
}
fun main() {
A<caret>A.x()
}
@@ -0,0 +1,8 @@
class AA {
companion object {
fun x() = 10
}
}
fun main() {
A<caret>A
}