Move caret into generated else branch
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<JetWhenExpression> {
|
||||
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<JetWhenExpression>
|
||||
|
||||
@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() {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
// "Add else branch" "true"
|
||||
fun test() {
|
||||
val a = 12
|
||||
wh<caret>en (a) {
|
||||
when (a) {
|
||||
in 0..11 -> { /* some code */ }
|
||||
12, 13, 14 -> { /* some code */ }
|
||||
else -> {
|
||||
else -> {<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
fun test() {
|
||||
val a = 12
|
||||
when (a) {
|
||||
else -> {
|
||||
else -> {<caret>
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user