mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-02-04 23:39:07 +07:00
[kotlin] Convert MoveWhenElseBranchFix to ModCommandAction
^KTIJ-29518 GitOrigin-RevId: 802da378d1df897c6078d66ef1bcda484c572a07
This commit is contained in:
committed by
intellij-monorepo-bot
parent
59cd1cbd87
commit
6e72accd1a
@@ -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<KtWhenExpression>() ?: return@IntentionBased emptyList()
|
||||
val moveWhenElseBranch = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.ElseMisplacedInWhen ->
|
||||
val whenExpression = diagnostic.psi.getNonStrictParentOfType<KtWhenExpression>() ?: return@ModCommandBased emptyList()
|
||||
|
||||
listOf(
|
||||
MoveWhenElseBranchFix(whenExpression)
|
||||
listOfNotNull(
|
||||
MoveWhenElseBranchFix.createIfApplicable(whenExpression)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -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<KtWhenExpression>(element) {
|
||||
class MoveWhenElseBranchFix private constructor(
|
||||
element: KtWhenExpression
|
||||
) : KotlinPsiUpdateModCommandAction.ElementBased<KtWhenExpression, Unit>(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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<KtWhenExpression>() ?: return null
|
||||
return MoveWhenElseBranchFix(whenExpression)
|
||||
|
||||
return MoveWhenElseBranchFix.createIfApplicable(whenExpression)?.asIntention()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user