From 7cb5bc89023073f4d141aeb116efb9218f14f81e Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Fri, 25 Dec 2015 14:47:18 +0300 Subject: [PATCH] Add Else Branch to When Quick-Fix: Convert to Kotlin & refactor --- .../kotlin/idea/KotlinBundle.properties | 2 - .../idea/quickfix/AddWhenElseBranchFix.kt | 87 +++++-------------- .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 2 +- 3 files changed, 25 insertions(+), 66 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties index 779db143fb0..97d8b8e5202 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/KotlinBundle.properties @@ -147,8 +147,6 @@ migrate.lambda.syntax=Migrate lambda syntax migrate.lambda.syntax.family=Migrate lambda syntax remove.val.var.from.parameter=Remove ''{0}'' from parameter 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.family.name=Move else branch to the end change.to.property.name.family.name=Change to property name diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddWhenElseBranchFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddWhenElseBranchFix.kt index 6f4517c279d..7852f8dcc81 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddWhenElseBranchFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddWhenElseBranchFix.kt @@ -14,75 +14,36 @@ * limitations under the License. */ -package org.jetbrains.kotlin.idea.quickfix; +package org.jetbrains.kotlin.idea.quickfix -import com.intellij.codeInsight.CodeInsightUtilCore; -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; -import com.intellij.util.IncorrectOperationException; -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.*; +import com.intellij.codeInsight.CodeInsightUtilCore +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiFile +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType -public class AddWhenElseBranchFix extends KotlinQuickFixAction { - private static final String ELSE_ENTRY_TEXT = "else -> {}"; +class AddWhenElseBranchFix(element: KtWhenExpression) : KotlinQuickFixAction(element) { + override fun getFamilyName() = "Add else branch" + override fun getText() = familyName - public AddWhenElseBranchFix(@NotNull KtWhenExpression element) { - super(element); + override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean { + return super.isAvailable(project, editor, file) && element.closeBrace != null } - - @NotNull - @Override - public String getText() { - return KotlinBundle.message("add.when.else.branch.action"); + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + val psiFactory = KtPsiFactory(file) + val entry = psiFactory.createWhenEntry("else -> {}") + val whenCloseBrace = element.closeBrace ?: error("isAvailable should check if close brace exist") + val insertedWhenEntry = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(element.addBefore(entry, whenCloseBrace)) as KtWhenEntry + editor!!.caretModel.moveToOffset((insertedWhenEntry.expression as KtBlockExpression).lBrace!!.endOffset) } - @NotNull - @Override - public String getFamilyName() { - 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); - } - }; + companion object : KotlinSingleIntentionActionFactory() { + public override fun createAction(diagnostic: Diagnostic): AddWhenElseBranchFix? { + return diagnostic.psiElement.getNonStrictParentOfType()?.let { AddWhenElseBranchFix(it) } + } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 56120346091..a6d74629a16 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -187,7 +187,7 @@ class QuickFixRegistrar : QuickFixContributor { AMBIGUOUS_ANONYMOUS_TYPE_INFERRED.registerActions(SpecifyTypeExplicitlyFix()) 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) BREAK_OR_CONTINUE_IN_WHEN.registerFactory(AddLoopLabelFix)