convert to nested ifs: treat nested or-/parenthesis- expr simple; include return statements on top level and expr only (IDEA-126049)

This commit is contained in:
Anna Kozlova
2014-06-16 17:26:26 +04:00
parent faa4b30196
commit 2264ee6dd2
8 changed files with 81 additions and 7 deletions
@@ -62,7 +62,7 @@ public class ConvertToNestedIfIntention extends Intention {
if (returnValue == null || ErrorUtil.containsDeepError(returnValue)) {
return;
}
final String newStatementText = buildIf(returnValue, new StringBuilder()).toString();
final String newStatementText = buildIf(returnValue, true, new StringBuilder()).toString();
final Project project = returnStatement.getProject();
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
final PsiBlockStatement blockStatement = (PsiBlockStatement)elementFactory.createStatementFromText("{" + newStatementText + "}", returnStatement);
@@ -73,23 +73,23 @@ public class ConvertToNestedIfIntention extends Intention {
PsiReplacementUtil.replaceStatement(returnStatement, "return false;");
}
private static StringBuilder buildIf(@Nullable PsiExpression expression, StringBuilder out) {
private static StringBuilder buildIf(@Nullable PsiExpression expression, boolean top, StringBuilder out) {
if (expression instanceof PsiPolyadicExpression) {
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)expression;
final PsiExpression[] operands = polyadicExpression.getOperands();
final IElementType tokenType = polyadicExpression.getOperationTokenType();
if (JavaTokenType.ANDAND.equals(tokenType)) {
for (PsiExpression operand : operands) {
buildIf(operand, out);
buildIf(operand, false, out);
}
if (!StringUtil.endsWith(out, "return true;")) {
if (top && !StringUtil.endsWith(out, "return true;")) {
out.append("return true;");
}
return out;
}
else if (JavaTokenType.OROR.equals(tokenType)) {
else if (top && JavaTokenType.OROR.equals(tokenType)) {
for (PsiExpression operand : operands) {
buildIf(operand, out);
buildIf(operand, false, out);
if (!StringUtil.endsWith(out, "return true;")) {
out.append("return true;");
}
@@ -99,7 +99,7 @@ public class ConvertToNestedIfIntention extends Intention {
}
else if (expression instanceof PsiParenthesizedExpression) {
final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)expression;
buildIf(parenthesizedExpression.getExpression(), out);
buildIf(parenthesizedExpression.getExpression(), top, out);
return out;
}
if (expression != null) {
@@ -0,0 +1,7 @@
package com.siyeh.ipp.trivialif.convert_to_nested_if;
public class X {
boolean f(boolean a, boolean b, boolean c, boolean d) {
return a |<caret>| (b && c) || d;
}
}
@@ -0,0 +1,10 @@
package com.siyeh.ipp.trivialif.convert_to_nested_if;
public class X {
boolean f(boolean a, boolean b, boolean c, boolean d) {
if (a) return true;
if (b) if (c) return true;
if (d) return true;
return false;
}
}
@@ -0,0 +1,7 @@
package com.siyeh.ipp.trivialif.convert_to_nested_if;
public class X {
boolean f(boolean a, boolean b, boolean c, boolean d) {
return a &<caret>& (b || c) && d;
}
}
@@ -0,0 +1,8 @@
package com.siyeh.ipp.trivialif.convert_to_nested_if;
public class X {
boolean f(boolean a, boolean b, boolean c, boolean d) {
if (a) if (b || c) if (d) return true;
return false;
}
}
@@ -0,0 +1,19 @@
package com.siyeh.ipp.trivialif.convert_to_nested_if;
public class X {
public static boolean foo(double a, double b, double c)
{
// the following return statement is converted by "Convert to multiple 'ifs'" (on the second &&) to the below, incorrect if-then-else
return (a > c && a < b) &<caret>& !bar1(a) && !bar2(a);
}
private static boolean bar1(double a)
{
return true;
}
private static boolean bar2(double a)
{
return true;
}
}
@@ -0,0 +1,20 @@
package com.siyeh.ipp.trivialif.convert_to_nested_if;
public class X {
public static boolean foo(double a, double b, double c)
{
// the following return statement is converted by "Convert to multiple 'ifs'" (on the second &&) to the below, incorrect if-then-else
if (a > c) if (a < b) if (!bar1(a)) if (!bar2(a)) return true;
return false;
}
private static boolean bar1(double a)
{
return true;
}
private static boolean bar2(double a)
{
return true;
}
}
@@ -22,6 +22,9 @@ public class ConvertToNestedIfIntentionTest extends IPPTestCase {
public void testNested() { doTest(); }
public void testStaircase() { doTest(); }
public void testStaircaseWithParenthesis() { doTest(); }
public void testStaircaseWithOrInside() { doTest(); }
public void testOrStaircaseInside() { doTest(); }
public void testOneLevelStaircase() { assertIntentionNotAvailable(); }
public void testAndOrMixed() { doTest(); }