[kotlin] Port ConfusingExpressionInWhenBranchFix to K2

^KTIJ-29486

GitOrigin-RevId: e9643fd3909d395cfc4850bfeb5961854d102e0f
This commit is contained in:
Andrey Cherkasov
2024-09-07 23:54:36 +00:00
committed by intellij-monorepo-bot
parent 24587f775a
commit fe5183a84e
6 changed files with 59 additions and 3 deletions
@@ -0,0 +1,36 @@
// 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 com.intellij.modcommand.ActionContext
import com.intellij.modcommand.ModPsiUpdater
import com.intellij.modcommand.PsiUpdateModCommandAction
import org.jetbrains.kotlin.analysis.api.fir.diagnostics.KaFirDiagnostic
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.codeinsight.api.applicators.fixes.KotlinQuickFixFactory
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
internal object ConfusingBranchConditionErrorFixFactories {
val wrapExpressionInParenthesesFixFactory = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.ConfusingBranchConditionError ->
val element = diagnostic.psi as? KtExpression ?: return@ModCommandBased emptyList()
listOf(WrapExpressionInParenthesesFixFactory(element))
}
private class WrapExpressionInParenthesesFixFactory(
element: KtExpression,
) : PsiUpdateModCommandAction<KtExpression>(element) {
override fun getFamilyName(): String = KotlinBundle.message("wrap.expression.in.parentheses")
override fun invoke(
context: ActionContext,
element: KtExpression,
updater: ModPsiUpdater,
) {
val wrapped = KtPsiFactory(context.project).createExpressionByPattern("($0)", element)
element.replace(wrapped)
}
}
}
@@ -169,6 +169,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() {
registerFactory(CallFromPublicInlineFixFactories.nonPublicCallFromPublicInlineFixFactory)
registerFactory(CallFromPublicInlineFixFactories.protectedCallFromPublicInlineErrorFixFactory)
registerFactory(CallFromPublicInlineFixFactories.superCallFromPublicInlineFixFactory)
registerFactory(ConfusingBranchConditionErrorFixFactories.wrapExpressionInParenthesesFixFactory)
}
private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix {
@@ -11227,7 +11227,24 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/quickfix/wrapWhenExpressionInParentheses")
public static class WrapWhenExpressionInParentheses 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("logicalExpressionInWhen.kt")
public void testLogicalExpressionInWhen() throws Exception {
runTest("../../../idea/tests/testData/quickfix/wrapWhenExpressionInParentheses/logicalExpressionInWhen.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/quickfix/wrongLongSuffix")
@@ -9,4 +9,5 @@ fun test(x: A, b: Boolean) {
x in x<caret> -> {}
}
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConfusingExpressionInWhenBranchFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConfusingExpressionInWhenBranchFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ConfusingBranchConditionErrorFixFactories$WrapExpressionInParenthesesFixFactory
@@ -9,4 +9,5 @@ fun test(x: A, b: Boolean) {
(x in x) -> {}
}
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConfusingExpressionInWhenBranchFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConfusingExpressionInWhenBranchFix
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ConfusingBranchConditionErrorFixFactories$WrapExpressionInParenthesesFixFactory
@@ -204,7 +204,7 @@ internal fun MutableTWorkspace.generateK2FixTests() {
model("$idea/quickfix/variables/changeToPropertyAccess", pattern = pattern, isRecursive = false, isIgnored = true)
model("$idea/quickfix/variables/changeToFunctionInvocation", pattern = pattern, isRecursive = false)
model("$idea/quickfix/wrapArgumentWithParentheses", pattern = pattern, isIgnored = true)
model("$idea/quickfix/wrapWhenExpressionInParentheses", pattern = pattern, isIgnored = true)
model("$idea/quickfix/wrapWhenExpressionInParentheses", pattern = pattern)
model("$idea/quickfix/wrongLongSuffix", pattern = pattern)
model("$idea/quickfix/yieldUnsupported", pattern = pattern, isIgnored = true)
}