From 7f73e1895dc8b07eed79558bc9b138e80e35cd75 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Mon, 26 Jan 2015 14:18:24 +0100 Subject: [PATCH] don't insert unneeded parentheses on "invert if" --- .../invertIfCondition/afterParenthesis4.java | 11 +++++++++++ .../invertIfCondition/beforeParenthesis4.java | 10 ++++++++++ .../intellij/codeInsight/CodeInsightServicesUtil.java | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis4.java create mode 100644 java/java-tests/testData/codeInsight/invertIfCondition/beforeParenthesis4.java diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis4.java b/java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis4.java new file mode 100644 index 000000000000..da3b934cc4f4 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/afterParenthesis4.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/beforeParenthesis4.java b/java/java-tests/testData/codeInsight/invertIfCondition/beforeParenthesis4.java new file mode 100644 index 000000000000..93749b36c901 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/beforeParenthesis4.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 7e350d960982..c9f28724dc17 100644 --- a/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java +++ b/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java @@ -54,7 +54,7 @@ public class CodeInsightServicesUtil { op.replace(inverted); } } - if (tokenType == JavaTokenType.ANDAND && booleanExpression.getParent() instanceof PsiExpression) { + if (tokenType == JavaTokenType.ANDAND && booleanExpression.getParent() instanceof PsiPolyadicExpression) { final PsiParenthesizedExpression parth = (PsiParenthesizedExpression)factory.createExpressionFromText("(a)", expression); parth.getExpression().replace(expression); return parth;