diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index 4c03c55c2cc..b27134b2125 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -100,3 +100,5 @@ migrate.tuple=Migrate tuples in project\: e.g., \#(,) and \#(,,) will be replace migrate.sure=Replace sure() calls by !! in project add.when.else.branch.action.family.name=Add Else Branch add.when.else.branch.action=Add else branch +move.when.else.branch.to.the.end.action=Move else branch to the end +move.when.else.branch.to.the.end.family.name=Move Else Branch to the End diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/MoveWhenElseBranchFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/MoveWhenElseBranchFix.java new file mode 100644 index 00000000000..9d4dad15056 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/MoveWhenElseBranchFix.java @@ -0,0 +1,98 @@ +/* + * Copyright 2010-2013 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 org.jetbrains.jet.plugin.quickfix; + +import com.intellij.codeInsight.CodeInsightUtilBase; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.jet.lang.diagnostics.Diagnostic; +import org.jetbrains.jet.lang.psi.JetPsiFactory; +import org.jetbrains.jet.lang.psi.JetWhenEntry; +import org.jetbrains.jet.lang.psi.JetWhenExpression; +import org.jetbrains.jet.plugin.JetBundle; + +public class MoveWhenElseBranchFix extends JetIntentionAction { + public MoveWhenElseBranchFix(@NotNull JetWhenExpression element) { + super(element); + } + + @NotNull + @Override + public String getText() { + return JetBundle.message("move.when.else.branch.to.the.end.action"); + } + + @NotNull + @Override + public String getFamilyName() { + return JetBundle.message("move.when.else.branch.to.the.end.family.name"); + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + if (!super.isAvailable(project, editor, file)) { + return false; + } + int elseCount = 0; + for (JetWhenEntry entry : element.getEntries()) { + if (entry.isElse()) { + elseCount++; + } + } + return (elseCount == 1); + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + JetWhenEntry elseEntry = null; + JetWhenEntry lastEntry = null; + for (JetWhenEntry entry : element.getEntries()) { + if (entry.isElse()) { + elseEntry = entry; + } + lastEntry = entry; + } + assert (elseEntry != null) : "isAvailable should check whether there is only one else branch"; + int cursorOffset = editor.getCaretModel().getOffset() - elseEntry.getTextOffset(); + + PsiElement insertedBranch = element.addAfter(elseEntry, lastEntry); + element.addAfter(JetPsiFactory.createNewLine(project), lastEntry); + element.deleteChildRange(elseEntry, elseEntry); + JetWhenEntry insertedWhenEntry = (JetWhenEntry) CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(insertedBranch); + + editor.getCaretModel().moveToOffset(insertedWhenEntry.getTextOffset() + cursorOffset); + } + + public static JetIntentionActionFactory createFactory() { + return new JetIntentionActionFactory() { + @Nullable + @Override + public JetIntentionAction createAction(Diagnostic diagnostic) { + PsiElement element = diagnostic.getPsiElement(); + JetWhenExpression whenExpression = PsiTreeUtil.getParentOfType(element, JetWhenExpression.class, false); + if (whenExpression == null) return null; + return new MoveWhenElseBranchFix(whenExpression); + } + }; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index a5ad9eb0a9a..670415597c5 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -146,6 +146,7 @@ public class QuickFixes { actions.put(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE, new SpecifyTypeExplicitlyFix()); + factories.put(ELSE_MISPLACED_IN_WHEN, MoveWhenElseBranchFix.createFactory()); factories.put(NO_ELSE_IN_WHEN, AddWhenElseBranchFix.createFactory()); factories.put(NO_TYPE_ARGUMENTS_ON_RHS_OF_IS_EXPRESSION, AddStarProjectionsFix.createFactoryForIsExpression()); diff --git a/idea/testData/quickfix/when/afterElseNotLastInWhen.kt b/idea/testData/quickfix/when/afterElseNotLastInWhen.kt new file mode 100644 index 00000000000..48bd88fcd36 --- /dev/null +++ b/idea/testData/quickfix/when/afterElseNotLastInWhen.kt @@ -0,0 +1,9 @@ +// "Move else branch to the end" "true" +fun test() { + val a = 12 + when (a) { + 1 -> { /* some code */ } + 2 -> { /* some more code */ } + else -> { /* other code */ } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/when/afterTwoElseBranchesInWhen.kt b/idea/testData/quickfix/when/afterTwoElseBranchesInWhen.kt new file mode 100644 index 00000000000..ddf036a0336 --- /dev/null +++ b/idea/testData/quickfix/when/afterTwoElseBranchesInWhen.kt @@ -0,0 +1,16 @@ +// "Move else branch to the end" "false" +// ERROR: 'else' entry must be the last one in a when-expression +// ERROR: 'else' entry must be the last one in a when-expression +// ERROR: Unreachable code +// ERROR: Unreachable code +package foo + +fun foo() { + var i = 2 + when (i) { + 1, 2 -> { /* some code */ } + else -> { /* first else branch */ } + else -> { /* second else branch */ } + 3 -> { /* some other code */ } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/when/beforeElseNotLastInWhen.kt b/idea/testData/quickfix/when/beforeElseNotLastInWhen.kt new file mode 100644 index 00000000000..eb027b40815 --- /dev/null +++ b/idea/testData/quickfix/when/beforeElseNotLastInWhen.kt @@ -0,0 +1,9 @@ +// "Move else branch to the end" "true" +fun test() { + val a = 12 + when (a) { + 1 -> { /* some code */ } + else -> { /* other code */ } + 2 -> { /* some more code */ } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/when/beforeTwoElseBranchesInWhen.kt b/idea/testData/quickfix/when/beforeTwoElseBranchesInWhen.kt new file mode 100644 index 00000000000..ddf036a0336 --- /dev/null +++ b/idea/testData/quickfix/when/beforeTwoElseBranchesInWhen.kt @@ -0,0 +1,16 @@ +// "Move else branch to the end" "false" +// ERROR: 'else' entry must be the last one in a when-expression +// ERROR: 'else' entry must be the last one in a when-expression +// ERROR: Unreachable code +// ERROR: Unreachable code +package foo + +fun foo() { + var i = 2 + when (i) { + 1, 2 -> { /* some code */ } + else -> { /* first else branch */ } + else -> { /* second else branch */ } + 3 -> { /* some other code */ } + } +} \ No newline at end of file