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