From 10fbf8c3d4cf7c796141aad069fa386850b76cac Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 8 Jun 2007 15:48:51 +0400 Subject: [PATCH] intention part of IDEADEV-17531 --- plugins/IntentionPowerPak/src/META-INF/plugin.xml | 8 ++- .../com/siyeh/IntentionPowerPackBundle.properties | 2 + .../src/com/siyeh/ipp/base/Intention.java | 9 ++- .../DoWhileLoopPredicate.java | 21 ++++--- ...tWhileLoopConditionToIfStatementIntention.java} | 65 ++++++++++++---------- .../ReplaceDoWhileLoopWithWhileLoopIntention.java | 4 +- .../ReplaceWhileLoopWithDoWhileLoopIntention.java | 10 ++-- .../{forloop => whileloop}/WhileLoopPredicate.java | 13 +++-- .../ExtractIncrementIntention/after.java.template | 6 -- .../ExtractIncrementIntention/before.java.template | 5 -- .../after.java.template | 12 ++++ .../before.java.template | 4 +- .../description.html | 8 +++ .../after.java.template | 2 +- .../before.java.template | 2 +- 15 files changed, 100 insertions(+), 71 deletions(-) rename plugins/IntentionPowerPak/src/com/siyeh/ipp/{forloop => whileloop}/DoWhileLoopPredicate.java (67%) copy plugins/IntentionPowerPak/src/com/siyeh/ipp/{forloop/ReplaceWhileLoopWithDoWhileLoopIntention.java => whileloop/ExtractWhileLoopConditionToIfStatementIntention.java} (50%) rename plugins/IntentionPowerPak/src/com/siyeh/ipp/{forloop => whileloop}/ReplaceDoWhileLoopWithWhileLoopIntention.java (98%) rename plugins/IntentionPowerPak/src/com/siyeh/ipp/{forloop => whileloop}/ReplaceWhileLoopWithDoWhileLoopIntention.java (96%) rename plugins/IntentionPowerPak/src/com/siyeh/ipp/{forloop => whileloop}/WhileLoopPredicate.java (78%) delete mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ExtractIncrementIntention/after.java.template delete mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ExtractIncrementIntention/before.java.template create mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ExtractWhileLoopConditionToIfStatementIntention/after.java.template copy plugins/IntentionPowerPak/src/intentionDescriptions/{ReplaceWhileLoopWithDoWhileLoopIntention => ExtractWhileLoopConditionToIfStatementIntention}/before.java.template (56%) create mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/ExtractWhileLoopConditionToIfStatementIntention/description.html diff --git a/plugins/IntentionPowerPak/src/META-INF/plugin.xml b/plugins/IntentionPowerPak/src/META-INF/plugin.xml index 1816411153..d56d24cd42 100644 --- a/plugins/IntentionPowerPak/src/META-INF/plugin.xml +++ b/plugins/IntentionPowerPak/src/META-INF/plugin.xml @@ -250,11 +250,15 @@ intention.category.control.flow - com.siyeh.ipp.forloop.ReplaceWhileLoopWithDoWhileLoopIntention + com.siyeh.ipp.whileloop.ReplaceWhileLoopWithDoWhileLoopIntention intention.category.control.flow - com.siyeh.ipp.forloop.ReplaceDoWhileLoopWithWhileLoopIntention + com.siyeh.ipp.whileloop.ReplaceDoWhileLoopWithWhileLoopIntention + intention.category.control.flow + + + com.siyeh.ipp.whileloop.ExtractWhileLoopConditionToIfStatementIntention intention.category.control.flow diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties index 3819d9b2bb..943750c5eb 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties +++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties @@ -113,6 +113,8 @@ convert.catch.to.throws.intention.name=Replace Catch Section with Throws Declara convert.catch.to.throws.intention.family.name=Replace Catch Section with Throws Declaration wrap.vararg.arguments.with.explicit.array.intention.name=Wrap Vararg Arguments with Explicit Array Creation wrap.vararg.arguments.with.explicit.array.intention.family.name=Wrap Vararg Arguments with Explicit Array Creation +extract.while.loop.condition.to.if.statement.intention.name=Extract Condition to Internal If Statement +extract.while.loop.condition.to.if.statement.intention.family.name=Extract While Loop Condition to Internal If Statement #hand made demorgans.intention.name1=Replace '\\&\\&' with '||' diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/base/Intention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/base/Intention.java index fea227d9a6..72817528ca 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/base/Intention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/base/Intention.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2006 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,7 +64,7 @@ public abstract class Intention extends PsiElementBaseIntentionAction { final PsiManager mgr = expression.getManager(); final PsiElementFactory factory = mgr.getElementFactory(); final PsiExpression newCall = - factory.createExpressionFromText(newExpression, null); + factory.createExpressionFromText(newExpression, expression); final PsiElement insertedElement = expression.replace(newCall); final CodeStyleManager codeStyleManager = mgr.getCodeStyleManager(); codeStyleManager.reformat(insertedElement); @@ -76,7 +76,6 @@ public abstract class Intention extends PsiElementBaseIntentionAction { throws IncorrectOperationException{ final PsiManager manager = expression.getManager(); final PsiElementFactory factory = manager.getElementFactory(); - PsiExpression expressionToReplace = expression; final String newExpressionText = newExpression.getText(); final String expString; @@ -102,7 +101,7 @@ public abstract class Intention extends PsiElementBaseIntentionAction { } } final PsiExpression newCall = - factory.createExpressionFromText(expString, null); + factory.createExpressionFromText(expString, expression); assert expressionToReplace != null; final PsiElement insertedElement = expressionToReplace.replace(newCall); final CodeStyleManager codeStyleManager = manager.getCodeStyleManager(); @@ -125,7 +124,7 @@ public abstract class Intention extends PsiElementBaseIntentionAction { expString = "!(" + newExpression + ')'; } final PsiExpression newCall = - factory.createExpressionFromText(expString, null); + factory.createExpressionFromText(expString, expression); assert expressionToReplace != null; final PsiElement insertedElement = expressionToReplace.replace(newCall); final CodeStyleManager codeStyleManager = mgr.getCodeStyleManager(); diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/DoWhileLoopPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/DoWhileLoopPredicate.java similarity index 67% rename from plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/DoWhileLoopPredicate.java rename to plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/DoWhileLoopPredicate.java index 0f0b455188..8b7dbd874d 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/DoWhileLoopPredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/DoWhileLoopPredicate.java @@ -1,5 +1,5 @@ /* - * Copyright 2006 Bas Leijdekkers + * Copyright 2006-2007 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,12 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.siyeh.ipp.forloop; +package com.siyeh.ipp.whileloop; -import com.siyeh.ipp.base.PsiElementPredicate; -import com.siyeh.ipp.psiutils.ErrorUtil; -import com.intellij.psi.*; +import com.intellij.psi.JavaTokenType; +import com.intellij.psi.PsiDoWhileStatement; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiJavaToken; import com.intellij.psi.tree.IElementType; +import com.siyeh.ipp.base.PsiElementPredicate; class DoWhileLoopPredicate implements PsiElementPredicate { @@ -32,7 +34,12 @@ class DoWhileLoopPredicate implements PsiElementPredicate { return false; } final PsiElement parent = element.getParent(); - return parent instanceof PsiDoWhileStatement && - !ErrorUtil.containsError(parent); + if (!(parent instanceof PsiDoWhileStatement)) { + return false; + } + final PsiDoWhileStatement doWhileStatement = + (PsiDoWhileStatement)parent; + return !(doWhileStatement.getCondition() == null || + doWhileStatement.getBody() == null); } } \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceWhileLoopWithDoWhileLoopIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ExtractWhileLoopConditionToIfStatementIntention.java similarity index 50% copy from plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceWhileLoopWithDoWhileLoopIntention.java copy to plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ExtractWhileLoopConditionToIfStatementIntention.java index da0ed7a3a0..1e08baec4e 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceWhileLoopWithDoWhileLoopIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ExtractWhileLoopConditionToIfStatementIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2006 Bas Leijdekkers + * Copyright 2007 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,17 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.siyeh.ipp.forloop; +package com.siyeh.ipp.whileloop; -import com.siyeh.ipp.base.Intention; -import com.siyeh.ipp.base.PsiElementPredicate; import com.intellij.psi.*; +import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.util.IncorrectOperationException; +import com.siyeh.ipp.base.Intention; +import com.siyeh.ipp.base.PsiElementPredicate; import org.jetbrains.annotations.NotNull; -import java.awt.event.KeyEvent; - -public class ReplaceWhileLoopWithDoWhileLoopIntention extends Intention { +public class ExtractWhileLoopConditionToIfStatementIntention extends Intention { @NotNull protected PsiElementPredicate getElementPredicate() { @@ -37,33 +36,41 @@ public class ReplaceWhileLoopWithDoWhileLoopIntention extends Intention { if (whileStatement == null) { return; } - final PsiStatement body = whileStatement.getBody(); - final StringBuilder doWhileStatementText = new StringBuilder("if("); final PsiExpression condition = whileStatement.getCondition(); - if (condition != null) { - doWhileStatementText.append(condition.getText()); + if (condition == null) { + return; } - doWhileStatementText.append(") {\n"); + final String conditionText = condition.getText(); + final PsiManager manager = whileStatement.getManager(); + final PsiElementFactory factory = + manager.getElementFactory(); + final PsiExpression newCondition = + factory.createExpressionFromText("true", whileStatement); + condition.replace(newCondition); + final PsiStatement body = whileStatement.getBody(); + final String ifStatementText = "if (!(" + conditionText + ")) break;"; + final PsiStatement ifStatement = + factory.createStatementFromText(ifStatementText, + whileStatement); + + final PsiElement newElement; if (body instanceof PsiBlockStatement) { - doWhileStatementText.append("do {"); final PsiBlockStatement blockStatement = (PsiBlockStatement)body; final PsiCodeBlock codeBlock = blockStatement.getCodeBlock(); - final PsiElement[] children = codeBlock.getChildren(); - if (children.length > 2) { - for (int i = 1; i < children.length - 1; i++) { - final PsiElement child = children[i]; - doWhileStatementText.append(child.getText()); - } - } - doWhileStatementText.append('}'); + final PsiElement bodyElement = codeBlock.getFirstBodyElement(); + newElement = codeBlock.addBefore(ifStatement, + bodyElement); } else if (body != null) { - doWhileStatementText.append(body.getText()); - } - doWhileStatementText.append("while("); - if (condition != null) { - doWhileStatementText.append(condition.getText()); + final PsiBlockStatement blockStatement = + (PsiBlockStatement)factory.createStatementFromText("{}", + whileStatement); + final PsiCodeBlock codeBlock = blockStatement.getCodeBlock(); + newElement = codeBlock.add(ifStatement); + codeBlock.add(body); + } else { + return; } - doWhileStatementText.append(");\n}"); - replaceStatement(doWhileStatementText.toString(), whileStatement); + final CodeStyleManager codeStyleManager = manager.getCodeStyleManager(); + codeStyleManager.reformat(newElement); } -} +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceDoWhileLoopWithWhileLoopIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ReplaceDoWhileLoopWithWhileLoopIntention.java similarity index 98% rename from plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceDoWhileLoopWithWhileLoopIntention.java rename to plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ReplaceDoWhileLoopWithWhileLoopIntention.java index 39d0269a06..93119756d9 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceDoWhileLoopWithWhileLoopIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ReplaceDoWhileLoopWithWhileLoopIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2006 Bas Leijdekkers + * Copyright 2006-2007 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.siyeh.ipp.forloop; +package com.siyeh.ipp.whileloop; import com.intellij.psi.*; import com.intellij.util.IncorrectOperationException; diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceWhileLoopWithDoWhileLoopIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ReplaceWhileLoopWithDoWhileLoopIntention.java similarity index 96% rename from plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceWhileLoopWithDoWhileLoopIntention.java rename to plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ReplaceWhileLoopWithDoWhileLoopIntention.java index da0ed7a3a0..11d8331472 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/ReplaceWhileLoopWithDoWhileLoopIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ReplaceWhileLoopWithDoWhileLoopIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2006 Bas Leijdekkers + * Copyright 2006-2007 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,16 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.siyeh.ipp.forloop; +package com.siyeh.ipp.whileloop; -import com.siyeh.ipp.base.Intention; -import com.siyeh.ipp.base.PsiElementPredicate; import com.intellij.psi.*; import com.intellij.util.IncorrectOperationException; +import com.siyeh.ipp.base.Intention; +import com.siyeh.ipp.base.PsiElementPredicate; import org.jetbrains.annotations.NotNull; -import java.awt.event.KeyEvent; - public class ReplaceWhileLoopWithDoWhileLoopIntention extends Intention { @NotNull diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/WhileLoopPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/WhileLoopPredicate.java similarity index 78% rename from plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/WhileLoopPredicate.java rename to plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/WhileLoopPredicate.java index 9a4dc8c041..64e27cf973 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/forloop/WhileLoopPredicate.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/WhileLoopPredicate.java @@ -1,5 +1,5 @@ /* - * Copyright 2006 Bas Leijdekkers + * Copyright 2006-2007 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.siyeh.ipp.forloop; +package com.siyeh.ipp.whileloop; import com.intellij.psi.JavaTokenType; import com.intellij.psi.PsiElement; @@ -21,7 +21,6 @@ import com.intellij.psi.PsiJavaToken; import com.intellij.psi.PsiWhileStatement; import com.intellij.psi.tree.IElementType; import com.siyeh.ipp.base.PsiElementPredicate; -import com.siyeh.ipp.psiutils.ErrorUtil; class WhileLoopPredicate implements PsiElementPredicate { @@ -35,7 +34,11 @@ class WhileLoopPredicate implements PsiElementPredicate { return false; } final PsiElement parent = element.getParent(); - return parent instanceof PsiWhileStatement && - !ErrorUtil.containsError(parent); + if (!(parent instanceof PsiWhileStatement)) { + return false; + } + final PsiWhileStatement whileStatement = (PsiWhileStatement)parent; + return !(whileStatement.getCondition() == null || + whileStatement.getBody() == null); } } \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractIncrementIntention/after.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractIncrementIntention/after.java.template deleted file mode 100644 index 24830dee7c..0000000000 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractIncrementIntention/after.java.template +++ /dev/null @@ -1,6 +0,0 @@ -public class X { - int f() { - System.out.println(i); - i++; - } -} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractIncrementIntention/before.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractIncrementIntention/before.java.template deleted file mode 100644 index 20b5f42e21..0000000000 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractIncrementIntention/before.java.template +++ /dev/null @@ -1,5 +0,0 @@ -public class X { - int f() { - System.out.println(i++); - } -} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractWhileLoopConditionToIfStatementIntention/after.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractWhileLoopConditionToIfStatementIntention/after.java.template new file mode 100644 index 0000000000..2591d5d807 --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractWhileLoopConditionToIfStatementIntention/after.java.template @@ -0,0 +1,12 @@ +public class X { + private boolean flag; + + void f() { + while(true) { + if (!flag) { + break; + } + System.out.println("looping"); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWhileLoopWithDoWhileLoopIntention/before.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractWhileLoopConditionToIfStatementIntention/before.java.template similarity index 56% copy from plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWhileLoopWithDoWhileLoopIntention/before.java.template copy to plugins/IntentionPowerPak/src/intentionDescriptions/ExtractWhileLoopConditionToIfStatementIntention/before.java.template index 39e8625ee5..d1fb724f1f 100644 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWhileLoopWithDoWhileLoopIntention/before.java.template +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractWhileLoopConditionToIfStatementIntention/before.java.template @@ -1,9 +1,9 @@ public class X { private boolean flag; - void f(String[] as) { + void f() { while(flag) { - System.out.println("looping"); + System.out.println("looping"); } } } \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractWhileLoopConditionToIfStatementIntention/description.html b/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractWhileLoopConditionToIfStatementIntention/description.html new file mode 100644 index 0000000000..7f80b21dca --- /dev/null +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/ExtractWhileLoopConditionToIfStatementIntention/description.html @@ -0,0 +1,8 @@ + + +This intention extracts the condition of a while +statement and places the negated condition in an if statement inside the while +loop. + + + diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWhileLoopWithDoWhileLoopIntention/after.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWhileLoopWithDoWhileLoopIntention/after.java.template index b5ce96d939..ff8ec663a4 100644 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWhileLoopWithDoWhileLoopIntention/after.java.template +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWhileLoopWithDoWhileLoopIntention/after.java.template @@ -1,7 +1,7 @@ public class X { private boolean flag; - void f(String[] as) { + void f() { if (flag) { do { System.out.println("looping"); diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWhileLoopWithDoWhileLoopIntention/before.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWhileLoopWithDoWhileLoopIntention/before.java.template index 39e8625ee5..c335771f89 100644 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWhileLoopWithDoWhileLoopIntention/before.java.template +++ b/plugins/IntentionPowerPak/src/intentionDescriptions/ReplaceWhileLoopWithDoWhileLoopIntention/before.java.template @@ -1,7 +1,7 @@ public class X { private boolean flag; - void f(String[] as) { + void f() { while(flag) { System.out.println("looping"); } -- 2.11.4.GIT