Move caret into generated else branch
This commit is contained in:
@@ -152,9 +152,15 @@ public class JetPsiFactory {
|
|||||||
return function.getValueParameters().get(0);
|
return function.getValueParameters().get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JetWhenEntry createElseWhenEntry(Project project) {
|
@NotNull
|
||||||
JetNamedFunction function = createFunction(project, "fun foo() { when(12) { else -> { } } }");
|
public static JetWhenEntry createWhenEntry(@NotNull Project project, @NotNull String entryText) {
|
||||||
return PsiTreeUtil.findChildOfType(function, JetWhenEntry.class);
|
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
|
@NotNull
|
||||||
|
|||||||
@@ -16,8 +16,10 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.plugin.quickfix;
|
package org.jetbrains.jet.plugin.quickfix;
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.CodeInsightUtilBase;
|
||||||
import com.intellij.openapi.editor.Editor;
|
import com.intellij.openapi.editor.Editor;
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project;
|
||||||
|
import com.intellij.openapi.util.TextRange;
|
||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.psi.PsiElement;
|
||||||
import com.intellij.psi.PsiFile;
|
import com.intellij.psi.PsiFile;
|
||||||
import com.intellij.psi.util.PsiTreeUtil;
|
import com.intellij.psi.util.PsiTreeUtil;
|
||||||
@@ -26,14 +28,18 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||||
import org.jetbrains.jet.lang.psi.JetPsiFactory;
|
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.lang.psi.JetWhenExpression;
|
||||||
import org.jetbrains.jet.plugin.JetBundle;
|
import org.jetbrains.jet.plugin.JetBundle;
|
||||||
|
|
||||||
public class AddWhenElseBranchFix extends JetIntentionAction<JetWhenExpression> {
|
public class AddWhenElseBranchFix extends JetIntentionAction<JetWhenExpression> {
|
||||||
|
private static final String ELSE_ENTRY_TEXT = "else -> {}";
|
||||||
|
|
||||||
public AddWhenElseBranchFix(@NotNull JetWhenExpression element) {
|
public AddWhenElseBranchFix(@NotNull JetWhenExpression element) {
|
||||||
super(element);
|
super(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public String getText() {
|
public String getText() {
|
||||||
@@ -53,11 +59,19 @@ public class AddWhenElseBranchFix extends JetIntentionAction<JetWhenExpression>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||||
PsiElement insertBeforeAnchor = element.getCloseBraceNode();
|
PsiElement whenCloseBrace = element.getCloseBraceNode();
|
||||||
if (insertBeforeAnchor != null) {
|
assert (whenCloseBrace != null) : "isAvailable should check if close brace exist";
|
||||||
PsiElement insertedBranch = element.addBefore(JetPsiFactory.createElseWhenEntry(project), insertBeforeAnchor);
|
|
||||||
element.addAfter(JetPsiFactory.createNewLine(project), insertedBranch);
|
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() {
|
public static JetIntentionActionFactory createFactory() {
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
// "Add else branch" "true"
|
// "Add else branch" "true"
|
||||||
fun test() {
|
fun test() {
|
||||||
val a = 12
|
val a = 12
|
||||||
wh<caret>en (a) {
|
when (a) {
|
||||||
in 0..11 -> { /* some code */ }
|
in 0..11 -> { /* some code */ }
|
||||||
12, 13, 14 -> { /* some code */ }
|
12, 13, 14 -> { /* some code */ }
|
||||||
else -> {
|
else -> {<caret>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
fun test() {
|
fun test() {
|
||||||
val a = 12
|
val a = 12
|
||||||
when (a) {
|
when (a) {
|
||||||
else -> {
|
else -> {<caret>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user