mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
replacing infinite while loop with do-while doesn't need 'if'
This commit is contained in:
+18
-12
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.siyeh.ipp.whileloop.replace_while_with_do_while_loop;
|
||||
|
||||
class InfiniteLoop {
|
||||
|
||||
void m() {
|
||||
while<caret>((true)) {
|
||||
System.out.println(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -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));
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
class NoBraces {
|
||||
void m() {
|
||||
while<caret>(b()) System.out.println();
|
||||
}
|
||||
|
||||
boolean b() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
class NoBraces {
|
||||
void m() {
|
||||
if (b()) {
|
||||
do System.out.println();
|
||||
while (b());
|
||||
}
|
||||
}
|
||||
|
||||
boolean b() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.siyeh.ipp.whileloop.replace_while_with_do_while_loop;
|
||||
|
||||
class Regular {
|
||||
|
||||
void m() {
|
||||
<caret>while(b()) {
|
||||
System.out.println(1);
|
||||
}
|
||||
}
|
||||
|
||||
boolean b() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+16
@@ -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;
|
||||
}
|
||||
}
|
||||
+39
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user