mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
add necessary parentheses when simplifying if (IDEA-178828)
This commit is contained in:
+7
-7
@@ -92,7 +92,7 @@ public class BoolUtils {
|
||||
}
|
||||
if (expression instanceof PsiPolyadicExpression) {
|
||||
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)expression;
|
||||
IElementType tokenType = polyadicExpression.getOperationTokenType();
|
||||
final IElementType tokenType = polyadicExpression.getOperationTokenType();
|
||||
final PsiExpression[] operands = polyadicExpression.getOperands();
|
||||
if (ComparisonUtils.isComparison(polyadicExpression)) {
|
||||
final String negatedComparison = ComparisonUtils.getNegatedComparison(tokenType);
|
||||
@@ -121,23 +121,23 @@ public class BoolUtils {
|
||||
}
|
||||
if(tokenType.equals(JavaTokenType.ANDAND) || tokenType.equals(JavaTokenType.OROR)) {
|
||||
final String targetToken;
|
||||
final boolean needParenthesis;
|
||||
final int newPrecedence;
|
||||
if (tokenType.equals(JavaTokenType.ANDAND)) {
|
||||
targetToken = "||";
|
||||
needParenthesis = ParenthesesUtils.OR_PRECEDENCE > precedence;
|
||||
newPrecedence = ParenthesesUtils.OR_PRECEDENCE;
|
||||
}
|
||||
else {
|
||||
targetToken = "&&";
|
||||
needParenthesis = ParenthesesUtils.AND_PRECEDENCE > precedence;
|
||||
newPrecedence = ParenthesesUtils.AND_PRECEDENCE;
|
||||
}
|
||||
Function<PsiElement, String> replacer = child -> {
|
||||
final Function<PsiElement, String> replacer = child -> {
|
||||
if (child instanceof PsiExpression) {
|
||||
return getNegatedExpressionText((PsiExpression)child);
|
||||
return getNegatedExpressionText((PsiExpression)child, newPrecedence);
|
||||
}
|
||||
return child instanceof PsiJavaToken ? targetToken : child.getText();
|
||||
};
|
||||
final String join = StringUtil.join(polyadicExpression.getChildren(), replacer, "");
|
||||
return needParenthesis ? '(' + join + ')' : join;
|
||||
return (newPrecedence > precedence) ? '(' + join + ')' : join;
|
||||
}
|
||||
}
|
||||
return '!' + ParenthesesUtils.getText(expression, ParenthesesUtils.PREFIX_PRECEDENCE);
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
class X {
|
||||
boolean test(String s1, String s2) {
|
||||
return (s1 == null || s1.isEmpty()) && (s2 == null || s2.isEmpty());
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class X {
|
||||
boolean test(String s1, String s2) {
|
||||
if<caret>(s1 != null && !s1.isEmpty() || s2 != null && !s2.isEmpty()) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+7
-23
@@ -28,27 +28,11 @@ public class TrivialIfFixTest extends IGQuickFixesTestCase {
|
||||
myDefaultHint = "Simplify 'if else'";
|
||||
}
|
||||
|
||||
public void testComments() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testCommentsInAssignment() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNegatedConditional() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNegatedConditional1() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testAssert1() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testAssert2() {
|
||||
doTest();
|
||||
}
|
||||
public void testComments() { doTest(); }
|
||||
public void testCommentsInAssignment() { doTest(); }
|
||||
public void testNegatedConditional() { doTest(); }
|
||||
public void testNegatedConditional1() { doTest(); }
|
||||
public void testAssert1() { doTest(); }
|
||||
public void testAssert2() { doTest(); }
|
||||
public void testParentheses() { doTest(); }
|
||||
}
|
||||
Reference in New Issue
Block a user