diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/InlineElseBranchAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/InlineElseBranchAction.java deleted file mode 100644 index 7571226057b7..000000000000 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/InlineElseBranchAction.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.intellij.codeInsight.intention.impl; - -import com.intellij.codeInsight.CodeInsightBundle; -import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.project.Project; -import com.intellij.psi.*; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.Nls; -import org.jetbrains.annotations.NotNull; - -/** - * @author Pavel.Dolgov - */ -public class InlineElseBranchAction extends PsiElementBaseIntentionAction { - @Override - public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { - PsiElement parent = element.getParent(); - if (parent instanceof PsiIfStatement) { - PsiIfStatement ifStatement = (PsiIfStatement)parent; - PsiStatement elseBranch = ifStatement.getElseBranch(); - PsiKeyword elseKeyword = ifStatement.getElseElement(); - if (elseBranch != null && elseKeyword != null) { - InvertIfConditionAction.addAfter(ifStatement, elseBranch); - elseBranch.delete(); - elseKeyword.delete(); - } - } - } - - @Override - public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { - if (element instanceof PsiKeyword && ((PsiKeyword)element).getTokenType() == JavaTokenType.ELSE_KEYWORD) { - PsiElement parent = element.getParent(); - return parent instanceof PsiIfStatement && - ((PsiIfStatement)parent).getElseBranch() != null && - parent.getParent() instanceof PsiCodeBlock; - } - return false; - } - - @NotNull - @Override - public String getText() { - return getFamilyName(); - } - - @Nls - @NotNull - @Override - public String getFamilyName() { - return CodeInsightBundle.message("intention.inline.else.branch"); - } -} diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/UnwrapElseBranchAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/UnwrapElseBranchAction.java new file mode 100644 index 000000000000..66b191b87c2f --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/UnwrapElseBranchAction.java @@ -0,0 +1,131 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.intention.impl; + +import com.intellij.codeInsight.CodeInsightBundle; +import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.IncorrectOperationException; +import com.intellij.util.ObjectUtils; +import com.siyeh.ig.psiutils.ControlFlowUtils; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +/** + * @author Pavel.Dolgov + */ +public class UnwrapElseBranchAction extends PsiElementBaseIntentionAction { + private static final Logger LOG = Logger.getInstance(UnwrapElseBranchAction.class); + + @Override + public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { + PsiElement parent = element.getParent(); + if (parent instanceof PsiIfStatement) { + PsiIfStatement ifStatement = (PsiIfStatement)parent; + PsiStatement elseBranch = ifStatement.getElseBranch(); + PsiElement grandParent = ifStatement.getParent(); + if (elseBranch != null && grandParent != null) { + if (!(grandParent instanceof PsiCodeBlock)) { + PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); + PsiCodeBlock codeBlock = factory.createCodeBlockFromText("{" + ifStatement.getText() + "}", ifStatement); + codeBlock = (PsiCodeBlock)ifStatement.replace(codeBlock); + ifStatement = (PsiIfStatement)codeBlock.getStatements()[0]; + elseBranch = ifStatement.getElseBranch(); + LOG.assertTrue(elseBranch != null); + } + InvertIfConditionAction.addAfter(ifStatement, elseBranch); + elseBranch.delete(); + } + } + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { + if (element instanceof PsiKeyword && ((PsiKeyword)element).getTokenType() == JavaTokenType.ELSE_KEYWORD) { + PsiElement parent = element.getParent(); + if (parent instanceof PsiIfStatement) { + PsiIfStatement ifStatement = (PsiIfStatement)parent; + PsiStatement elseBranch = ifStatement.getElseBranch(); + if (elseBranch != null) { + PsiStatement thenBranch = ifStatement.getThenBranch(); + boolean thenCompletesNormally = ControlFlowUtils.statementMayCompleteNormally(thenBranch); + boolean elseCompletesNormally = ControlFlowUtils.statementMayCompleteNormally(elseBranch); + if (!thenCompletesNormally || + elseCompletesNormally || + !nextStatementMayBecomeUnreachable(ifStatement)) { + if (thenCompletesNormally) { + setText(CodeInsightBundle.message("intention.unwrap.else.branch.changes.semantics")); + } + else { + setText(CodeInsightBundle.message("intention.unwrap.else.branch")); + } + return true; + } + } + } + } + return false; + } + + /** + * Check if there could be new unreachable code if the statement may not complete normally + * + * @param statement after refactoring it may not complete normally + * @return true if the refactoring may cause unreachable code + */ + private static boolean nextStatementMayBecomeUnreachable(PsiStatement statement) { + PsiStatement nextStatement = PsiTreeUtil.getNextSiblingOfType(statement, PsiStatement.class); + if (nextStatement != null) { + return !(nextStatement instanceof PsiSwitchLabelStatement); + } + + PsiElement parent = statement.getParent(); + if (parent instanceof PsiIfStatement) { + PsiIfStatement ifStatement = (PsiIfStatement)parent; + PsiStatement thenBranch = ifStatement.getThenBranch(); + PsiStatement elseBranch = ifStatement.getElseBranch(); + if (thenBranch == statement && ControlFlowUtils.statementMayCompleteNormally(elseBranch) || + elseBranch == statement && ControlFlowUtils.statementMayCompleteNormally(thenBranch)) { + return false; + } + return nextStatementMayBecomeUnreachable(ifStatement); + } + if (parent instanceof PsiLabeledStatement) { + return nextStatementMayBecomeUnreachable((PsiLabeledStatement)parent); + } + if (parent instanceof PsiCodeBlock) { + PsiStatement parentStatement = ObjectUtils.tryCast(parent.getParent(), PsiStatement.class); + if (parentStatement instanceof PsiBlockStatement || + parentStatement instanceof PsiSynchronizedStatement || + parentStatement instanceof PsiTryStatement || // TODO handle try-catch more accurately + parentStatement instanceof PsiSwitchStatement) { + return nextStatementMayBecomeUnreachable(parentStatement); + } + } + return false; + } + + @Nls + @NotNull + @Override + public String getFamilyName() { + return CodeInsightBundle.message("intention.unwrap.else.branch"); + } +} diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/afterBlock1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterBlock1.java similarity index 72% rename from java/java-tests/testData/codeInsight/inlineElseBranch/afterBlock1.java rename to java/java-tests/testData/codeInsight/unwrapElseBranch/afterBlock1.java index ec35a09e3b1a..668b0b2d7798 100644 --- a/java/java-tests/testData/codeInsight/inlineElseBranch/afterBlock1.java +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterBlock1.java @@ -1,4 +1,4 @@ -// "Inline 'else' branch" "true" +// "Unwrap 'else' branch (changes semantics)" "true" class T { void f(boolean b) { diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/afterBlock2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterBlock2.java similarity index 78% rename from java/java-tests/testData/codeInsight/inlineElseBranch/afterBlock2.java rename to java/java-tests/testData/codeInsight/unwrapElseBranch/afterBlock2.java index 7c74a2aeeead..b06718edba2d 100644 --- a/java/java-tests/testData/codeInsight/inlineElseBranch/afterBlock2.java +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterBlock2.java @@ -1,4 +1,4 @@ -// "Inline 'else' branch" "true" +// "Unwrap 'else' branch (changes semantics)" "true" class T { void f(boolean b) { diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterBlock3.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterBlock3.java new file mode 100644 index 000000000000..1479060fc0e8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterBlock3.java @@ -0,0 +1,9 @@ +// "Unwrap 'else' branch" "true" + +class T { + void f(boolean b) { + if (b) + throw new RuntimeException("When true"); + System.out.println("Otherwise"); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterChainedIf1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterChainedIf1.java new file mode 100644 index 000000000000..57fa31e30de3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterChainedIf1.java @@ -0,0 +1,12 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean a, boolean b) { + if (a) { + if (b) { + System.out.println("When true"); + } + System.out.println("Otherwise"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterChainedIf2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterChainedIf2.java new file mode 100644 index 000000000000..a47b43ce605a --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterChainedIf2.java @@ -0,0 +1,13 @@ +// "Unwrap 'else' branch" "true" + +class T { + String f(boolean a, boolean b) { + if (a) { + if (b) { + return "When true"; + } + System.out.println("Otherwise"); + } + return "Default"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterChainedIf3.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterChainedIf3.java new file mode 100644 index 000000000000..d938dbaa6813 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterChainedIf3.java @@ -0,0 +1,13 @@ +// "Unwrap 'else' branch" "true" + +class T { + String f(boolean a, boolean b) { + if (a) { + if (b) { + return "When true"; + } + return "Otherwise"; + } + return "Default"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterLabeled1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterLabeled1.java new file mode 100644 index 000000000000..2b432b58e705 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterLabeled1.java @@ -0,0 +1,15 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean a, boolean b) { + if (a) { + Label: + { + if (b) + System.out.println("When true"); + throw new RuntimeException("Otherwise"); + } + } + System.out.println("Done"); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterLastReachable1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterLastReachable1.java new file mode 100644 index 000000000000..ce6bbb6f511e --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterLastReachable1.java @@ -0,0 +1,12 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean a, boolean b) { + if (a) { + if (b) + System.out.println("When true"); + System.out.println("Otherwise"); + return; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterLastReachable2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterLastReachable2.java new file mode 100644 index 000000000000..40d9a6f4a897 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterLastReachable2.java @@ -0,0 +1,12 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + String f(boolean a, boolean b) { + if (a) { + if (b) + System.out.println("When true"); + return "Otherwise"; + } + return "Default"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/afterSimple1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSimple1.java similarity index 77% rename from java/java-tests/testData/codeInsight/inlineElseBranch/afterSimple1.java rename to java/java-tests/testData/codeInsight/unwrapElseBranch/afterSimple1.java index f869b70df39f..c376f3fe0a1d 100644 --- a/java/java-tests/testData/codeInsight/inlineElseBranch/afterSimple1.java +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSimple1.java @@ -1,4 +1,4 @@ -// "Inline 'else' branch" "true" +// "Unwrap 'else' branch (changes semantics)" "true" class T { void f(boolean b) { diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/afterSimple2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSimple2.java similarity index 74% rename from java/java-tests/testData/codeInsight/inlineElseBranch/afterSimple2.java rename to java/java-tests/testData/codeInsight/unwrapElseBranch/afterSimple2.java index 4ec5dc5fa95e..7cf27bd5939a 100644 --- a/java/java-tests/testData/codeInsight/inlineElseBranch/afterSimple2.java +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSimple2.java @@ -1,4 +1,4 @@ -// "Inline 'else' branch" "true" +// "Unwrap 'else' branch (changes semantics)" "true" class T { void f(boolean b) { diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSimple3.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSimple3.java new file mode 100644 index 000000000000..d49e37cd61eb --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSimple3.java @@ -0,0 +1,11 @@ +// "Unwrap 'else' branch" "true" + +class T { + String f(boolean b) { + if (b) + return "When true"; + // Before + return "Otherwise"; + // After + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSimple4.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSimple4.java new file mode 100644 index 000000000000..14bc08a4cd6e --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSimple4.java @@ -0,0 +1,11 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + String f(boolean b) { + if (b) + System.out.println("When true"); + else + return "Otherwise"; + return "Default"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSwitch1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSwitch1.java new file mode 100644 index 000000000000..f9f380a86d46 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSwitch1.java @@ -0,0 +1,15 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean b, int n) { + switch (n) { + case 1: + if (b) + System.out.println("When true"); + // Before + System.out.println("Otherwise"); + // After + case 2: + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSwitch2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSwitch2.java new file mode 100644 index 000000000000..b59a1f8bb6ad --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSwitch2.java @@ -0,0 +1,16 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean b, int n) { + switch (n) { + case 1: + if (b) + System.out.println("When true"); + // Before + System.out.println("Otherwise"); + // After + break; + case 2: + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSwitch3.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSwitch3.java new file mode 100644 index 000000000000..2877097058d5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSwitch3.java @@ -0,0 +1,17 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean b, int n) { + switch (n) { + case 1: + if (b) + System.out.println("When true"); + // Before + System.out.println("Otherwise"); + // After + return; + case 2: + } + System.out.println("Done"); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSynchronized1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSynchronized1.java new file mode 100644 index 000000000000..fa7db7d8a1d7 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterSynchronized1.java @@ -0,0 +1,11 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(Object o, boolean b) { + synchronized (o) { + if (b) + System.out.println("When true"); + throw new RuntimeException("Otherwise"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/afterTry1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterTry1.java new file mode 100644 index 000000000000..587ab10aaee5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/afterTry1.java @@ -0,0 +1,13 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean b) { + try { + if (b) + System.out.println("When true"); + throw new RuntimeException("Otherwise"); + } finally { + System.out.println("Finally"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeBlock1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeBlock1.java similarity index 77% rename from java/java-tests/testData/codeInsight/inlineElseBranch/beforeBlock1.java rename to java/java-tests/testData/codeInsight/unwrapElseBranch/beforeBlock1.java index 00e1ee934de2..3047fe08aeb4 100644 --- a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeBlock1.java +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeBlock1.java @@ -1,4 +1,4 @@ -// "Inline 'else' branch" "true" +// "Unwrap 'else' branch (changes semantics)" "true" class T { void f(boolean b) { diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeBlock2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeBlock2.java similarity index 81% rename from java/java-tests/testData/codeInsight/inlineElseBranch/beforeBlock2.java rename to java/java-tests/testData/codeInsight/unwrapElseBranch/beforeBlock2.java index e7460e732a3e..3931eb92d3aa 100644 --- a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeBlock2.java +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeBlock2.java @@ -1,4 +1,4 @@ -// "Inline 'else' branch" "true" +// "Unwrap 'else' branch (changes semantics)" "true" class T { void f(boolean b) { diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeBlock3.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeBlock3.java new file mode 100644 index 000000000000..62a17dc1d9eb --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeBlock3.java @@ -0,0 +1,11 @@ +// "Unwrap 'else' branch" "true" + +class T { + void f(boolean b) { + if (b) + throw new RuntimeException("When true"); + else { + System.out.println("Otherwise"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeChainedIf.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeChainedIf1.java similarity index 81% rename from java/java-tests/testData/codeInsight/inlineElseBranch/beforeChainedIf.java rename to java/java-tests/testData/codeInsight/unwrapElseBranch/beforeChainedIf1.java index f088cbabf3ca..5a87aa8a6bb2 100644 --- a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeChainedIf.java +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeChainedIf1.java @@ -1,4 +1,4 @@ -// "Inline 'else' branch" "false" +// "Unwrap 'else' branch (changes semantics)" "true" class T { void f(boolean a, boolean b) { diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeChainedIf2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeChainedIf2.java new file mode 100644 index 000000000000..c4502c12f3b2 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeChainedIf2.java @@ -0,0 +1,13 @@ +// "Unwrap 'else' branch" "true" + +class T { + String f(boolean a, boolean b) { + if (a) + if (b) { + return "When true"; + } else { + System.out.println("Otherwise"); + } + return "Default"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeChainedIf3.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeChainedIf3.java new file mode 100644 index 000000000000..858c9fee462f --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeChainedIf3.java @@ -0,0 +1,13 @@ +// "Unwrap 'else' branch" "true" + +class T { + String f(boolean a, boolean b) { + if (a) + if (b) { + return "When true"; + } else { + return "Otherwise"; + } + return "Default"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeLabeled1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeLabeled1.java new file mode 100644 index 000000000000..2a395f2bc9a1 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeLabeled1.java @@ -0,0 +1,17 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean a, boolean b) { + if (a) { + Label: + { + if (b) + System.out.println("When true"); + else { + throw new RuntimeException("Otherwise"); + } + } + } + System.out.println("Done"); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeLabeled2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeLabeled2.java new file mode 100644 index 000000000000..d97ad1cc18e3 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeLabeled2.java @@ -0,0 +1,15 @@ +// "Unwrap 'else' branch (changes semantics)" "false" + +class T { + void f(boolean b) { + Label: + { + if (b) + System.out.println("When true"); + else { + throw new RuntimeException("Otherwise"); + } + } + System.out.println("Done"); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeLastReachable1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeLastReachable1.java new file mode 100644 index 000000000000..f18ea8ca8a01 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeLastReachable1.java @@ -0,0 +1,14 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean a, boolean b) { + if (a) { + if (b) + System.out.println("When true"); + else { + System.out.println("Otherwise"); + return; + } + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeLastReachable2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeLastReachable2.java new file mode 100644 index 000000000000..0c5fcccc8bec --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeLastReachable2.java @@ -0,0 +1,13 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + String f(boolean a, boolean b) { + if (a) { + if (b) + System.out.println("When true"); + else + return "Otherwise"; + } + return "Default"; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeNotLastReachable1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeNotLastReachable1.java new file mode 100644 index 000000000000..3333c8a2ee7a --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeNotLastReachable1.java @@ -0,0 +1,14 @@ +// "Unwrap 'else' branch (changes semantics)" "false" + +class T { + void m(boolean b) { + if (b) { + System.out.println(1); + } + else { + System.out.println(2); + return; + } + System.out.println(3); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeNotLastReachable2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeNotLastReachable2.java new file mode 100644 index 000000000000..2c4524d0dfe7 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeNotLastReachable2.java @@ -0,0 +1,14 @@ +// "Unwrap 'else' branch" "false" + +class T { + void m(boolean b) { + if (b) { + System.out.println(1); + } + else { + System.out.println(2); + return; + } + System.out.println(3); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeSimple1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSimple1.java similarity index 79% rename from java/java-tests/testData/codeInsight/inlineElseBranch/beforeSimple1.java rename to java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSimple1.java index ecc901bd087f..7b2701974a56 100644 --- a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeSimple1.java +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSimple1.java @@ -1,4 +1,4 @@ -// "Inline 'else' branch" "true" +// "Unwrap 'else' branch (changes semantics)" "true" class T { void f(boolean b) { diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeSimple2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSimple2.java similarity index 77% rename from java/java-tests/testData/codeInsight/inlineElseBranch/beforeSimple2.java rename to java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSimple2.java index 3515ccd3dcee..94a7f578cd8b 100644 --- a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeSimple2.java +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSimple2.java @@ -1,4 +1,4 @@ -// "Inline 'else' branch" "true" +// "Unwrap 'else' branch (changes semantics)" "true" class T { void f(boolean b) { diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSimple3.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSimple3.java new file mode 100644 index 000000000000..bb4d542680da --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSimple3.java @@ -0,0 +1,12 @@ +// "Unwrap 'else' branch" "true" + +class T { + String f(boolean b) { + if (b) + return "When true"; + else + // Before + return "Otherwise"; + // After + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSwitch1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSwitch1.java new file mode 100644 index 000000000000..8b7814447344 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSwitch1.java @@ -0,0 +1,16 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean b, int n) { + switch (n) { + case 1: + if (b) + System.out.println("When true"); + else + // Before + System.out.println("Otherwise"); + // After + case 2: + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSwitch2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSwitch2.java new file mode 100644 index 000000000000..993f250ed5ce --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSwitch2.java @@ -0,0 +1,18 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean b, int n) { + switch (n) { + case 1: + if (b) + System.out.println("When true"); + else { + // Before + System.out.println("Otherwise"); + // After + break; + } + case 2: + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSwitch3.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSwitch3.java new file mode 100644 index 000000000000..10b8fa25ba50 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSwitch3.java @@ -0,0 +1,19 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean b, int n) { + switch (n) { + case 1: + if (b) + System.out.println("When true"); + else { + // Before + System.out.println("Otherwise"); + // After + return; + } + case 2: + } + System.out.println("Done"); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSwitch4.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSwitch4.java new file mode 100644 index 000000000000..366bd7c02144 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSwitch4.java @@ -0,0 +1,18 @@ +// "Unwrap 'else' branch (changes semantics)" "false" + +class T { + void f(boolean b, int n) { + switch (n) { + case 1: + if (b) + System.out.println("When true"); + else { + // Before + System.out.println("Otherwise"); + // After + return; + } + } + System.out.println("Done"); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSynchronized1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSynchronized1.java new file mode 100644 index 000000000000..279cd7462c15 --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSynchronized1.java @@ -0,0 +1,13 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(Object o, boolean b) { + synchronized (o) { + if (b) + System.out.println("When true"); + else { + throw new RuntimeException("Otherwise"); + } + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSynchronized2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSynchronized2.java new file mode 100644 index 000000000000..d379d41a033b --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeSynchronized2.java @@ -0,0 +1,14 @@ +// "Unwrap 'else' branch (changes semantics)" "false" + +class T { + void f(Object o, boolean b) { + synchronized (o) { + if (b) + System.out.println("When true"); + else { + throw new RuntimeException("Otherwise"); + } + } + System.out.println("Done"); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeTry1.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeTry1.java new file mode 100644 index 000000000000..e3b45a0d46fa --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeTry1.java @@ -0,0 +1,15 @@ +// "Unwrap 'else' branch (changes semantics)" "true" + +class T { + void f(boolean b) { + try { + if (b) + System.out.println("When true"); + else { + throw new RuntimeException("Otherwise"); + } + } finally { + System.out.println("Finally"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeTry2.java b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeTry2.java new file mode 100644 index 000000000000..5c6c6f51a08b --- /dev/null +++ b/java/java-tests/testData/codeInsight/unwrapElseBranch/beforeTry2.java @@ -0,0 +1,15 @@ +// "Unwrap 'else' branch (changes semantics)" "false" + +class T { + void f(boolean b) { + try { + if (b) + System.out.println("When true"); + else { + throw new RuntimeException("Otherwise"); + } + } finally { + } + System.out.println("Done"); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/intention/InlineElseBranchTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/intention/UnwrapElseBranchTest.java similarity index 88% rename from java/java-tests/testSrc/com/intellij/codeInsight/intention/InlineElseBranchTest.java rename to java/java-tests/testSrc/com/intellij/codeInsight/intention/UnwrapElseBranchTest.java index 906100e3442a..2980140908df 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/intention/InlineElseBranchTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/intention/UnwrapElseBranchTest.java @@ -20,11 +20,11 @@ import com.intellij.codeInsight.daemon.LightIntentionActionTestCase; /** * @author Pavel.Dolgov */ -public class InlineElseBranchTest extends LightIntentionActionTestCase { +public class UnwrapElseBranchTest extends LightIntentionActionTestCase { @Override protected String getBasePath() { - return "/codeInsight/inlineElseBranch/"; + return "/codeInsight/unwrapElseBranch/"; } public void test() throws Exception { diff --git a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties index d8598789d319..a373e250d965 100644 --- a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties +++ b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties @@ -235,7 +235,8 @@ intention.replace.cast.with.var.family=Replace cast with variable intention.convert.color.representation.text=Convert to ''new Color{0}'' intention.convert.color.representation.family=Convert Color representation intention.break.string.on.line.breaks.text=Break string on '\\n' -intention.inline.else.branch=Inline 'else' branch +intention.unwrap.else.branch=Unwrap 'else' branch +intention.unwrap.else.branch.changes.semantics=Unwrap 'else' branch (changes semantics) intention.create.test=Create Test diff --git a/resources-en/src/intentionDescriptions/InlineElseBranchAction/description.html b/resources-en/src/intentionDescriptions/InlineElseBranchAction/description.html deleted file mode 100644 index 27391d30cb41..000000000000 --- a/resources-en/src/intentionDescriptions/InlineElseBranchAction/description.html +++ /dev/null @@ -1,5 +0,0 @@ - - -This intention inlines the code from the else branch after the if statement. - - diff --git a/resources-en/src/intentionDescriptions/InlineElseBranchAction/after.java.template b/resources-en/src/intentionDescriptions/UnwrapElseBranchAction/after.java.template similarity index 100% rename from resources-en/src/intentionDescriptions/InlineElseBranchAction/after.java.template rename to resources-en/src/intentionDescriptions/UnwrapElseBranchAction/after.java.template diff --git a/resources-en/src/intentionDescriptions/InlineElseBranchAction/before.java.template b/resources-en/src/intentionDescriptions/UnwrapElseBranchAction/before.java.template similarity index 100% rename from resources-en/src/intentionDescriptions/InlineElseBranchAction/before.java.template rename to resources-en/src/intentionDescriptions/UnwrapElseBranchAction/before.java.template diff --git a/resources-en/src/intentionDescriptions/UnwrapElseBranchAction/description.html b/resources-en/src/intentionDescriptions/UnwrapElseBranchAction/description.html new file mode 100644 index 000000000000..8975ddf056bd --- /dev/null +++ b/resources-en/src/intentionDescriptions/UnwrapElseBranchAction/description.html @@ -0,0 +1,5 @@ + + +This intention moves the code of the else branch after the if statement. + + diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index a4e34fd52800..391d22cad893 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -978,7 +978,7 @@ Java/Control Flow - com.intellij.codeInsight.intention.impl.InlineElseBranchAction + com.intellij.codeInsight.intention.impl.UnwrapElseBranchAction Java/Control Flow