Navigation: Support NEW_AS_CONSTRUCTOR flag for constructor calls

#KT-15398 Fixed
 #KT-15536 Fixed
This commit is contained in:
Alexey Sedunov
2017-03-01 15:26:42 +03:00
parent 5e8cd654ec
commit eb9c775476
8 changed files with 60 additions and 5 deletions
@@ -16,17 +16,15 @@
package org.jetbrains.kotlin.idea.search.ideaExtensions
import com.intellij.codeInsight.JavaTargetElementEvaluator
import com.intellij.codeInsight.TargetElementEvaluatorEx
import com.intellij.codeInsight.TargetElementUtil
import com.intellij.codeInsight.TargetElementUtilExtender
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiReference
import org.jetbrains.kotlin.idea.references.KtDestructuringDeclarationReference
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isAbstract
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.*
class KotlinTargetElementEvaluator : TargetElementEvaluatorEx {
override fun includeSelfInGotoImplementation(element: PsiElement): Boolean = !(element is KtClass && element.isAbstract())
@@ -36,6 +34,15 @@ class KotlinTargetElementEvaluator : TargetElementEvaluatorEx {
if (ref is KtDestructuringDeclarationReference && flags.and(TargetElementUtil.ELEMENT_NAME_ACCEPTED) != 0) {
return ref.element
}
val refExpression = ref.element as? KtSimpleNameExpression
val calleeExpression = refExpression?.getParentOfTypeAndBranch<KtCallElement> { calleeExpression }
if (calleeExpression != null) {
(ref.resolve() as? KtConstructor<*>)?.let {
return if (flags and JavaTargetElementEvaluator.NEW_AS_CONSTRUCTOR != 0) it else it.containingClassOrObject
}
}
return null
}
@@ -0,0 +1,8 @@
// PSI_ELEMENT: org.jetbrains.kotlin.psi.KtClass
// OPTIONS: usages, constructorUsages
annotation class MyAnnotation()
@<caret>MyAnnotation
fun test() {
MyAnnotation::class.java
}
@@ -0,0 +1,2 @@
Annotation 5 @MyAnnotation
Unclassified usage 7 MyAnnotation::class.java
@@ -0,0 +1,7 @@
// PSI_ELEMENT: org.jetbrains.kotlin.psi.KtClass
// OPTIONS: usages, constructorUsages
fun test() {
val kk: KK = <caret>KK()
}
class KK()
@@ -0,0 +1,2 @@
Local variable declaration 4 val kk: KK = KK()
New instance creation 4 val kk: KK = KK()
@@ -0,0 +1,9 @@
// PSI_ELEMENT: org.jetbrains.kotlin.psi.KtClass
// OPTIONS: usages, constructorUsages
fun test() {
val kk: KK = <caret>KK()
}
class KK {
constructor()
}
@@ -0,0 +1,2 @@
Local variable declaration 4 val kk: KK = KK()
New instance creation 4 val kk: KK = KK()
@@ -979,6 +979,18 @@ public class FindUsagesTestGenerated extends AbstractFindUsagesTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/findUsages/kotlin/findPrimaryConstructorUsages"), Pattern.compile("^(.+)\\.0\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("annotationEntry.0.kt")
public void testAnnotationEntry() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findPrimaryConstructorUsages/annotationEntry.0.kt");
doTest(fileName);
}
@TestMetadata("constructorCall.0.kt")
public void testConstructorCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findPrimaryConstructorUsages/constructorCall.0.kt");
doTest(fileName);
}
@TestMetadata("jvmOverloaded.0.kt")
public void testJvmOverloaded() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findPrimaryConstructorUsages/jvmOverloaded.0.kt");
@@ -1195,6 +1207,12 @@ public class FindUsagesTestGenerated extends AbstractFindUsagesTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/findUsages/kotlin/findSecondaryConstructorUsages"), Pattern.compile("^(.+)\\.0\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("constructorCall.0.kt")
public void testConstructorCall() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findSecondaryConstructorUsages/constructorCall.0.kt");
doTest(fileName);
}
@TestMetadata("defaultSecondaryConstructor.0.kt")
public void testDefaultSecondaryConstructor() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/findUsages/kotlin/findSecondaryConstructorUsages/defaultSecondaryConstructor.0.kt");