From d7e2efc0c060700eab15423a9aafe0782952486b Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Tue, 10 Jul 2018 15:57:51 +0200 Subject: [PATCH] invert if: fix parent ifStatement (IDEA-191327) --- .../intention/impl/InvertIfConditionAction.java | 5 +++++ .../codeInsight/invertIfCondition/afterElseIf.java | 12 ++++++++++++ .../codeInsight/invertIfCondition/beforeElseIf.java | 7 +++++++ 3 files changed, 24 insertions(+) create mode 100644 java/java-tests/testData/codeInsight/invertIfCondition/afterElseIf.java create mode 100644 java/java-tests/testData/codeInsight/invertIfCondition/beforeElseIf.java diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java index b4a4da52912f..8a5832a26e09 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java @@ -213,6 +213,11 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction { PsiStatement statement = factory.createStatementFromText("return;", ifStatement); statement = (PsiStatement) codeStyle.reformat(statement); if (thenBranch instanceof PsiBlockStatement) { + if (ifStatement.getParent() instanceof PsiIfStatement) { + ifStatement = (PsiIfStatement)wrapWithCodeBlock(ifStatement); + thenBranch = ifStatement.getThenBranch(); + assert thenBranch != null; + } PsiCodeBlock codeBlock = ((PsiBlockStatement)thenBranch).getCodeBlock(); PsiElement firstElement = codeBlock.getFirstBodyElement(); PsiElement lastElement = codeBlock.getLastBodyElement(); diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/afterElseIf.java b/java/java-tests/testData/codeInsight/invertIfCondition/afterElseIf.java new file mode 100644 index 000000000000..eff5e240d7a7 --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/afterElseIf.java @@ -0,0 +1,12 @@ +// "Invert 'if' condition" "true" +class A { + public void foo(boolean c, boolean c2) { + if (c) a(); + else { + if (c2) { + return; + } + b(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/beforeElseIf.java b/java/java-tests/testData/codeInsight/invertIfCondition/beforeElseIf.java new file mode 100644 index 000000000000..580b14065d9a --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/beforeElseIf.java @@ -0,0 +1,7 @@ +// "Invert 'if' condition" "true" +class A { + public void foo(boolean c, boolean c2) { + if (c) a(); + else if (!c2) b(); + } +} \ No newline at end of file