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 675258e266a..ce49e79196f 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetPsiFactory.java @@ -152,9 +152,15 @@ 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 JetWhenEntry createWhenEntry(@NotNull Project project, @NotNull String entryText) { + JetNamedFunction function = createFunction(project, "fun foo() { when(12) { " + entryText + " } }"); + JetWhenEntry whenEntry = PsiTreeUtil.findChildOfType(function, JetWhenEntry.class); + + assert whenEntry != null : "Couldn't generate when entry"; + assert entryText.equals(whenEntry.getText()) : "Generate when entry text differs from the given text"; + + return whenEntry; } @NotNull diff --git a/idea/src/org/jetbrains/jet/plugin/quickfix/AddWhenElseBranchFix.java b/idea/src/org/jetbrains/jet/plugin/quickfix/AddWhenElseBranchFix.java index 53db5ed1a85..84a07a4a932 100644 --- a/idea/src/org/jetbrains/jet/plugin/quickfix/AddWhenElseBranchFix.java +++ b/idea/src/org/jetbrains/jet/plugin/quickfix/AddWhenElseBranchFix.java @@ -16,8 +16,10 @@ 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.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; @@ -26,14 +28,18 @@ 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 AddWhenElseBranchFix extends JetIntentionAction { + private static final String ELSE_ENTRY_TEXT = "else -> {}"; + public AddWhenElseBranchFix(@NotNull JetWhenExpression element) { super(element); } + @NotNull @Override public String getText() { @@ -53,11 +59,19 @@ public class AddWhenElseBranchFix extends JetIntentionAction @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.createNewLine(project), insertedBranch); - } + PsiElement whenCloseBrace = element.getCloseBraceNode(); + assert (whenCloseBrace != null) : "isAvailable should check if close brace exist"; + + JetWhenEntry entry = JetPsiFactory.createWhenEntry(project, ELSE_ENTRY_TEXT); + + PsiElement insertedBranch = element.addBefore(entry, whenCloseBrace); + element.addAfter(JetPsiFactory.createNewLine(project), insertedBranch); + + JetWhenEntry insertedWhenEntry = (JetWhenEntry) CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(insertedBranch); + final TextRange textRange = insertedWhenEntry.getTextRange(); + + int indexOfOpenBrace = insertedWhenEntry.getText().indexOf('{'); + editor.getCaretModel().moveToOffset(textRange.getStartOffset() + indexOfOpenBrace + 1); } public static JetIntentionActionFactory createFactory() { diff --git a/idea/testData/quickfix/when/afterNoElseInWhenWithBranches.kt b/idea/testData/quickfix/when/afterNoElseInWhenWithBranches.kt index 8cea816ca2d..fa3123674fe 100644 --- a/idea/testData/quickfix/when/afterNoElseInWhenWithBranches.kt +++ b/idea/testData/quickfix/when/afterNoElseInWhenWithBranches.kt @@ -1,10 +1,10 @@ // "Add else branch" "true" fun test() { val a = 12 - when (a) { + when (a) { in 0..11 -> { /* some code */ } 12, 13, 14 -> { /* some code */ } - else -> { + else -> { } } } \ No newline at end of file diff --git a/idea/testData/quickfix/when/afterNoElseInWhenWithoutBranches.kt b/idea/testData/quickfix/when/afterNoElseInWhenWithoutBranches.kt index 06bbe64c643..2cc67f46998 100644 --- a/idea/testData/quickfix/when/afterNoElseInWhenWithoutBranches.kt +++ b/idea/testData/quickfix/when/afterNoElseInWhenWithoutBranches.kt @@ -2,7 +2,7 @@ fun test() { val a = 12 when (a) { - else -> { + else -> { } } } \ No newline at end of file