From 0bedaf8d49347eecb2ec55fe08cd6a29486f9cb0 Mon Sep 17 00:00:00 2001 From: anna Date: Mon, 21 Oct 2013 18:31:31 +0200 Subject: [PATCH] missed comments on Simplify (IDEA-114798) --- .../ig/controlflow/TrivialIfInspection.java | 7 +++ .../controlflow/trivialIf/Comments.after.java | 6 +++ .../controlflow/trivialIf/Comments.java | 10 ++++ .../controlflow/TrivialIfInspectionTest.java | 49 +++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/trivialIf/Comments.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/trivialIf/Comments.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/controlflow/TrivialIfInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/TrivialIfInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/TrivialIfInspection.java index 0a88a1213796..92b2e164d8a2 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/TrivialIfInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/TrivialIfInspection.java @@ -32,6 +32,8 @@ import com.siyeh.ig.psiutils.EquivalenceChecker; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; +import java.util.Collection; + public class TrivialIfInspection extends BaseInspection { @Override @@ -131,6 +133,11 @@ public class TrivialIfInspection extends BaseInspection { } final String conditionText = condition.getText(); @NonNls final String newStatement = "return " + conditionText + ';'; + final Collection comments = PsiTreeUtil.findChildrenOfType(statement, PsiComment.class); + final PsiElement parent = statement.getParent(); + for (PsiComment comment : comments) { + parent.addBefore(comment.copy(), statement); + } replaceStatement(statement, newStatement); } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/trivialIf/Comments.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/trivialIf/Comments.after.java new file mode 100644 index 000000000000..b1d10e8c7840 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/trivialIf/Comments.after.java @@ -0,0 +1,6 @@ +class Test { + public static boolean test(String key, String keyValue){ + // some comment goes here + return key != null && key.equals(keyValue); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/trivialIf/Comments.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/trivialIf/Comments.java new file mode 100644 index 000000000000..1c481776fab9 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/controlflow/trivialIf/Comments.java @@ -0,0 +1,10 @@ +class Test { + public static boolean test(String key, String keyValue){ + if(key != null && key.equals(keyValue)) { + // some comment goes here + return true; + } else { + return false; + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/controlflow/TrivialIfInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/controlflow/TrivialIfInspectionTest.java new file mode 100644 index 000000000000..013c1e8e68f0 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/controlflow/TrivialIfInspectionTest.java @@ -0,0 +1,49 @@ +/* + * Copyright 2000-2013 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.siyeh.ig.fixes.controlflow; + +import com.intellij.codeInsight.intention.IntentionAction; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.IGQuickFixesTestCase; +import com.siyeh.ig.controlflow.TrivialIfInspection; + +import java.util.List; + +public class TrivialIfInspectionTest extends IGQuickFixesTestCase { + + @Override + protected void setUp() throws Exception { + super.setUp(); + myFixture.enableInspections(new TrivialIfInspection()); + myRelativePath = "controlflow/trivialIf"; + } + + public void testComments() { + final String testName = getTestName(false); + myFixture.configureByFile(getRelativePath() + "/" + testName + ".java"); + final String message = InspectionGadgetsBundle.message("constant.conditional.expression.simplify.quickfix"); + final List actions = + myFixture.filterAvailableIntentions(message); + assertFalse("No actions available", actions.isEmpty()); + for (IntentionAction action : actions) { + if (action.getText().equals(message)) { + myFixture.launchAction(action); + myFixture.checkResultByFile(getRelativePath() + "/" + testName + ".after.java"); + break; + } + } + } +} \ No newline at end of file