From ae06f67cd204137f64331c9ee108b2d8d9d6120c Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 12 Sep 2012 21:39:04 +0400 Subject: [PATCH] add suppress with comment for local variables (IDEA-90851) --- .../actions/SuppressByJavaCommentFix.java | 23 +++++--- .../actions/SuppressLocalWithCommentFix.java | 56 +++++++++++++++++++ .../codeInspection/SuppressManagerImpl.java | 1 + 3 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 java/java-impl/src/com/intellij/codeInsight/daemon/impl/actions/SuppressLocalWithCommentFix.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/actions/SuppressByJavaCommentFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/actions/SuppressByJavaCommentFix.java index 63e992bc6701..1db5a3981add 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/actions/SuppressByJavaCommentFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/actions/SuppressByJavaCommentFix.java @@ -45,7 +45,20 @@ public class SuppressByJavaCommentFix extends SuppressByCommentFix { final Editor editor, final PsiElement element, final PsiElement container) throws IncorrectOperationException { - boolean added = false; + PsiElement declaredElement = getElementToAnnotate(element, container); + if (declaredElement != null) { + SuppressFix.addSuppressAnnotation(project, editor, container, (PsiLocalVariable)declaredElement, myID); + } else { + suppressWithComment(project, editor, element, container); + } + } + + protected void suppressWithComment(Project project, Editor editor, PsiElement element, PsiElement container) { + super.createSuppression(project, editor, element, container); + } + + @Nullable + protected static PsiElement getElementToAnnotate(PsiElement element, PsiElement container) { if (container instanceof PsiDeclarationStatement && SuppressManager.getInstance().canHave15Suppressions(element)) { final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)container; final PsiElement[] declaredElements = declarationStatement.getDeclaredElements(); @@ -53,15 +66,11 @@ public class SuppressByJavaCommentFix extends SuppressByCommentFix { if (declaredElement instanceof PsiLocalVariable) { final PsiModifierList modifierList = ((PsiLocalVariable)declaredElement).getModifierList(); if (modifierList != null) { - SuppressFix.addSuppressAnnotation(project, editor, container, (PsiLocalVariable)declaredElement, myID); - added = true; - break; + return declaredElement; } } } } - if (!added) { - super.createSuppression(project, editor, element, container); - } + return null; } } diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/actions/SuppressLocalWithCommentFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/actions/SuppressLocalWithCommentFix.java new file mode 100644 index 000000000000..6f589aa79540 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/actions/SuppressLocalWithCommentFix.java @@ -0,0 +1,56 @@ +/* + * Copyright 2000-2012 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.daemon.impl.actions; + +import com.intellij.codeInsight.daemon.HighlightDisplayKey; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiElement; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * User: anna + */ +public class SuppressLocalWithCommentFix extends SuppressByJavaCommentFix { + public SuppressLocalWithCommentFix(HighlightDisplayKey key) { + super(key); + } + + @Nullable + @Override + protected PsiElement getContainer(PsiElement context) { + final PsiElement container = super.getContainer(context); + if (container != null) { + final PsiElement elementToAnnotate = getElementToAnnotate(context, container); + if (elementToAnnotate == null) return null; + } + return container; + } + + @Override + protected void createSuppression(Project project, Editor editor, PsiElement element, PsiElement container) + throws IncorrectOperationException { + suppressWithComment(project, editor, element, container); + } + + @NotNull + @Override + public String getText() { + return "Suppress for statement with comment"; + } +} diff --git a/java/java-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java b/java/java-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java index fa83649ba458..c53f7c702938 100644 --- a/java/java-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java +++ b/java/java-impl/src/com/intellij/codeInspection/SuppressManagerImpl.java @@ -55,6 +55,7 @@ public class SuppressManagerImpl extends SuppressManager { public SuppressIntentionAction[] createSuppressActions(@NotNull final HighlightDisplayKey displayKey) { return new SuppressIntentionAction[]{ new SuppressByJavaCommentFix(displayKey), + new SuppressLocalWithCommentFix(displayKey), new SuppressParameterFix(displayKey), new SuppressFix(displayKey), new SuppressForClassFix(displayKey),