[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:
committed by
Space Team
parent
f5bbf2b4fe
commit
8b0eb0488f
+21
@@ -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)
|
||||
}
|
||||
}
|
||||
+2
@@ -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
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class AA {
|
||||
companion object {
|
||||
fun x() = 10
|
||||
}
|
||||
}
|
||||
fun main() {
|
||||
A<caret>A.Companion.x()
|
||||
}
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
isImplicitCompanionReference: false
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class AA {
|
||||
companion object BB {
|
||||
fun x() = 10
|
||||
}
|
||||
}
|
||||
fun main() {
|
||||
A<caret>A.BB.x()
|
||||
}
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
isImplicitCompanionReference: false
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class AA {
|
||||
companion object {
|
||||
fun x() = 10
|
||||
}
|
||||
}
|
||||
fun main() {
|
||||
A<caret>A()
|
||||
}
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
isImplicitCompanionReference: false
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
class AA {
|
||||
companion object {
|
||||
operator fun invoke() {}
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
A<caret>A()
|
||||
}
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
isImplicitCompanionReference: false
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class AA {
|
||||
companion object {
|
||||
fun x() = 10
|
||||
}
|
||||
}
|
||||
fun main() {
|
||||
AA.Com<caret>panion.x()
|
||||
}
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
isImplicitCompanionReference: false
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class AA {
|
||||
companion object BB {
|
||||
fun x() = 10
|
||||
}
|
||||
}
|
||||
fun main() {
|
||||
AA.B<caret>B.x()
|
||||
}
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
isImplicitCompanionReference: false
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class AA {
|
||||
companion object {
|
||||
fun x() = 10
|
||||
}
|
||||
}
|
||||
fun main() {
|
||||
A<caret>A.x()
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
isImplicitCompanionReference: true
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
class AA {
|
||||
companion object {
|
||||
fun x() = 10
|
||||
}
|
||||
}
|
||||
fun main() {
|
||||
A<caret>A
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
isImplicitCompanionReference: true
|
||||
Reference in New Issue
Block a user