From 6e72accd1a4c38895f715cd9dafcfe3a36461ca2 Mon Sep 17 00:00:00 2001 From: Andrey Cherkasov Date: Sun, 28 Jul 2024 02:24:10 +0400 Subject: [PATCH] [kotlin] Convert MoveWhenElseBranchFix to ModCommandAction ^KTIJ-29518 GitOrigin-RevId: 802da378d1df897c6078d66ef1bcda484c572a07 --- .../fixes/ElseMisplacedInWhenFixFactory.kt | 8 ++-- .../idea/quickfix/MoveWhenElseBranchFix.kt | 41 ++++++++++--------- .../quickfix/MoveWhenElseBranchFixFactory.kt | 6 ++- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/ElseMisplacedInWhenFixFactory.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/ElseMisplacedInWhenFixFactory.kt index 76ff0a93239d..eb44eb3c4f11 100644 --- a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/ElseMisplacedInWhenFixFactory.kt +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/ElseMisplacedInWhenFixFactory.kt @@ -9,11 +9,11 @@ import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType internal object ElseMisplacedInWhenFixFactory { - val moveWhenElseBranch = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.ElseMisplacedInWhen -> - val whenExpression = diagnostic.psi.getNonStrictParentOfType() ?: return@IntentionBased emptyList() + val moveWhenElseBranch = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.ElseMisplacedInWhen -> + val whenExpression = diagnostic.psi.getNonStrictParentOfType() ?: return@ModCommandBased emptyList() - listOf( - MoveWhenElseBranchFix(whenExpression) + listOfNotNull( + MoveWhenElseBranchFix.createIfApplicable(whenExpression) ) } } \ No newline at end of file diff --git a/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/MoveWhenElseBranchFix.kt b/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/MoveWhenElseBranchFix.kt index 072e5a612707..544912f206d9 100644 --- a/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/MoveWhenElseBranchFix.kt +++ b/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/MoveWhenElseBranchFix.kt @@ -1,39 +1,42 @@ // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. 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.modcommand.ActionContext +import com.intellij.modcommand.ModPsiUpdater +import com.intellij.psi.util.startOffset import org.jetbrains.kotlin.idea.base.resources.KotlinBundle -import org.jetbrains.kotlin.idea.codeinsight.api.classic.quickfixes.KotlinQuickFixAction -import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.idea.codeinsight.api.applicable.intentions.KotlinPsiUpdateModCommandAction import org.jetbrains.kotlin.psi.KtPsiUtil import org.jetbrains.kotlin.psi.KtWhenEntry import org.jetbrains.kotlin.psi.KtWhenExpression -class MoveWhenElseBranchFix(element: KtWhenExpression) : KotlinQuickFixAction(element) { +class MoveWhenElseBranchFix private constructor( + element: KtWhenExpression +) : KotlinPsiUpdateModCommandAction.ElementBased(element, Unit) { override fun getFamilyName() = KotlinBundle.message("move.else.branch.to.the.end") - override fun getText() = familyName - - override fun isAvailable(project: Project, editor: Editor?, file: KtFile): Boolean { - val element = element ?: return false - return KtPsiUtil.checkWhenExpressionHasSingleElse(element) - } - - override fun invoke(project: Project, editor: Editor?, file: KtFile) { - val element = element ?: return + override fun invoke( + actionContext: ActionContext, + element: KtWhenExpression, + elementContext: Unit, + updater: ModPsiUpdater, + ) { val entries = element.entries val lastEntry = entries.lastOrNull() ?: return val elseEntry = entries.singleOrNull { it.isElse } ?: return - - val cursorOffset = editor!!.caretModel.offset - elseEntry.textOffset + val cursorOffset = actionContext.offset - elseEntry.textOffset val insertedBranch = element.addAfter(elseEntry, lastEntry) as KtWhenEntry elseEntry.delete() - val insertedWhenEntry = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(insertedBranch) ?: return - editor.caretModel.moveToOffset(insertedWhenEntry.textOffset + cursorOffset) + updater.moveCaretTo(insertedBranch.startOffset + cursorOffset) } + companion object { + fun createIfApplicable(element: KtWhenExpression): MoveWhenElseBranchFix? { + return if (KtPsiUtil.checkWhenExpressionHasSingleElse(element)) + MoveWhenElseBranchFix(element) + else null + } + } } diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveWhenElseBranchFixFactory.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveWhenElseBranchFixFactory.kt index 362aa0a917b0..284cd34df518 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveWhenElseBranchFixFactory.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveWhenElseBranchFixFactory.kt @@ -2,13 +2,15 @@ package org.jetbrains.kotlin.idea.quickfix +import com.intellij.codeInsight.intention.IntentionAction import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.psi.KtWhenExpression import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType internal object MoveWhenElseBranchFixFactory : KotlinSingleIntentionActionFactory() { - override fun createAction(diagnostic: Diagnostic): MoveWhenElseBranchFix? { + override fun createAction(diagnostic: Diagnostic): IntentionAction? { val whenExpression = diagnostic.psiElement.getNonStrictParentOfType() ?: return null - return MoveWhenElseBranchFix(whenExpression) + + return MoveWhenElseBranchFix.createIfApplicable(whenExpression)?.asIntention() } }