KT-13605 Find Usages fails to find usages of component function defined in Java
#KT-13605 Fixed
This commit is contained in:
@@ -78,7 +78,7 @@ fun PsiReference.matchesTarget(candidateTarget: PsiElement): Boolean {
|
||||
if (candidateTarget !is KtNamedFunction) return false
|
||||
}
|
||||
is KtDestructuringDeclarationReference -> {
|
||||
if (candidateTarget !is KtNamedFunction && candidateTarget !is KtParameter) return false
|
||||
if (candidateTarget !is KtNamedFunction && candidateTarget !is KtParameter && candidateTarget !is PsiMethod) return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-15
@@ -25,24 +25,16 @@ import org.jetbrains.kotlin.KtNodeTypes
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.idea.references.KtDestructuringDeclarationReference
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.dataClassUtils.getComponentIndex
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
|
||||
|
||||
class DestructuringDeclarationReferenceSearcher(
|
||||
targetDeclaration: PsiElement,
|
||||
private val componentIndex: Int,
|
||||
searchScope: SearchScope,
|
||||
consumer: Processor<PsiReference>,
|
||||
optimizer: SearchRequestCollector
|
||||
) : OperatorReferenceSearcher<KtDestructuringDeclaration>(targetDeclaration, searchScope, consumer, optimizer, wordsToSearch = listOf("(")) {
|
||||
|
||||
//TODO
|
||||
private val componentIndex = when (targetDeclaration) {
|
||||
is KtParameter -> targetDeclaration.dataClassComponentFunction()?.name?.asString()?.let { getComponentIndex(it) }
|
||||
is KtFunction -> targetDeclaration.name?.let { getComponentIndex(it) }
|
||||
//TODO: java component functions (see KT-13605)
|
||||
else -> null
|
||||
}
|
||||
|
||||
override fun resolveTargetToDescriptor(): FunctionDescriptor? {
|
||||
if (targetDeclaration is KtParameter) {
|
||||
return targetDeclaration.dataClassComponentFunction()
|
||||
@@ -52,15 +44,10 @@ class DestructuringDeclarationReferenceSearcher(
|
||||
}
|
||||
}
|
||||
|
||||
override fun run() {
|
||||
if (componentIndex == null) return
|
||||
super.run()
|
||||
}
|
||||
|
||||
override fun extractReference(element: PsiElement): PsiReference? {
|
||||
val destructuringDeclaration = element as? KtDestructuringDeclaration ?: return null
|
||||
val entries = destructuringDeclaration.entries
|
||||
if (entries.size < componentIndex!!) return null
|
||||
if (entries.size < componentIndex) return null
|
||||
return entries[componentIndex - 1].references.firstIsInstance<KtDestructuringDeclarationReference>()
|
||||
}
|
||||
|
||||
|
||||
+4
-2
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.idea.util.fuzzyExtensionReceiverType
|
||||
import org.jetbrains.kotlin.idea.util.toFuzzyType
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.dataClassUtils.getComponentIndex
|
||||
import org.jetbrains.kotlin.resolve.dataClassUtils.isComponentLike
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
|
||||
import org.jetbrains.kotlin.types.expressions.OperatorConventions
|
||||
@@ -105,7 +106,8 @@ abstract class OperatorReferenceSearcher<TReferenceElement : KtElement>(
|
||||
): OperatorReferenceSearcher<*>? {
|
||||
if (isComponentLike(name)) {
|
||||
if (!options.searchForComponentConventions) return null
|
||||
return DestructuringDeclarationReferenceSearcher(declaration, searchScope, consumer, optimizer)
|
||||
val componentIndex = getComponentIndex(name.asString())
|
||||
return DestructuringDeclarationReferenceSearcher(declaration, componentIndex, searchScope, consumer, optimizer)
|
||||
}
|
||||
|
||||
if (!options.searchForOperatorConventions) return null
|
||||
@@ -160,7 +162,7 @@ abstract class OperatorReferenceSearcher<TReferenceElement : KtElement>(
|
||||
} as? FunctionDescriptor
|
||||
}
|
||||
|
||||
open fun run() {
|
||||
fun run() {
|
||||
val inProgress = SearchesInProgress.get()
|
||||
if (!inProgress.add(targetDeclaration)) return //TODO: it's not quite correct
|
||||
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// PSI_ELEMENT: com.intellij.psi.PsiMethod
|
||||
// OPTIONS: usages
|
||||
|
||||
public class A {
|
||||
}
|
||||
|
||||
public class JavaClass {
|
||||
public A <caret>component1() {
|
||||
return new A();
|
||||
}
|
||||
|
||||
public int component2() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fun f(c: JavaClass) {
|
||||
val (a, n) = c
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
Resolved (a, n)
|
||||
Searched references to JavaClass
|
||||
Searched references to parameter c of f(c: JavaClass) in Kotlin files
|
||||
+1
@@ -0,0 +1 @@
|
||||
Value read 2 val (a, n) = c
|
||||
@@ -1569,6 +1569,12 @@ public class FindUsagesTestGenerated extends AbstractFindUsagesTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("javaComponentFunctions.0.java")
|
||||
public void testJavaComponentFunctions() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/java/findJavaMethodUsages/javaComponentFunctions.0.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("OverriddenMethodSyntheticAccessor.0.java")
|
||||
public void testOverriddenMethodSyntheticAccessor() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/java/findJavaMethodUsages/OverriddenMethodSyntheticAccessor.0.java");
|
||||
|
||||
Reference in New Issue
Block a user