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 new file mode 100644 index 000000000000..7571226057b7 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/InlineElseBranchAction.java @@ -0,0 +1,69 @@ +/* + * 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/InvertIfConditionAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java index 50b8653a71c1..97335908d2d9 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 @@ -312,9 +312,9 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction { ifStatement.setElseBranch(thenBranch); } - private static void addAfter(PsiIfStatement ifStatement, PsiStatement thenBranch) throws IncorrectOperationException { - if (thenBranch instanceof PsiBlockStatement) { - PsiBlockStatement blockStatement = (PsiBlockStatement) thenBranch; + static void addAfter(PsiIfStatement ifStatement, PsiStatement branch) throws IncorrectOperationException { + if (branch instanceof PsiBlockStatement) { + PsiBlockStatement blockStatement = (PsiBlockStatement) branch; final PsiCodeBlock block = blockStatement.getCodeBlock(); final PsiElement firstBodyElement = block.getFirstBodyElement(); final PsiElement lastBodyElement = block.getLastBodyElement(); @@ -322,7 +322,7 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction { ifStatement.getParent().addRangeAfter(firstBodyElement, lastBodyElement, ifStatement); } } else { - ifStatement.getParent().addAfter(thenBranch, ifStatement); + ifStatement.getParent().addAfter(branch, ifStatement); } } diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/afterBlock1.java b/java/java-tests/testData/codeInsight/inlineElseBranch/afterBlock1.java new file mode 100644 index 000000000000..ec35a09e3b1a --- /dev/null +++ b/java/java-tests/testData/codeInsight/inlineElseBranch/afterBlock1.java @@ -0,0 +1,9 @@ +// "Inline 'else' branch" "true" + +class T { + void f(boolean b) { + 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/inlineElseBranch/afterBlock2.java b/java/java-tests/testData/codeInsight/inlineElseBranch/afterBlock2.java new file mode 100644 index 000000000000..7c74a2aeeead --- /dev/null +++ b/java/java-tests/testData/codeInsight/inlineElseBranch/afterBlock2.java @@ -0,0 +1,12 @@ +// "Inline 'else' branch" "true" + +class T { + void f(boolean b) { + if (b) { + System.out.println("When true"); + } + // Before + System.out.println("Otherwise"); + // After + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/afterSimple1.java b/java/java-tests/testData/codeInsight/inlineElseBranch/afterSimple1.java new file mode 100644 index 000000000000..f869b70df39f --- /dev/null +++ b/java/java-tests/testData/codeInsight/inlineElseBranch/afterSimple1.java @@ -0,0 +1,11 @@ +// "Inline 'else' branch" "true" + +class T { + void f(boolean b) { + if (b) + System.out.println("When true"); + // Before + System.out.println("Otherwise"); + // After + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/afterSimple2.java b/java/java-tests/testData/codeInsight/inlineElseBranch/afterSimple2.java new file mode 100644 index 000000000000..4ec5dc5fa95e --- /dev/null +++ b/java/java-tests/testData/codeInsight/inlineElseBranch/afterSimple2.java @@ -0,0 +1,10 @@ +// "Inline 'else' branch" "true" + +class T { + void f(boolean b) { + 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/inlineElseBranch/beforeBlock1.java b/java/java-tests/testData/codeInsight/inlineElseBranch/beforeBlock1.java new file mode 100644 index 000000000000..00e1ee934de2 --- /dev/null +++ b/java/java-tests/testData/codeInsight/inlineElseBranch/beforeBlock1.java @@ -0,0 +1,11 @@ +// "Inline 'else' branch" "true" + +class T { + void f(boolean b) { + if (b) + System.out.println("When true"); + else { + System.out.println("Otherwise"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeBlock2.java b/java/java-tests/testData/codeInsight/inlineElseBranch/beforeBlock2.java new file mode 100644 index 000000000000..e7460e732a3e --- /dev/null +++ b/java/java-tests/testData/codeInsight/inlineElseBranch/beforeBlock2.java @@ -0,0 +1,13 @@ +// "Inline 'else' branch" "true" + +class T { + void f(boolean b) { + if (b) { + System.out.println("When true"); + } else { + // Before + System.out.println("Otherwise"); + // After + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeChainedIf.java b/java/java-tests/testData/codeInsight/inlineElseBranch/beforeChainedIf.java new file mode 100644 index 000000000000..f088cbabf3ca --- /dev/null +++ b/java/java-tests/testData/codeInsight/inlineElseBranch/beforeChainedIf.java @@ -0,0 +1,12 @@ +// "Inline 'else' branch" "false" + +class T { + void f(boolean a, boolean b) { + if (a) + if (b) { + System.out.println("When true"); + } else { + System.out.println("Otherwise"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeSimple1.java b/java/java-tests/testData/codeInsight/inlineElseBranch/beforeSimple1.java new file mode 100644 index 000000000000..ecc901bd087f --- /dev/null +++ b/java/java-tests/testData/codeInsight/inlineElseBranch/beforeSimple1.java @@ -0,0 +1,12 @@ +// "Inline 'else' branch" "true" + +class T { + void f(boolean b) { + if (b) + System.out.println("When true"); + else + // Before + System.out.println("Otherwise"); + // After + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/inlineElseBranch/beforeSimple2.java b/java/java-tests/testData/codeInsight/inlineElseBranch/beforeSimple2.java new file mode 100644 index 000000000000..3515ccd3dcee --- /dev/null +++ b/java/java-tests/testData/codeInsight/inlineElseBranch/beforeSimple2.java @@ -0,0 +1,11 @@ +// "Inline 'else' branch" "true" + +class T { + void f(boolean b) { + if (b) { + System.out.println("When true"); + } else { + System.out.println("Otherwise"); + } + } +} \ 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/InlineElseBranchTest.java new file mode 100644 index 000000000000..906100e3442a --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/intention/InlineElseBranchTest.java @@ -0,0 +1,33 @@ +/* + * 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; + +import com.intellij.codeInsight.daemon.LightIntentionActionTestCase; + +/** + * @author Pavel.Dolgov + */ +public class InlineElseBranchTest extends LightIntentionActionTestCase { + + @Override + protected String getBasePath() { + return "/codeInsight/inlineElseBranch/"; + } + + public void test() throws Exception { + doAllTests(); + } +} diff --git a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties index 6e4db094fa4e..d8598789d319 100644 --- a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties +++ b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties @@ -235,6 +235,7 @@ 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.create.test=Create Test diff --git a/resources-en/src/intentionDescriptions/InlineElseBranchAction/after.java.template b/resources-en/src/intentionDescriptions/InlineElseBranchAction/after.java.template new file mode 100644 index 000000000000..e22f335eada3 --- /dev/null +++ b/resources-en/src/intentionDescriptions/InlineElseBranchAction/after.java.template @@ -0,0 +1,6 @@ +void f(boolean b) { + if (b) { + System.out.println("When true"); + } + System.out.println("Otherwise"); +} \ No newline at end of file diff --git a/resources-en/src/intentionDescriptions/InlineElseBranchAction/before.java.template b/resources-en/src/intentionDescriptions/InlineElseBranchAction/before.java.template new file mode 100644 index 000000000000..e5566d292f52 --- /dev/null +++ b/resources-en/src/intentionDescriptions/InlineElseBranchAction/before.java.template @@ -0,0 +1,7 @@ +void f(boolean b) { + if (b) { + System.out.println("When true"); + } else { + System.out.println("Otherwise"); + } +} \ No newline at end of file diff --git a/resources-en/src/intentionDescriptions/InlineElseBranchAction/description.html b/resources-en/src/intentionDescriptions/InlineElseBranchAction/description.html new file mode 100644 index 000000000000..27391d30cb41 --- /dev/null +++ b/resources-en/src/intentionDescriptions/InlineElseBranchAction/description.html @@ -0,0 +1,5 @@ + + +This intention inlines the code from the else branch after the if statement. + + diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 57335772132a..a71f092f67fd 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -972,6 +972,10 @@ com.intellij.codeInsight.intention.impl.ConvertCompareToToEqualsIntention Java/Control Flow + + com.intellij.codeInsight.intention.impl.InlineElseBranchAction + Java/Control Flow + com.intellij.codeInsight.intention.impl.CreateFieldFromParameterAction