diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java index a9bb63ff73d..79ad8974575 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java @@ -21,6 +21,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFileFactory; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.LocalTimeCounter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -82,6 +83,10 @@ public class JetPsiFactory { return property.findElementAt(3); } + public static PsiElement createNewLineWhiteSpace(Project project) { + return createWhiteSpace(project, "\n"); + } + public static JetClass createClass(Project project, String text) { return createDeclaration(project, text, JetClass.class); } @@ -147,6 +152,11 @@ public class JetPsiFactory { return function.getValueParameters().get(0); } + public static JetWhenEntry createElseWhenEntry(Project project) { + JetNamedFunction function = createFunction(project, "fun foo() { when(12) { else -> { } } }"); + return PsiTreeUtil.findChildOfType(function, JetWhenEntry.class); + } + @NotNull public static JetImportDirective createImportDirective(Project project, @NotNull String path) { return createImportDirective(project, new ImportPath(path)); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetWhenExpression.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetWhenExpression.java index 18b5f58c892..bb32b25b9a2 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetWhenExpression.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetWhenExpression.java @@ -57,4 +57,10 @@ public class JetWhenExpression extends JetExpressionImpl { public PsiElement getWhenKeywordElement() { return findChildByType(JetTokens.WHEN_KEYWORD); } + + @Nullable + public PsiElement getCloseBraceNode() { + ASTNode openBraceNode = getNode().findChildByType(JetTokens.RBRACE); + return openBraceNode != null ? openBraceNode.getPsi() : null; + } } diff --git a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties index 55e6b2a6951..6594d13dfc2 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetBundle.properties +++ b/idea/src/org/jetbrains/jet/plugin/JetBundle.properties @@ -94,3 +94,5 @@ options.jet.attribute.descriptor.label=Label change.to.function.invocation=Change to function invocation migrate.tuple=Migrate tuples in project\: e.g., \#(,) and \#(,,) will be replaced by Pair and Triple 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 diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/AddWhenElseBranchFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/AddWhenElseBranchFix.java new file mode 100644 index 00000000000..d4ddc6f88a1 --- /dev/null +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/AddWhenElseBranchFix.java @@ -0,0 +1,75 @@ +/* + * Copyright 2010-2012 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.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.JetWhenExpression; +import org.jetbrains.jet.plugin.JetBundle; + +public class AddWhenElseBranchFix extends JetIntentionAction { + public AddWhenElseBranchFix(@NotNull JetWhenExpression element) { + super(element); + } + + @NotNull + @Override + public String getText() { + return JetBundle.message("add.when.else.branch.action"); + } + + @NotNull + @Override + public String getFamilyName() { + return JetBundle.message("add.when.else.branch.action.family.name"); + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { + return super.isAvailable(project, editor, file) && element.getCloseBraceNode() != null; + } + + @Override + public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + PsiElement insertBeforeAnchor = element.getCloseBraceNode(); + if (insertBeforeAnchor != null) { + PsiElement insertedBranch = element.addBefore(JetPsiFactory.createElseWhenEntry(project), insertBeforeAnchor); + element.addAfter(JetPsiFactory.createNewLineWhiteSpace(project), insertedBranch); + } + } + + 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 AddWhenElseBranchFix(whenExpression); + } + }; + } +} diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java index 172527bced4..96def177297 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/QuickFixes.java @@ -142,5 +142,7 @@ public class QuickFixes { actions.put(UNNECESSARY_NOT_NULL_ASSERTION, ExclExclCallFix.removeExclExclCall()); actions.put(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE, new SpecifyTypeExplicitlyFix()); + + factories.put(NO_ELSE_IN_WHEN, AddWhenElseBranchFix.createFactory()); } } diff --git a/idea/testData/quickfix/when/afterNoElseInWhenWithBranches.kt b/idea/testData/quickfix/when/afterNoElseInWhenWithBranches.kt new file mode 100644 index 00000000000..8cea816ca2d --- /dev/null +++ b/idea/testData/quickfix/when/afterNoElseInWhenWithBranches.kt @@ -0,0 +1,10 @@ +// "Add else branch" "true" +fun test() { + val a = 12 + when (a) { + in 0..11 -> { /* some code */ } + 12, 13, 14 -> { /* some code */ } + else -> { + } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/when/afterNoElseInWhenWithoutBranches.kt b/idea/testData/quickfix/when/afterNoElseInWhenWithoutBranches.kt new file mode 100644 index 00000000000..06bbe64c643 --- /dev/null +++ b/idea/testData/quickfix/when/afterNoElseInWhenWithoutBranches.kt @@ -0,0 +1,8 @@ +// "Add else branch" "true" +fun test() { + val a = 12 + when (a) { + else -> { + } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/when/beforeNoElseInWhenWithBranches.kt b/idea/testData/quickfix/when/beforeNoElseInWhenWithBranches.kt new file mode 100644 index 00000000000..7463b2169e6 --- /dev/null +++ b/idea/testData/quickfix/when/beforeNoElseInWhenWithBranches.kt @@ -0,0 +1,8 @@ +// "Add else branch" "true" +fun test() { + val a = 12 + when (a) { + in 0..11 -> { /* some code */ } + 12, 13, 14 -> { /* some code */ } + } +} \ No newline at end of file diff --git a/idea/testData/quickfix/when/beforeNoElseInWhenWithoutBranches.kt b/idea/testData/quickfix/when/beforeNoElseInWhenWithoutBranches.kt new file mode 100644 index 00000000000..d351cfccca6 --- /dev/null +++ b/idea/testData/quickfix/when/beforeNoElseInWhenWithoutBranches.kt @@ -0,0 +1,6 @@ +// "Add else branch" "true" +fun test() { + val a = 12 + when (a) { + } +} \ No newline at end of file