diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties index eae16b25f18f..3d658f098b0e 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/GroovyInspectionBundle.properties @@ -107,4 +107,6 @@ target.0.does.not.exist=Target ''{0}'' does not exist target.annotation.is.unused=@Target is unused change.lvalue.type=Change variable ''{0}'' type to ''{1}'' replace.qualified.name.with.import=Replace qualified name with import -highlight.assignments.from.void=Highlight assignments from void type \ No newline at end of file +highlight.assignments.from.void=Highlight assignments from void type +comments.count.as.content=Comments count as content +ignore.when.catch.parameter.is.named.ignore.or.ignored=Ignore when catch parameter is named ignore or ignored \ No newline at end of file diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/exception/GroovyEmptyCatchBlockInspection.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/exception/GroovyEmptyCatchBlockInspection.java index 231bc9086d79..50ff0bc6bc20 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/exception/GroovyEmptyCatchBlockInspection.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/exception/GroovyEmptyCatchBlockInspection.java @@ -18,16 +18,26 @@ package org.jetbrains.plugins.groovy.codeInspection.exception; import com.intellij.codeInsight.daemon.impl.quickfix.RenameElementFix; import com.intellij.codeInspection.LocalQuickFix; import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; +import com.intellij.psi.PsiComment; +import com.intellij.psi.PsiElement; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.codeInspection.BaseInspection; import org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor; +import org.jetbrains.plugins.groovy.codeInspection.GroovyInspectionBundle; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrCatchClause; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock; import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter; +import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil; + +import javax.swing.*; public class GroovyEmptyCatchBlockInspection extends BaseInspection { + public boolean myIgnore = true; + public boolean myCountCommentsAsContent = true; @Nls @NotNull @@ -41,11 +51,21 @@ public class GroovyEmptyCatchBlockInspection extends BaseInspection { return "Empty 'catch' block"; } + @NotNull public BaseInspectionVisitor buildVisitor() { return new Visitor(); } - private static class Visitor extends BaseInspectionVisitor { + @Nullable + @Override + public JComponent createOptionsPanel() { + MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this); + panel.addCheckbox(GroovyInspectionBundle.message("comments.count.as.content"), "myCountCommentsAsContent"); + panel.addCheckbox(GroovyInspectionBundle.message("ignore.when.catch.parameter.is.named.ignore.or.ignored"), "myIgnore"); + return panel; + } + + private class Visitor extends BaseInspectionVisitor { public void visitCatchClause(GrCatchClause catchClause) { super.visitCatchClause(catchClause); @@ -56,15 +76,29 @@ public class GroovyEmptyCatchBlockInspection extends BaseInspection { final GrParameter parameter = catchClause.getParameter(); if (parameter == null) return; - if (GrExceptionUtil.ignore(parameter)) return; + if (myIgnore && GrExceptionUtil.ignore(parameter)) return; - final LocalQuickFix[] fixes = {new RenameElementFix(parameter, "ignored")}; + final LocalQuickFix[] fixes = myIgnore + ? new RenameElementFix[]{new RenameElementFix(parameter, "ignored")} + : LocalQuickFix.EMPTY_ARRAY; registerError(catchClause.getFirstChild(), "Empty '#ref' block #loc", fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING); } - private static boolean isEmpty(@NotNull GrOpenBlock body) { + private boolean isEmpty(@NotNull GrOpenBlock body) { final GrStatement[] statements = body.getStatements(); - return statements.length == 0; + if (statements.length != 0) return false; + + if (myCountCommentsAsContent) { + final PsiElement brace = body.getLBrace(); + if (brace != null) { + final PsiElement next = PsiUtil.skipWhitespaces(brace.getNextSibling(), true); + if (next instanceof PsiComment) { + return false; + } + } + } + + return true; } } } \ No newline at end of file diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.groovy index 9387d0df31a0..2b9ecad11444 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.groovy @@ -23,6 +23,7 @@ import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialIfInspec import org.jetbrains.plugins.groovy.codeInspection.control.GroovyUnnecessaryContinueInspection import org.jetbrains.plugins.groovy.codeInspection.control.GroovyUnnecessaryReturnInspection import org.jetbrains.plugins.groovy.codeInspection.declaration.GrMethodMayBeStaticInspection +import org.jetbrains.plugins.groovy.codeInspection.exception.GroovyEmptyCatchBlockInspection import org.jetbrains.plugins.groovy.codeInspection.metrics.GroovyOverlyLongMethodInspection import org.jetbrains.plugins.groovy.codeInspection.noReturnMethod.MissingReturnInspection import org.jetbrains.plugins.groovy.codeInspection.threading.GroovyUnconditionalWaitInspection @@ -316,4 +317,34 @@ for(i in []) { ''', GroovyUnnecessaryContinueInspection) } + void testEmptyCatchBlock1() { + testHighlighting(''' +try{} catch(IOException e) {} +try{} catch(IOException ignored) {} +try{} catch(IOException ignore) {} +try{} catch(IOException e) {/*comment*/} +''', GroovyEmptyCatchBlockInspection) + } + + void testEmptyCatchBlock2() { + GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection() + inspection.myIgnore = false + myFixture.enableInspections(inspection) + testHighlighting('try{} catch(IOException ignored) {}') + } + + void testEmptyCatchBlock3() { + GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection() + inspection.myIgnore = false + myFixture.enableInspections(inspection) + testHighlighting('try{} catch(IOException ignored) {}') + } + + void testEmptyCatchBlock4() { + GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection() + inspection.myCountCommentsAsContent = false + myFixture.enableInspections(inspection) + testHighlighting('try{} catch(IOException e) {/*comment*/}') + } + }