[kotlin] Make CommaInWhenConditionWithoutArgumentFix available for K2 and convert it to ModCommandAction

^KTIJ-29485

GitOrigin-RevId: da6d36ce9d4ee7dd21d514951a2bffade68ad348
This commit is contained in:
Andrey Cherkasov
2024-10-02 12:27:49 +00:00
committed by intellij-monorepo-bot
parent d739f0f122
commit ebeda47617
15 changed files with 127 additions and 70 deletions
@@ -0,0 +1,16 @@
// 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.CommaInWhenConditionWithoutArgumentFix
import org.jetbrains.kotlin.psi.KtWhenExpression
internal object CommaInWhenConditionWithoutArgumentFixFactories {
val replaceCommaWithOrFixFactory = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.CommaInWhenConditionWithoutArgument ->
listOfNotNull(
(diagnostic.psi.parent as? KtWhenExpression)?.let(::CommaInWhenConditionWithoutArgumentFix)
)
}
}
@@ -371,7 +371,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() {
// TODO: NON_EXHAUSTIVE_WHEN[_ON_SEALED_CLASS] will be replaced in future. We need to register the fix for those diagnostics as well
registerPsiQuickFixes(KaFirDiagnostic.NoElseInWhen::class, AddWhenElseBranchFix)
registerFactory(AddWhenRemainingBranchFixFactories.noElseInWhen)
registerPsiQuickFixes(KaFirDiagnostic.CommaInWhenConditionWithoutArgument::class, CommaInWhenConditionWithoutArgumentFix)
registerFactory(CommaInWhenConditionWithoutArgumentFixFactories.replaceCommaWithOrFixFactory)
registerPsiQuickFixes(KaFirDiagnostic.SenselessNullInWhen::class, RemoveWhenBranchFix)
}
@@ -9940,6 +9940,30 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/quickfix/migration/commasInWhenWithoutArgument")
public static class CommasInWhenWithoutArgument extends AbstractHighLevelQuickFixTest {
@java.lang.Override
@org.jetbrains.annotations.NotNull
public final KotlinPluginMode getPluginMode() {
return KotlinPluginMode.K2;
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("commasInConditionWithNoArguments.kt")
public void testCommasInConditionWithNoArguments() throws Exception {
runTest("../../../idea/tests/testData/quickfix/migration/commasInWhenWithoutArgument/commasInConditionWithNoArguments.kt");
}
@TestMetadata("commasInConditionWithNoArguments2.kt")
public void testCommasInConditionWithNoArguments2() throws Exception {
runTest("../../../idea/tests/testData/quickfix/migration/commasInWhenWithoutArgument/commasInConditionWithNoArguments2.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/quickfix/migration/missingConstructorKeyword")
public static class MissingConstructorKeyword extends AbstractHighLevelQuickFixTest {
@@ -5,81 +5,76 @@
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
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.modcommand.PsiUpdateModCommandAction
import com.intellij.psi.PsiElement
import com.intellij.util.containers.addIfNotNull
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.codeinsight.api.classic.quickfixes.CleanupFix
import org.jetbrains.kotlin.idea.codeinsight.api.classic.quickfixes.KotlinPsiOnlyQuickFixAction
import org.jetbrains.kotlin.idea.codeinsight.api.classic.quickfixes.PsiElementSuitabilityCheckers
import org.jetbrains.kotlin.idea.codeinsight.api.classic.quickfixes.QuickFixesPsiBasedFactory
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
class CommaInWhenConditionWithoutArgumentFix(element: KtWhenExpression) : KotlinPsiOnlyQuickFixAction<KtWhenExpression>(element),
CleanupFix {
override fun getFamilyName(): String = text
override fun getText(): String = KotlinBundle.message("replace.with.in.when")
class CommaInWhenConditionWithoutArgumentFix(
element: KtWhenExpression,
) : PsiUpdateModCommandAction<KtWhenExpression>(element), CleanupFix.ModCommand {
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val whenExpression = element ?: return
replaceCommasWithOrsInWhenExpression(whenExpression)
override fun getFamilyName(): String = KotlinBundle.message("replace.with.in.when")
override fun invoke(
context: ActionContext,
element: KtWhenExpression,
updater: ModPsiUpdater,
) {
replaceCommasWithOrsInWhenExpression(element)
}
companion object : QuickFixesPsiBasedFactory<PsiElement>(PsiElement::class, PsiElementSuitabilityCheckers.ALWAYS_SUITABLE) {
override fun doCreateQuickFix(psiElement: PsiElement): List<IntentionAction> {
return listOfNotNull((psiElement.parent?.parent as? KtWhenExpression)?.let(::CommaInWhenConditionWithoutArgumentFix))
}
private class WhenEntryConditionsData(
val conditions: List<KtExpression>,
val first: PsiElement,
val last: PsiElement,
val arrow: PsiElement
)
private class WhenEntryConditionsData(
val conditions: List<KtExpression>,
val first: PsiElement,
val last: PsiElement,
val arrow: PsiElement
)
private fun replaceCommasWithOrsInWhenExpression(whenExpression: KtWhenExpression) {
for (whenEntry in whenExpression.entries) {
if (whenEntry.conditions.size > 1) {
val conditionsData = getConditionsDataOrNull(whenEntry) ?: return
// Leave branch untouched if there are no valid conditions
if (conditionsData.conditions.isEmpty()) continue
val replacement = KtPsiFactory(whenEntry.project).buildExpression {
appendExpressions(conditionsData.conditions, separator = "||")
}
whenEntry.deleteChildRange(conditionsData.first, conditionsData.last)
whenEntry.addBefore(replacement, conditionsData.arrow)
private fun replaceCommasWithOrsInWhenExpression(whenExpression: KtWhenExpression) {
for (whenEntry in whenExpression.entries) {
if (whenEntry.conditions.size > 1) {
val conditionsData = getConditionsDataOrNull(whenEntry) ?: return
// Leave branch untouched if there are no valid conditions
if (conditionsData.conditions.isEmpty()) continue
val replacement = KtPsiFactory(whenEntry.project).buildExpression {
appendExpressions(conditionsData.conditions, separator = "||")
}
whenEntry.deleteChildRange(conditionsData.first, conditionsData.last)
whenEntry.addBefore(replacement, conditionsData.arrow)
}
}
}
private fun getConditionsDataOrNull(whenEntry: KtWhenEntry): WhenEntryConditionsData? {
val conditions = mutableListOf<KtExpression>()
private fun getConditionsDataOrNull(whenEntry: KtWhenEntry): WhenEntryConditionsData? {
val conditions = mutableListOf<KtExpression>()
var arrow: PsiElement? = null
var arrow: PsiElement? = null
var child = whenEntry.firstChild
whenEntryChildren@ while (child != null) {
when {
child is KtWhenConditionWithExpression -> {
conditions.addIfNotNull(child.expression)
}
child.node.elementType == KtTokens.ARROW -> {
arrow = child
break@whenEntryChildren
}
var child = whenEntry.firstChild
whenEntryChildren@ while (child != null) {
when {
child is KtWhenConditionWithExpression -> {
conditions.addIfNotNull(child.expression)
}
child.node.elementType == KtTokens.ARROW -> {
arrow = child
break@whenEntryChildren
}
child = child.nextSibling
}
val last = child?.prevSibling
return if (arrow != null && last != null)
WhenEntryConditionsData(conditions, whenEntry.firstChild, last, arrow)
else
null
child = child.nextSibling
}
val last = child?.prevSibling
return if (arrow != null && last != null)
WhenEntryConditionsData(conditions, whenEntry.firstChild, last, arrow)
else
null
}
}
@@ -0,0 +1,17 @@
// 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.intention.IntentionAction
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.codeinsight.api.classic.quickfixes.PsiElementSuitabilityCheckers
import org.jetbrains.kotlin.idea.codeinsight.api.classic.quickfixes.QuickFixesPsiBasedFactory
import org.jetbrains.kotlin.psi.KtWhenExpression
internal object CommaInWhenConditionWithoutArgumentFixFactory : QuickFixesPsiBasedFactory<PsiElement>(PsiElement::class, PsiElementSuitabilityCheckers.ALWAYS_SUITABLE) {
override fun doCreateQuickFix(psiElement: PsiElement): List<IntentionAction> {
return listOfNotNull(
(psiElement.parent?.parent as? KtWhenExpression)?.let { CommaInWhenConditionWithoutArgumentFix(it).asIntention() }
)
}
}
@@ -561,7 +561,7 @@ class QuickFixRegistrar : QuickFixContributor {
MISPLACED_TYPE_PARAMETER_CONSTRAINTS.registerFactory(MoveTypeParameterConstraintFixFactory)
COMMA_IN_WHEN_CONDITION_WITHOUT_ARGUMENT.registerFactory(CommaInWhenConditionWithoutArgumentFix)
COMMA_IN_WHEN_CONDITION_WITHOUT_ARGUMENT.registerFactory(CommaInWhenConditionWithoutArgumentFixFactory)
DATA_CLASS_NOT_PROPERTY_PARAMETER.registerFactory(AddValVarToConstructorParameterAction.DataClassConstructorNotPropertyQuickFixFactory)
MISSING_VAL_ON_ANNOTATION_PARAMETER.registerFactory(AddValVarToConstructorParameterAction.AnnotationClassConstructorNotValPropertyQuickFixFactory)
@@ -9,4 +9,5 @@ fun test(i: Int, j: Int) {
else -> { /* other code */ }
}
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
@@ -9,4 +9,5 @@ fun test(i: Int, j: Int) {
else -> { /* other code */ }
}
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
@@ -6,4 +6,5 @@ fun test(a: Boolean, b: Boolean, c: Boolean) {
else -> "e"
}
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
@@ -6,4 +6,5 @@ fun test(a: Boolean, b: Boolean, c: Boolean) {
else -> "e"
}
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
@@ -13,5 +13,5 @@ fun test(i: Int, j: Int) {
else -> { /* other code */ }
}
}
/* IGNORE_K2 */
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
@@ -13,5 +13,5 @@ fun test(i: Int, j: Int) {
else -> { /* other code */ }
}
}
/* IGNORE_K2 */
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
@@ -6,6 +6,6 @@ fun test(a: Boolean, b: Boolean, c: Boolean) {
else -> "e"
}
}
/* IGNORE_K2 */
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
@@ -6,6 +6,6 @@ fun test(a: Boolean, b: Boolean, c: Boolean) {
else -> "e"
}
}
/* IGNORE_K2 */
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.CommaInWhenConditionWithoutArgumentFix
@@ -140,6 +140,7 @@ internal fun MutableTWorkspace.generateK2FixTests() {
model("$idea/quickfix/makeTypeParameterReified", pattern = pattern)
model("$idea/quickfix/makeUpperBoundNonNullable", pattern = pattern, isIgnored = true)
model("$idea/quickfix/memberVisibilityCanBePrivate", pattern = pattern, isIgnored = true)
model("$idea/quickfix/migration/commasInWhenWithoutArgument", pattern = pattern)
model("$idea/quickfix/migration/missingConstructorKeyword", pattern = pattern)
model("$idea/quickfix/migration/removeNameFromFunctionExpression", pattern = pattern)
model("$idea/quickfix/migration/typeParameterList", pattern = pattern)