QuickFix for ELSE_MISPLACED_IN_WHEN.
This commit is contained in:
committed by
Evgeny Gerashchenko
parent
8d71840255
commit
e9613b95ac
@@ -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
|
||||
|
||||
@@ -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<JetWhenExpression> {
|
||||
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
@@ -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 */ }
|
||||
el<caret>se -> { /* other code */ }
|
||||
}
|
||||
}
|
||||
@@ -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 */ }
|
||||
el<caret>se -> { /* first else branch */ }
|
||||
else -> { /* second else branch */ }
|
||||
3 -> { /* some other code */ }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Move else branch to the end" "true"
|
||||
fun test() {
|
||||
val a = 12
|
||||
when (a) {
|
||||
1 -> { /* some code */ }
|
||||
el<caret>se -> { /* other code */ }
|
||||
2 -> { /* some more code */ }
|
||||
}
|
||||
}
|
||||
@@ -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 */ }
|
||||
el<caret>se -> { /* first else branch */ }
|
||||
else -> { /* second else branch */ }
|
||||
3 -> { /* some other code */ }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user