From ab04ae495694bedee098018afeb612cdd3b1ed1a Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 21 Oct 2015 14:27:24 +0300 Subject: [PATCH] KT-9365 Code completion does not suggest annotation names at type usage #KT-9365 Fixed --- .../idea/project/ResolveElementCache.kt | 20 +++++++++---------- .../basic/common/annotations/ForType.kt | 9 +++++++++ .../test/JSBasicCompletionTestGenerated.java | 6 ++++++ .../test/JvmBasicCompletionTestGenerated.java | 6 ++++++ 4 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 idea/idea-completion/testData/basic/common/annotations/ForType.kt diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/ResolveElementCache.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/ResolveElementCache.kt index 0337bec7a27..b1b99ce96df 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/ResolveElementCache.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/ResolveElementCache.kt @@ -36,6 +36,7 @@ import org.jetbrains.kotlin.idea.stubindex.JetProbablyNothingFunctionShortNameIn import org.jetbrains.kotlin.idea.stubindex.JetProbablyNothingPropertyShortNameIndex import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext import org.jetbrains.kotlin.psi.psiUtil.getParentOfType import org.jetbrains.kotlin.resolve.* @@ -436,12 +437,10 @@ public class ResolveElementCache( bodyResolver.resolvePropertyAccessors(bodyResolveContext, property, descriptor) - forceResolveAnnotations(property) + forceResolveAnnotationsInside(property) for (accessor in property.getAccessors()) { JetFlowInformationProvider(accessor, trace).checkDeclaration() - - forceResolveAnnotations(accessor) } return trace @@ -457,7 +456,7 @@ public class ResolveElementCache( val bodyResolver = createBodyResolver(resolveSession, trace, file, statementFilter) bodyResolver.resolveFunctionBody(DataFlowInfo.EMPTY, trace, namedFunction, functionDescriptor, scope) - forceResolveAnnotations(namedFunction) + forceResolveAnnotationsInside(namedFunction) return trace } @@ -472,7 +471,7 @@ public class ResolveElementCache( val bodyResolver = createBodyResolver(resolveSession, trace, file, statementFilter) bodyResolver.resolveSecondaryConstructorBody(DataFlowInfo.EMPTY, trace, constructor, constructorDescriptor, scope) - forceResolveAnnotations(constructor) + forceResolveAnnotationsInside(constructor) return trace } @@ -500,15 +499,16 @@ public class ResolveElementCache( val bodyResolver = createBodyResolver(resolveSession, trace, file, statementFilter) bodyResolver.resolveAnonymousInitializer(DataFlowInfo.EMPTY, classInitializer, classOrObjectDescriptor) - forceResolveAnnotations(classInitializer) + forceResolveAnnotationsInside(classInitializer) return trace } - private fun forceResolveAnnotations(declaration: KtModifierListOwner) { - for (annotationEntry in declaration.annotationEntries) { - val annotationDescriptor = resolveSession.bindingContext[BindingContext.ANNOTATION, annotationEntry] ?: continue - ForceResolveUtil.forceResolveAllContents(annotationDescriptor) + private fun forceResolveAnnotationsInside(element: KtElement) { + element.forEachDescendantOfType(canGoInside = { it !is KtBlockExpression }) { entry -> + resolveSession.bindingContext[BindingContext.ANNOTATION, entry]?.let { + ForceResolveUtil.forceResolveAllContents(it) + } } } diff --git a/idea/idea-completion/testData/basic/common/annotations/ForType.kt b/idea/idea-completion/testData/basic/common/annotations/ForType.kt new file mode 100644 index 00000000000..cb3a1955bd5 --- /dev/null +++ b/idea/idea-completion/testData/basic/common/annotations/ForType.kt @@ -0,0 +1,9 @@ +@Target(AnnotationTarget.TYPE) +annotation class AnnType + +class AnnFalse + +val v: @Ann Int = 1 + +// EXIST: AnnType +// ABSENT: AnnFalse \ No newline at end of file diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JSBasicCompletionTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JSBasicCompletionTestGenerated.java index 07f5b3cfb22..5d8ad56b2a7 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JSBasicCompletionTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JSBasicCompletionTestGenerated.java @@ -753,6 +753,12 @@ public class JSBasicCompletionTestGenerated extends AbstractJSBasicCompletionTes doTest(fileName); } + @TestMetadata("ForType.kt") + public void testForType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/basic/common/annotations/ForType.kt"); + doTest(fileName); + } + @TestMetadata("FunctionAnnotation1.kt") public void testFunctionAnnotation1() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/basic/common/annotations/FunctionAnnotation1.kt"); diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JvmBasicCompletionTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JvmBasicCompletionTestGenerated.java index ef200e77153..ac94fef0750 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JvmBasicCompletionTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JvmBasicCompletionTestGenerated.java @@ -753,6 +753,12 @@ public class JvmBasicCompletionTestGenerated extends AbstractJvmBasicCompletionT doTest(fileName); } + @TestMetadata("ForType.kt") + public void testForType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/basic/common/annotations/ForType.kt"); + doTest(fileName); + } + @TestMetadata("FunctionAnnotation1.kt") public void testFunctionAnnotation1() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/basic/common/annotations/FunctionAnnotation1.kt");