From 514b70a0538c9d449559bdabaf4a160bc90aecef Mon Sep 17 00:00:00 2001 From: Andrey Cherkasov Date: Wed, 24 Jul 2024 22:43:11 +0400 Subject: [PATCH] [kotlin] Port MoveWhenElseBranchFix to K2 ^KTIJ-29518 GitOrigin-RevId: 9a9b4c59ce3714c0c80a4ee3de54d1652d0ee91c --- .../fixes/KotlinK2QuickFixRegistrar.kt | 1 + .../fixes/MoveWhenElseBranchFixFactory.kt | 19 +++++++++++++++++++ .../quickfix/when/elseNotLastInWhen.kt | 4 ++-- .../quickfix/when/elseNotLastInWhen.kt.after | 4 ++-- 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/MoveWhenElseBranchFixFactory.kt diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt index 19aea02b311b..9686b601a236 100644 --- a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/KotlinK2QuickFixRegistrar.kt @@ -127,6 +127,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() { registerFactory(AddDefaultConstructorFixFactory.addDefaultConstructorFixFactory) registerFactory(MakeClassAnAnnotationClassFixFactory.makeClassAnAnnotationClassFixFactory) registerFactory(WrongLongSuffixFixFactory.changeToCorrectLongSuffix) + registerFactory(MoveWhenElseBranchFixFactory.moveWhenElseBranchFixFactory) } private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix { diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/MoveWhenElseBranchFixFactory.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/MoveWhenElseBranchFixFactory.kt new file mode 100644 index 000000000000..9e10e0ccd7f1 --- /dev/null +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/MoveWhenElseBranchFixFactory.kt @@ -0,0 +1,19 @@ +// 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.k2.codeinsight.fixes + +import org.jetbrains.kotlin.analysis.api.fir.diagnostics.KaFirDiagnostic +import org.jetbrains.kotlin.idea.codeinsight.api.applicators.fixes.KotlinQuickFixFactory +import org.jetbrains.kotlin.idea.quickfix.MoveWhenElseBranchFix +import org.jetbrains.kotlin.psi.KtWhenExpression +import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType + +internal object MoveWhenElseBranchFixFactory { + + val moveWhenElseBranchFixFactory = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.ElseMisplacedInWhen -> + val whenExpression = diagnostic.psi.getNonStrictParentOfType() ?: return@IntentionBased emptyList() + + listOf( + MoveWhenElseBranchFix(whenExpression) + ) + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/when/elseNotLastInWhen.kt b/plugins/kotlin/idea/tests/testData/quickfix/when/elseNotLastInWhen.kt index 7a5f2971e78c..58373ba7f272 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/when/elseNotLastInWhen.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/when/elseNotLastInWhen.kt @@ -7,6 +7,6 @@ fun test() { 2 -> { /* some more code */ } } } -/* IGNORE_K2 */ -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveWhenElseBranchFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveWhenElseBranchFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveWhenElseBranchFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/when/elseNotLastInWhen.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/when/elseNotLastInWhen.kt.after index ed5e5f5102fe..8678a0fd3b90 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/when/elseNotLastInWhen.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/when/elseNotLastInWhen.kt.after @@ -7,6 +7,6 @@ fun test() { else -> { /* other code */ } } } -/* IGNORE_K2 */ -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveWhenElseBranchFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveWhenElseBranchFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveWhenElseBranchFix \ No newline at end of file