diff --git a/plugins/kotlin/base/analysis/src/org/jetbrains/kotlin/idea/search/ideaExtensions/KotlinTargetElementEvaluator.kt b/plugins/kotlin/base/analysis/src/org/jetbrains/kotlin/idea/search/ideaExtensions/KotlinTargetElementEvaluator.kt index 9cb96071066a..2b5c77e4e439 100644 --- a/plugins/kotlin/base/analysis/src/org/jetbrains/kotlin/idea/search/ideaExtensions/KotlinTargetElementEvaluator.kt +++ b/plugins/kotlin/base/analysis/src/org/jetbrains/kotlin/idea/search/ideaExtensions/KotlinTargetElementEvaluator.kt @@ -2,10 +2,8 @@ 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.codeInsight.* +import com.intellij.openapi.editor.Editor import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import com.intellij.psi.PsiReference @@ -17,7 +15,7 @@ import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.* -abstract class KotlinTargetElementEvaluator : TargetElementEvaluatorEx, TargetElementUtilExtender { +abstract class KotlinTargetElementEvaluator : TargetElementEvaluatorEx2(), TargetElementEvaluatorEx, TargetElementUtilExtender { companion object { const val DO_NOT_UNWRAP_LABELED_EXPRESSION = 0x100 const val BYPASS_IMPORT_ALIAS = 0x200 @@ -37,6 +35,21 @@ abstract class KotlinTargetElementEvaluator : TargetElementEvaluatorEx, TargetEl override fun includeSelfInGotoImplementation(element: PsiElement): Boolean = !(element is KtClass && element.isAbstract()) + override fun adjustReferenceOrReferencedElement( + file: PsiFile, + editor: Editor, + offset: Int, + flags: Int, + refElement: PsiElement? + ): PsiElement? { + if (!BitUtil.isSet(flags, TargetElementUtil.LOOKUP_ITEM_ACCEPTED)) { + if (refElement is KtConstructor<*>) { + return refElement.getContainingClassOrObject() + } + } + return refElement + } + override fun getElementByReference(ref: PsiReference, flags: Int): PsiElement? { if (ref is KtSimpleNameReference && ref.expression is KtLabelReferenceExpression) { val refTarget = ref.resolve() as? KtExpression ?: return null diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/highlighter/UsageHighlightingTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/highlighter/UsageHighlightingTestGenerated.java index 472ed4536178..06795a3555e8 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/highlighter/UsageHighlightingTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/highlighter/UsageHighlightingTestGenerated.java @@ -23,6 +23,16 @@ public class UsageHighlightingTestGenerated extends AbstractUsageHighlightingTes KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); } + @TestMetadata("class.kt") + public void testClass() throws Exception { + runTest("testData/usageHighlighter/class.kt"); + } + + @TestMetadata("classRef.kt") + public void testClassRef() throws Exception { + runTest("testData/usageHighlighter/classRef.kt"); + } + @TestMetadata("destructingDeclaration.kt") public void testDestructingDeclaration() throws Exception { runTest("testData/usageHighlighter/destructingDeclaration.kt"); @@ -107,4 +117,9 @@ public class UsageHighlightingTestGenerated extends AbstractUsageHighlightingTes public void testLocalVal() throws Exception { runTest("testData/usageHighlighter/localVal.kt"); } + + @TestMetadata("primaryCtor.kt") + public void testPrimaryCtor() throws Exception { + runTest("testData/usageHighlighter/primaryCtor.kt"); + } } diff --git a/plugins/kotlin/idea/tests/testData/usageHighlighter/class.kt b/plugins/kotlin/idea/tests/testData/usageHighlighter/class.kt new file mode 100644 index 000000000000..2744c189c287 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/usageHighlighter/class.kt @@ -0,0 +1,7 @@ +class Foo(val foo: Int) // the declaration + +fun bar() { + Foo(1) + ::Foo + ~Foo::class +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/usageHighlighter/classRef.kt b/plugins/kotlin/idea/tests/testData/usageHighlighter/classRef.kt new file mode 100644 index 000000000000..9132f45293b9 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/usageHighlighter/classRef.kt @@ -0,0 +1,7 @@ +class Foo(val foo: Int) // the declaration + +fun bar() { + Foo(1) + ::~Foo + Foo::class +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/usageHighlighter/primaryCtor.kt b/plugins/kotlin/idea/tests/testData/usageHighlighter/primaryCtor.kt new file mode 100644 index 000000000000..d783ae6a3094 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/usageHighlighter/primaryCtor.kt @@ -0,0 +1,7 @@ +class Foo(val foo: Int) // the declaration + +fun bar() { + ~Foo(1) + ::Foo + Foo::class +} \ No newline at end of file