From d1106ea8700fbb70563d6312321c37bcf1d09dd7 Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 29 Jul 2015 19:32:55 +0200 Subject: [PATCH] an ability to turn off gutter icons for annotations inferred from source (IDEA-135399) --- ...ExternalAnnotationsLineMarkerProvider.java | 30 +++++--- .../ToggleSourceInferredAnnotations.java | 76 +++++++++++++++++++ .../codeInsight/CodeInsightSettings.java | 2 + .../EditContractIntention/after.java.template | 2 +- .../after.java.template | 2 +- .../after.java.template | 8 ++ .../before.java.template | 8 ++ .../description.html | 6 ++ resources/src/META-INF/IdeaPlugin.xml | 5 ++ 9 files changed, 127 insertions(+), 12 deletions(-) create mode 100644 java/java-impl/src/com/intellij/codeInsight/ToggleSourceInferredAnnotations.java create mode 100644 resources-en/src/intentionDescriptions/ToggleSourceInferredAnnotations/after.java.template create mode 100644 resources-en/src/intentionDescriptions/ToggleSourceInferredAnnotations/before.java.template create mode 100644 resources-en/src/intentionDescriptions/ToggleSourceInferredAnnotations/description.html diff --git a/java/java-impl/src/com/intellij/codeInsight/ExternalAnnotationsLineMarkerProvider.java b/java/java-impl/src/com/intellij/codeInsight/ExternalAnnotationsLineMarkerProvider.java index 68c578eecdb9..b48698176fde 100644 --- a/java/java-impl/src/com/intellij/codeInsight/ExternalAnnotationsLineMarkerProvider.java +++ b/java/java-impl/src/com/intellij/codeInsight/ExternalAnnotationsLineMarkerProvider.java @@ -23,6 +23,7 @@ import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.codeInsight.intention.IntentionManager; import com.intellij.codeInsight.intention.impl.AddAnnotationIntention; import com.intellij.codeInsight.intention.impl.DeannotateIntentionAction; +import com.intellij.codeInsight.intention.impl.config.IntentionActionWrapper; import com.intellij.codeInsight.javadoc.JavaDocInfoGenerator; import com.intellij.codeInspection.dataFlow.EditContractIntention; import com.intellij.icons.AllIcons; @@ -58,7 +59,7 @@ public class ExternalAnnotationsLineMarkerProvider implements LineMarkerProvider boolean hasInferred = false; boolean hasExternal = false; - for (PsiAnnotation annotation : findSignatureNonCodeAnnotations(owner)) { + for (PsiAnnotation annotation : findSignatureNonCodeAnnotations(owner, true)) { hasExternal |= AnnotationUtil.isExternalAnnotation(annotation); hasInferred |= AnnotationUtil.isInferredAnnotation(annotation); } @@ -79,7 +80,8 @@ public class ExternalAnnotationsLineMarkerProvider implements LineMarkerProvider @Override public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) { PsiModifierListOwner owner = getAnnotationOwner(element); - if (owner == null || findSignatureNonCodeAnnotations(owner).isEmpty()) { + boolean includeSourceInferred = CodeInsightSettings.getInstance().SHOW_SOURCE_INFERRED_ANNOTATIONS; + if (owner == null || findSignatureNonCodeAnnotations(owner, includeSourceInferred).isEmpty()) { return null; } @@ -104,19 +106,19 @@ public class ExternalAnnotationsLineMarkerProvider implements LineMarkerProvider return (PsiModifierListOwner)owner; } - private static List findSignatureNonCodeAnnotations(PsiModifierListOwner owner) { - List result = ContainerUtil.newArrayList(findOwnNonCodeAnnotations(owner)); + static List findSignatureNonCodeAnnotations(PsiModifierListOwner owner, boolean includeSourceInferred) { + List result = ContainerUtil.newArrayList(findOwnNonCodeAnnotations(owner, includeSourceInferred)); if (owner instanceof PsiMethod) { for (PsiParameter parameter : ((PsiMethod)owner).getParameterList().getParameters()) { - result.addAll(findOwnNonCodeAnnotations(parameter)); + result.addAll(findOwnNonCodeAnnotations(parameter, includeSourceInferred)); } } return result; } - private static List findOwnNonCodeAnnotations(@NotNull PsiModifierListOwner element) { + private static List findOwnNonCodeAnnotations(@NotNull PsiModifierListOwner element, boolean includeSourceInferred) { List result = ContainerUtil.newArrayList(); Project project = element.getProject(); PsiAnnotation[] externalAnnotations = ExternalAnnotationsManager.getInstance(project).findExternalAnnotations(element); @@ -127,14 +129,20 @@ public class ExternalAnnotationsLineMarkerProvider implements LineMarkerProvider } } } - for (PsiAnnotation annotation : InferredAnnotationsManager.getInstance(project).findInferredAnnotations(element)) { - if (isVisibleAnnotation(annotation)) { - result.add(annotation); + if (includeSourceInferred || !isSourceCode(element)) { + for (PsiAnnotation annotation : InferredAnnotationsManager.getInstance(project).findInferredAnnotations(element)) { + if (isVisibleAnnotation(annotation)) { + result.add(annotation); + } } } return result; } + static boolean isSourceCode(PsiModifierListOwner element) { + return !(BaseExternalAnnotationsManager.preferCompiledElement(element) instanceof PsiCompiledElement); + } + private static boolean isVisibleAnnotation(@NotNull PsiAnnotation annotation) { PsiJavaCodeReferenceElement ref = annotation.getNameReferenceElement(); if (ref == null) return true; @@ -195,7 +203,9 @@ public class ExternalAnnotationsLineMarkerProvider implements LineMarkerProvider return action instanceof AddAnnotationIntention || action instanceof DeannotateIntentionAction || action instanceof EditContractIntention || - action instanceof MakeInferredAnnotationExplicit; + action instanceof ToggleSourceInferredAnnotations || + action instanceof MakeInferredAnnotationExplicit || + action instanceof IntentionActionWrapper && shouldShowInGutterPopup(((IntentionActionWrapper)action).getDelegate()); } } } diff --git a/java/java-impl/src/com/intellij/codeInsight/ToggleSourceInferredAnnotations.java b/java/java-impl/src/com/intellij/codeInsight/ToggleSourceInferredAnnotations.java new file mode 100644 index 000000000000..c106e038f7cc --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/ToggleSourceInferredAnnotations.java @@ -0,0 +1,76 @@ +/* + * Copyright 2000-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight; + +import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; +import com.intellij.codeInsight.intention.impl.BaseIntentionAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Condition; +import com.intellij.psi.PsiAnnotation; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiModifierListOwner; +import com.intellij.util.IncorrectOperationException; +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +import static com.intellij.codeInsight.ExternalAnnotationsLineMarkerProvider.*; + +/** + * @author peter + */ +public class ToggleSourceInferredAnnotations extends BaseIntentionAction { + + @Nls + @NotNull + @Override + public String getFamilyName() { + return "Show/Hide Annotations Inferred from Source Code"; + } + + @Override + public boolean isAvailable(@NotNull final Project project, Editor editor, PsiFile file) { + final PsiElement leaf = file.findElementAt(editor.getCaretModel().getOffset()); + final PsiModifierListOwner owner = getAnnotationOwner(leaf); + if (owner != null && isSourceCode(owner)) { + boolean hasSrcInferredAnnotation = ContainerUtil.or(findSignatureNonCodeAnnotations(owner, true), new Condition() { + @Override + public boolean value(PsiAnnotation annotation) { + return AnnotationUtil.isInferredAnnotation(annotation); + } + }); + if (hasSrcInferredAnnotation) { + setText((CodeInsightSettings.getInstance().SHOW_SOURCE_INFERRED_ANNOTATIONS ? "Hide" : "Show") + " annotations inferred from source code"); + return true; + } + } + + return false; + } + + @Override + public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + CodeInsightSettings.getInstance().SHOW_SOURCE_INFERRED_ANNOTATIONS = !CodeInsightSettings.getInstance().SHOW_SOURCE_INFERRED_ANNOTATIONS; + DaemonCodeAnalyzer.getInstance(project).restart(file); + } + + @Override + public boolean startInWriteAction() { + return false; + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/CodeInsightSettings.java b/platform/lang-impl/src/com/intellij/codeInsight/CodeInsightSettings.java index dc353cf3abf1..24d9d01a4c4c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/CodeInsightSettings.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/CodeInsightSettings.java @@ -86,6 +86,8 @@ public class CodeInsightSettings implements PersistentStateComponent, C public boolean SHOW_FULL_SIGNATURES_IN_PARAMETER_INFO = false; + public boolean SHOW_SOURCE_INFERRED_ANNOTATIONS = true; + @OptionTag private int SMART_BACKSPACE = SmartBackspaceMode.AUTOINDENT.ordinal(); diff --git a/resources-en/src/intentionDescriptions/EditContractIntention/after.java.template b/resources-en/src/intentionDescriptions/EditContractIntention/after.java.template index 0714ecfe997a..bcfdcb0b6407 100644 --- a/resources-en/src/intentionDescriptions/EditContractIntention/after.java.template +++ b/resources-en/src/intentionDescriptions/EditContractIntention/after.java.template @@ -2,7 +2,7 @@ import org.jetbrains.annotations.Contract; class A { - @Contract("any->null") + @Contract("_->null") Object getObject() { //do smth return null; diff --git a/resources-en/src/intentionDescriptions/MakeInferredAnnotationExplicit/after.java.template b/resources-en/src/intentionDescriptions/MakeInferredAnnotationExplicit/after.java.template index 0714ecfe997a..bcfdcb0b6407 100644 --- a/resources-en/src/intentionDescriptions/MakeInferredAnnotationExplicit/after.java.template +++ b/resources-en/src/intentionDescriptions/MakeInferredAnnotationExplicit/after.java.template @@ -2,7 +2,7 @@ import org.jetbrains.annotations.Contract; class A { - @Contract("any->null") + @Contract("_->null") Object getObject() { //do smth return null; diff --git a/resources-en/src/intentionDescriptions/ToggleSourceInferredAnnotations/after.java.template b/resources-en/src/intentionDescriptions/ToggleSourceInferredAnnotations/after.java.template new file mode 100644 index 000000000000..b852af59563a --- /dev/null +++ b/resources-en/src/intentionDescriptions/ToggleSourceInferredAnnotations/after.java.template @@ -0,0 +1,8 @@ +class A { + + // An icon for @Contract("_->null") not shown in the gutter, but still visible in the intention list + Object getObject() { + //do smth + return null; + } +} ] \ No newline at end of file diff --git a/resources-en/src/intentionDescriptions/ToggleSourceInferredAnnotations/before.java.template b/resources-en/src/intentionDescriptions/ToggleSourceInferredAnnotations/before.java.template new file mode 100644 index 000000000000..4593c704ded5 --- /dev/null +++ b/resources-en/src/intentionDescriptions/ToggleSourceInferredAnnotations/before.java.template @@ -0,0 +1,8 @@ +class A { + + // An icon for @Contract("_->null") shown in the gutter + Object getObject() { + //do smth + return null; + } +} ] \ No newline at end of file diff --git a/resources-en/src/intentionDescriptions/ToggleSourceInferredAnnotations/description.html b/resources-en/src/intentionDescriptions/ToggleSourceInferredAnnotations/description.html new file mode 100644 index 000000000000..550d8ca4cba0 --- /dev/null +++ b/resources-en/src/intentionDescriptions/ToggleSourceInferredAnnotations/description.html @@ -0,0 +1,6 @@ + + +Enable or disable '@' gutter icon where IntelliJ IDEA has inferred @Nullable/@NotNull/@Contract annotations for source code. + + + \ No newline at end of file diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 0056de562fd4..a8f8bc12d7e4 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -796,6 +796,11 @@ Java/Annotations MakeInferredAnnotationExplicit + + com.intellij.codeInsight.ToggleSourceInferredAnnotations + Java/Annotations + ToggleSourceInferredAnnotations + com.intellij.codeInsight.daemon.impl.quickfix.IterateOverIterableIntention Java/Control Flow