Add Else Branch to When Quick-Fix: Convert to Kotlin & refactor
This commit is contained in:
@@ -147,8 +147,6 @@ migrate.lambda.syntax=Migrate lambda syntax
|
|||||||
migrate.lambda.syntax.family=Migrate lambda syntax
|
migrate.lambda.syntax.family=Migrate lambda syntax
|
||||||
remove.val.var.from.parameter=Remove ''{0}'' from parameter
|
remove.val.var.from.parameter=Remove ''{0}'' from parameter
|
||||||
add.override.to.equals.hashCode.toString=Add 'override' to equals, hashCode, toString in project
|
add.override.to.equals.hashCode.toString=Add 'override' to equals, hashCode, toString 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.action=Move else branch to the end
|
||||||
move.when.else.branch.to.the.end.family.name=Move else branch to the end
|
move.when.else.branch.to.the.end.family.name=Move else branch to the end
|
||||||
change.to.property.name.family.name=Change to property name
|
change.to.property.name.family.name=Change to property name
|
||||||
|
|||||||
@@ -14,75 +14,36 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.idea.quickfix;
|
package org.jetbrains.kotlin.idea.quickfix
|
||||||
|
|
||||||
import com.intellij.codeInsight.CodeInsightUtilCore;
|
import com.intellij.codeInsight.CodeInsightUtilCore
|
||||||
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.PsiFile
|
||||||
import com.intellij.psi.PsiElement;
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import com.intellij.psi.PsiFile;
|
import org.jetbrains.kotlin.psi.*
|
||||||
import com.intellij.psi.util.PsiTreeUtil;
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
import com.intellij.util.IncorrectOperationException;
|
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic;
|
|
||||||
import org.jetbrains.kotlin.idea.KotlinBundle;
|
|
||||||
import org.jetbrains.kotlin.psi.*;
|
|
||||||
|
|
||||||
public class AddWhenElseBranchFix extends KotlinQuickFixAction<KtWhenExpression> {
|
class AddWhenElseBranchFix(element: KtWhenExpression) : KotlinQuickFixAction<KtWhenExpression>(element) {
|
||||||
private static final String ELSE_ENTRY_TEXT = "else -> {}";
|
override fun getFamilyName() = "Add else branch"
|
||||||
|
override fun getText() = familyName
|
||||||
|
|
||||||
public AddWhenElseBranchFix(@NotNull KtWhenExpression element) {
|
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean {
|
||||||
super(element);
|
return super.isAvailable(project, editor, file) && element.closeBrace != null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||||
@NotNull
|
val psiFactory = KtPsiFactory(file)
|
||||||
@Override
|
val entry = psiFactory.createWhenEntry("else -> {}")
|
||||||
public String getText() {
|
val whenCloseBrace = element.closeBrace ?: error("isAvailable should check if close brace exist")
|
||||||
return KotlinBundle.message("add.when.else.branch.action");
|
val insertedWhenEntry = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(element.addBefore(entry, whenCloseBrace)) as KtWhenEntry
|
||||||
|
editor!!.caretModel.moveToOffset((insertedWhenEntry.expression as KtBlockExpression).lBrace!!.endOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
companion object : KotlinSingleIntentionActionFactory() {
|
||||||
@Override
|
public override fun createAction(diagnostic: Diagnostic): AddWhenElseBranchFix? {
|
||||||
public String getFamilyName() {
|
return diagnostic.psiElement.getNonStrictParentOfType<KtWhenExpression>()?.let { AddWhenElseBranchFix(it) }
|
||||||
return KotlinBundle.message("add.when.else.branch.action.family.name");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiFile file) {
|
|
||||||
return super.isAvailable(project, editor, file) && getElement().getCloseBrace() != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void invoke(@NotNull Project project, Editor editor, @NotNull KtFile file) throws IncorrectOperationException {
|
|
||||||
PsiElement whenCloseBrace = getElement().getCloseBrace();
|
|
||||||
assert (whenCloseBrace != null) : "isAvailable should check if close brace exist";
|
|
||||||
|
|
||||||
KtPsiFactory psiFactory = KtPsiFactoryKt.KtPsiFactory(file);
|
|
||||||
KtWhenEntry entry = psiFactory.createWhenEntry(ELSE_ENTRY_TEXT);
|
|
||||||
|
|
||||||
PsiElement insertedBranch = getElement().addBefore(entry, whenCloseBrace);
|
|
||||||
getElement().addAfter(psiFactory.createNewLine(), insertedBranch);
|
|
||||||
|
|
||||||
KtWhenEntry insertedWhenEntry = (KtWhenEntry) CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(insertedBranch);
|
|
||||||
TextRange textRange = insertedWhenEntry.getTextRange();
|
|
||||||
|
|
||||||
int indexOfOpenBrace = insertedWhenEntry.getText().indexOf('{');
|
|
||||||
editor.getCaretModel().moveToOffset(textRange.getStartOffset() + indexOfOpenBrace + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static KotlinSingleIntentionActionFactory createFactory() {
|
|
||||||
return new KotlinSingleIntentionActionFactory() {
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public KotlinQuickFixAction createAction(@NotNull Diagnostic diagnostic) {
|
|
||||||
PsiElement element = diagnostic.getPsiElement();
|
|
||||||
KtWhenExpression whenExpression = PsiTreeUtil.getParentOfType(element, KtWhenExpression.class, false);
|
|
||||||
if (whenExpression == null) return null;
|
|
||||||
return new AddWhenElseBranchFix(whenExpression);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -187,7 +187,7 @@ class QuickFixRegistrar : QuickFixContributor {
|
|||||||
AMBIGUOUS_ANONYMOUS_TYPE_INFERRED.registerActions(SpecifyTypeExplicitlyFix())
|
AMBIGUOUS_ANONYMOUS_TYPE_INFERRED.registerActions(SpecifyTypeExplicitlyFix())
|
||||||
|
|
||||||
ELSE_MISPLACED_IN_WHEN.registerFactory(MoveWhenElseBranchFix.createFactory())
|
ELSE_MISPLACED_IN_WHEN.registerFactory(MoveWhenElseBranchFix.createFactory())
|
||||||
NO_ELSE_IN_WHEN.registerFactory(AddWhenElseBranchFix.createFactory())
|
NO_ELSE_IN_WHEN.registerFactory(AddWhenElseBranchFix)
|
||||||
NO_ELSE_IN_WHEN.registerFactory(AddWhenRemainingBranchesFix)
|
NO_ELSE_IN_WHEN.registerFactory(AddWhenRemainingBranchesFix)
|
||||||
BREAK_OR_CONTINUE_IN_WHEN.registerFactory(AddLoopLabelFix)
|
BREAK_OR_CONTINUE_IN_WHEN.registerFactory(AddLoopLabelFix)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user