diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis1.java b/java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis1.java new file mode 100644 index 000000000000..4584077665e2 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis1.java @@ -0,0 +1,11 @@ +// "Invert If Condition" "true" +class Inversion { + public void context(boolean a, boolean b, boolean c) { + if ((!a || !b) && !c) { + System.out.println(1); + } + else { + System.out.println(0); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis2.java b/java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis2.java new file mode 100644 index 000000000000..fd19babc6d64 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis2.java @@ -0,0 +1,11 @@ +// "Invert If Condition" "true" +class Inversion { + public void context(boolean a, boolean b, boolean c) { + if (!a || !b || !c) { + System.out.println(1); + } + else { + System.out.println(0); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis3.java b/java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis3.java new file mode 100644 index 000000000000..ca8ef27512f5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis3.java @@ -0,0 +1,11 @@ +// "Invert If Condition" "true" +class Inversion { + public void context(boolean a, boolean b, boolean c) { + if ((!a && !b) || !c) { + System.out.println(1); + } + else { + System.out.println(0); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/beforeParenthesis1.java b/java/java-tests/testData/codeInsight/invertIfCondition/beforeParenthesis1.java new file mode 100644 index 000000000000..910ff076934f --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/beforeParenthesis1.java @@ -0,0 +1,10 @@ +// "Invert If Condition" "true" +class Inversion { + public void context(boolean a, boolean b, boolean c) { + if (a && b || c) { + System.out.println(0); + } else { + System.out.println(1); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/beforeParenthesis2.java b/java/java-tests/testData/codeInsight/invertIfCondition/beforeParenthesis2.java new file mode 100644 index 000000000000..4aab06463a17 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/beforeParenthesis2.java @@ -0,0 +1,10 @@ +// "Invert If Condition" "true" +class Inversion { + public void context(boolean a, boolean b, boolean c) { + if (a && b && c) { + System.out.println(0); + } else { + System.out.println(1); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/beforeParenthesis3.java b/java/java-tests/testData/codeInsight/invertIfCondition/beforeParenthesis3.java new file mode 100644 index 000000000000..256ef0b2efc3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/beforeParenthesis3.java @@ -0,0 +1,10 @@ +// "Invert If Condition" "true" +class Inversion { + public void context(boolean a, boolean b, boolean c) { + if ((a || b) && c) { + System.out.println(0); + } else { + System.out.println(1); + } + } +} \ No newline at end of file diff --git a/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java b/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java index b440e4cef490..e06b22ab9027 100644 --- a/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java +++ b/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java @@ -50,9 +50,15 @@ public class CodeInsightServicesUtil { expression.getTokenBeforeOperand(op).replace(createOperationToken(factory, ourTokenMap[i + (i % 2 == 0 ? 1 : -1)])); } if (tokenType == JavaTokenType.OROR || tokenType == JavaTokenType.ANDAND) { - op.replace(invertCondition(op)); + PsiExpression inverted = invertCondition(op); + op.replace(inverted); } } + if (tokenType == JavaTokenType.ANDAND && booleanExpression.getParent() instanceof PsiExpression) { + final PsiParenthesizedExpression parth = (PsiParenthesizedExpression)factory.createExpressionFromText("(a)", expression); + parth.getExpression().replace(expression); + return parth; + } return expression; } }