diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ReplaceWhileLoopWithDoWhileLoopIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ReplaceWhileLoopWithDoWhileLoopIntention.java index 7fd5b1bd247a..ee308bdf58f4 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ReplaceWhileLoopWithDoWhileLoopIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/whileloop/ReplaceWhileLoopWithDoWhileLoopIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2007 Bas Leijdekkers + * Copyright 2006-2013 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,10 @@ package com.siyeh.ipp.whileloop; import com.intellij.psi.*; -import com.intellij.util.IncorrectOperationException; +import com.siyeh.ig.psiutils.BoolUtils; import com.siyeh.ipp.base.Intention; import com.siyeh.ipp.base.PsiElementPredicate; +import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; public class ReplaceWhileLoopWithDoWhileLoopIntention extends Intention { @@ -28,20 +29,22 @@ public class ReplaceWhileLoopWithDoWhileLoopIntention extends Intention { return new WhileLoopPredicate(); } - protected void processIntention(@NotNull PsiElement element) - throws IncorrectOperationException { - final PsiWhileStatement whileStatement = - (PsiWhileStatement)element.getParent(); + protected void processIntention(@NotNull PsiElement element) { + final PsiWhileStatement whileStatement = (PsiWhileStatement)element.getParent(); 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()); + final boolean infiniteLoop = BoolUtils.isTrue(condition); + @NonNls final StringBuilder doWhileStatementText = new StringBuilder(); + if (!infiniteLoop) { + doWhileStatementText.append("if("); + if (condition != null) { + doWhileStatementText.append(condition.getText()); + } + doWhileStatementText.append(") {\n"); } - doWhileStatementText.append(") {\n"); if (body instanceof PsiBlockStatement) { doWhileStatementText.append("do {"); final PsiBlockStatement blockStatement = (PsiBlockStatement)body; @@ -56,13 +59,16 @@ public class ReplaceWhileLoopWithDoWhileLoopIntention extends Intention { doWhileStatementText.append('}'); } else if (body != null) { - doWhileStatementText.append(body.getText()); + doWhileStatementText.append("do ").append(body.getText()).append('\n'); } doWhileStatementText.append("while("); if (condition != null) { doWhileStatementText.append(condition.getText()); } - doWhileStatementText.append(");\n}"); + doWhileStatementText.append(");"); + if (!infiniteLoop) { + doWhileStatementText.append("\n}"); + } replaceStatement(doWhileStatementText.toString(), whileStatement); } } diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/InfiniteLoop.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/InfiniteLoop.java new file mode 100644 index 000000000000..ccba421e67b8 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/InfiniteLoop.java @@ -0,0 +1,10 @@ +package com.siyeh.ipp.whileloop.replace_while_with_do_while_loop; + +class InfiniteLoop { + + void m() { + while((true)) { + System.out.println(1); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/InfiniteLoop_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/InfiniteLoop_after.java new file mode 100644 index 000000000000..efe03a688115 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/InfiniteLoop_after.java @@ -0,0 +1,10 @@ +package com.siyeh.ipp.whileloop.replace_while_with_do_while_loop; + +class InfiniteLoop { + + void m() { + do { + System.out.println(1); + } while ((true)); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/NoBraces.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/NoBraces.java new file mode 100644 index 000000000000..155799ddff8e --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/NoBraces.java @@ -0,0 +1,9 @@ +class NoBraces { + void m() { + while(b()) System.out.println(); + } + + boolean b() { + return true; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/NoBraces_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/NoBraces_after.java new file mode 100644 index 000000000000..8ac5fe02103e --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/NoBraces_after.java @@ -0,0 +1,12 @@ +class NoBraces { + void m() { + if (b()) { + do System.out.println(); + while (b()); + } + } + + boolean b() { + return true; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/Regular.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/Regular.java new file mode 100644 index 000000000000..3b9ba309560c --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/Regular.java @@ -0,0 +1,14 @@ +package com.siyeh.ipp.whileloop.replace_while_with_do_while_loop; + +class Regular { + + void m() { + while(b()) { + System.out.println(1); + } + } + + boolean b() { + return true; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/Regular_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/Regular_after.java new file mode 100644 index 000000000000..ae5b0ffbfdfc --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/whileloop/replace_while_with_do_while_loop/Regular_after.java @@ -0,0 +1,16 @@ +package com.siyeh.ipp.whileloop.replace_while_with_do_while_loop; + +class Regular { + + void m() { + if (b()) { + do { + System.out.println(1); + } while (b()); + } + } + + boolean b() { + return true; + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/whileloop/ReplaceWhileLoopWithDoWhileLoopIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/whileloop/ReplaceWhileLoopWithDoWhileLoopIntentionTest.java new file mode 100644 index 000000000000..106b56ad93ac --- /dev/null +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/whileloop/ReplaceWhileLoopWithDoWhileLoopIntentionTest.java @@ -0,0 +1,39 @@ +/* + * 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.ipp.whileloop; + +import com.siyeh.IntentionPowerPackBundle; +import com.siyeh.ipp.IPPTestCase; + +/** + * @author Bas Leijdekkers + */ +public class ReplaceWhileLoopWithDoWhileLoopIntentionTest extends IPPTestCase { + + public void testInfiniteLoop() { doTest(); } + public void testRegular() { doTest(); } + public void testNoBraces() { doTest(); } + + @Override + protected String getRelativePath() { + return "whileloop/replace_while_with_do_while_loop"; + } + + @Override + protected String getIntentionName() { + return IntentionPowerPackBundle.message("replace.while.loop.with.do.while.loop.intention.name"); + } +}