diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SplitIfAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SplitIfAction.java index b62a5aebd292..4564664d18db 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/SplitIfAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/SplitIfAction.java @@ -25,6 +25,7 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.refactoring.util.RefactoringUtil; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; @@ -104,8 +105,8 @@ public class SplitIfAction extends PsiElementBaseIntentionAction { PsiManager psiManager = ifStatement.getManager(); PsiIfStatement subIf = (PsiIfStatement)ifStatement.copy(); - subIf.getCondition().replace(rOperand); - ifStatement.getCondition().replace(lOperand); + subIf.getCondition().replace(RefactoringUtil.unparenthesizeExpression(rOperand)); + ifStatement.getCondition().replace(RefactoringUtil.unparenthesizeExpression(lOperand)); if (ifStatement.getThenBranch() instanceof PsiBlockStatement) { PsiBlockStatement blockStmt = (PsiBlockStatement)JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createStatementFromText("{}", null); @@ -141,8 +142,8 @@ public class SplitIfAction extends PsiElementBaseIntentionAction { PsiStatement elseBranch = ifStatement.getElseBranch(); if (elseBranch != null) { elseBranch = (PsiStatement)elseBranch.copy(); } - ifStatement.getCondition().replace(lOperand); - secondIf.getCondition().replace(rOperand); + ifStatement.getCondition().replace(RefactoringUtil.unparenthesizeExpression(lOperand)); + secondIf.getCondition().replace(RefactoringUtil.unparenthesizeExpression(rOperand)); ifStatement.setElseBranch(secondIf); if (elseBranch != null) { secondIf.setElseBranch(elseBranch); } diff --git a/java/java-tests/testData/codeInsight/splitIfAction/afterOrParenthesis.java b/java/java-tests/testData/codeInsight/splitIfAction/afterOrParenthesis.java new file mode 100644 index 000000000000..129158349fcc --- /dev/null +++ b/java/java-tests/testData/codeInsight/splitIfAction/afterOrParenthesis.java @@ -0,0 +1,9 @@ +class C { + void foo() { + if (a) { + call(); + } else if (b) { + call(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/splitIfAction/afterParenthesis.java b/java/java-tests/testData/codeInsight/splitIfAction/afterParenthesis.java new file mode 100644 index 000000000000..19020cded56d --- /dev/null +++ b/java/java-tests/testData/codeInsight/splitIfAction/afterParenthesis.java @@ -0,0 +1,9 @@ +class C { + void foo() { + if (a) { + if (b) { + call(); + } + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/splitIfAction/beforeOrParenthesis.java b/java/java-tests/testData/codeInsight/splitIfAction/beforeOrParenthesis.java new file mode 100644 index 000000000000..32c9a7b59d8b --- /dev/null +++ b/java/java-tests/testData/codeInsight/splitIfAction/beforeOrParenthesis.java @@ -0,0 +1,7 @@ +class C { + void foo() { + if ((a) || (b)) { + call(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/splitIfAction/beforeParenthesis.java b/java/java-tests/testData/codeInsight/splitIfAction/beforeParenthesis.java new file mode 100644 index 000000000000..4fb622a99c0b --- /dev/null +++ b/java/java-tests/testData/codeInsight/splitIfAction/beforeParenthesis.java @@ -0,0 +1,7 @@ +class C { + void foo() { + if ((a) && (b)) { + call(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/intention/SplitIfActionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/intention/SplitIfActionTest.java index 9bff30635d2f..e6dbb1e6e0dc 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/intention/SplitIfActionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/intention/SplitIfActionTest.java @@ -39,6 +39,20 @@ public class SplitIfActionTest extends LightCodeInsightTestCase { checkResultByFile("/codeInsight/splitIfAction/after5.java"); } + public void test6() throws Exception { + configureByFile("/codeInsight/splitIfAction/beforeParenthesis.java"); + perform(); + checkResultByFile("/codeInsight/splitIfAction/afterParenthesis.java"); + } + + public void test7() throws Exception { + configureByFile("/codeInsight/splitIfAction/beforeOrParenthesis.java"); + perform(); + checkResultByFile("/codeInsight/splitIfAction/afterOrParenthesis.java"); + } + + + private void perform() throws Exception { SplitIfAction action = new SplitIfAction(); assertTrue(action.isAvailable(getProject(), getEditor(), getFile()));