From d751ddd81c34fca0ddce0fa09d16ce6e00f5b0e0 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 22 Dec 2014 13:08:12 +0100 Subject: [PATCH] non-code anno gutter icons: add an explanation --- ...ExternalAnnotationsLineMarkerProvider.java | 43 +++++++++++++------ .../BytecodeAnalysisIntegrationTest.java | 6 ++- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/ExternalAnnotationsLineMarkerProvider.java b/java/java-impl/src/com/intellij/codeInsight/ExternalAnnotationsLineMarkerProvider.java index b4f318de8e4b..33ca812d78d2 100644 --- a/java/java-impl/src/com/intellij/codeInsight/ExternalAnnotationsLineMarkerProvider.java +++ b/java/java-impl/src/com/intellij/codeInsight/ExternalAnnotationsLineMarkerProvider.java @@ -38,6 +38,7 @@ import com.intellij.psi.*; import com.intellij.psi.util.PsiUtilCore; import com.intellij.ui.awt.RelativePoint; import com.intellij.util.Function; +import com.intellij.util.containers.ContainerUtil; import com.intellij.xml.util.XmlStringUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -50,7 +51,24 @@ public class ExternalAnnotationsLineMarkerProvider implements LineMarkerProvider private static final Function ourTooltipProvider = new Function() { @Override public String fun(PsiElement nameIdentifier) { - return XmlStringUtil.wrapInHtml(JavaDocInfoGenerator.generateSignature(nameIdentifier.getParent())); + PsiModifierListOwner owner = (PsiModifierListOwner)nameIdentifier.getParent(); + + boolean hasInferred = false; + boolean hasExternal = false; + for (PsiAnnotation annotation : findSignatureNonCodeAnnotations(owner)) { + hasExternal |= AnnotationUtil.isExternalAnnotation(annotation); + hasInferred |= AnnotationUtil.isInferredAnnotation(annotation); + } + + String header; + if (hasInferred && hasExternal) { + header = "External and inferred"; + } else if (hasInferred) { + header = "Inferred"; + } else { + header = "External"; + } + return XmlStringUtil.wrapInHtml(header + " annotations available. Full signature:

\n" + JavaDocInfoGenerator.generateSignature(owner)); } }; @@ -65,7 +83,7 @@ public class ExternalAnnotationsLineMarkerProvider implements LineMarkerProvider PsiElement nameIdentifier = ((PsiNameIdentifierOwner)owner).getNameIdentifier(); if (nameIdentifier == null || !nameIdentifier.getTextRange().equals(element.getTextRange())) return null; - if (!shouldShowSignature((PsiModifierListOwner)owner)) { + if (findSignatureNonCodeAnnotations((PsiModifierListOwner)owner).isEmpty()) { return null; } @@ -76,38 +94,35 @@ public class ExternalAnnotationsLineMarkerProvider implements LineMarkerProvider GutterIconRenderer.Alignment.RIGHT); } - private static boolean shouldShowSignature(PsiModifierListOwner owner) { - if (hasNonCodeAnnotations(owner)) { - return true; - } + private static List findSignatureNonCodeAnnotations(PsiModifierListOwner owner) { + List result = ContainerUtil.newArrayList(findOwnNonCodeAnnotations(owner)); if (owner instanceof PsiMethod) { for (PsiParameter parameter : ((PsiMethod)owner).getParameterList().getParameters()) { - if (hasNonCodeAnnotations(parameter)) { - return true; - } + result.addAll(findOwnNonCodeAnnotations(parameter)); } } - return false; + return result; } - private static boolean hasNonCodeAnnotations(@NotNull PsiModifierListOwner element) { + private static List findOwnNonCodeAnnotations(@NotNull PsiModifierListOwner element) { + List result = ContainerUtil.newArrayList(); Project project = element.getProject(); PsiAnnotation[] externalAnnotations = ExternalAnnotationsManager.getInstance(project).findExternalAnnotations(element); if (externalAnnotations != null) { for (PsiAnnotation annotation : externalAnnotations) { if (isVisibleAnnotation(annotation)) { - return true; + result.add(annotation); } } } for (PsiAnnotation annotation : InferredAnnotationsManager.getInstance(project).findInferredAnnotations(element)) { if (isVisibleAnnotation(annotation)) { - return true; + result.add(annotation); } } - return false; + return result; } private static boolean isVisibleAnnotation(@NotNull PsiAnnotation annotation) { diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisIntegrationTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisIntegrationTest.java index 8a7340c76093..c3d5bc8cafd2 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisIntegrationTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/bytecodeAnalysis/BytecodeAnalysisIntegrationTest.java @@ -132,14 +132,16 @@ public class BytecodeAnalysisIntegrationTest extends JavaCodeInsightFixtureTestC public void testInferredAnnoGutter() { setUpLibraries(); openDecompiledClass("org.apache.velocity.util.ExceptionUtils"); - checkHasGutter("@Contract("null,_,_->null") \n" + + checkHasGutter("Inferred annotations available. Full signature:

\n" + + "@Contract("null,_,_->null") \n" + "public static Throwable createWithCause("); } public void testExternalAnnoGutter() { setUpExternalUpAnnotations(); openDecompiledClass("java.lang.Boolean"); - checkHasGutter("@org.jetbrains.annotations.Contract("null->false") \n" + + checkHasGutter("External and inferred annotations available. Full signature:

\n" + + "@org.jetbrains.annotations.Contract("null->false") \n" + "private static boolean toBoolean(@org.jetbrains.annotations.Nullable String var0)"); }