diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/ConfusingBranchConditionErrorFixFactories.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/ConfusingBranchConditionErrorFixFactories.kt new file mode 100644 index 000000000000..969b5c3495e5 --- /dev/null +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/ConfusingBranchConditionErrorFixFactories.kt @@ -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(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) + } + } +} 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 5137850062a1..f20426dffd66 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 @@ -169,6 +169,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() { registerFactory(CallFromPublicInlineFixFactories.nonPublicCallFromPublicInlineFixFactory) registerFactory(CallFromPublicInlineFixFactories.protectedCallFromPublicInlineErrorFixFactory) registerFactory(CallFromPublicInlineFixFactories.superCallFromPublicInlineFixFactory) + registerFactory(ConfusingBranchConditionErrorFixFactories.wrapExpressionInParenthesesFixFactory) } private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix { diff --git a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java index fbb032ad60a6..f43925d03220 100644 --- a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java +++ b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java @@ -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") diff --git a/plugins/kotlin/idea/tests/testData/quickfix/wrapWhenExpressionInParentheses/logicalExpressionInWhen.kt b/plugins/kotlin/idea/tests/testData/quickfix/wrapWhenExpressionInParentheses/logicalExpressionInWhen.kt index 37b9294d494d..02af03fc1e61 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/wrapWhenExpressionInParentheses/logicalExpressionInWhen.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/wrapWhenExpressionInParentheses/logicalExpressionInWhen.kt @@ -9,4 +9,5 @@ fun test(x: A, b: Boolean) { x in x -> {} } } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConfusingExpressionInWhenBranchFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConfusingExpressionInWhenBranchFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ConfusingBranchConditionErrorFixFactories$WrapExpressionInParenthesesFixFactory \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/wrapWhenExpressionInParentheses/logicalExpressionInWhen.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/wrapWhenExpressionInParentheses/logicalExpressionInWhen.kt.after index 6684b3994ad6..0302edc44979 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/wrapWhenExpressionInParentheses/logicalExpressionInWhen.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/wrapWhenExpressionInParentheses/logicalExpressionInWhen.kt.after @@ -9,4 +9,5 @@ fun test(x: A, b: Boolean) { (x in x) -> {} } } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConfusingExpressionInWhenBranchFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConfusingExpressionInWhenBranchFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.ConfusingBranchConditionErrorFixFactories$WrapExpressionInParenthesesFixFactory \ No newline at end of file diff --git a/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2QuickFixTests.kt b/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2QuickFixTests.kt index 54fd28d6d974..2fcd91449929 100644 --- a/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2QuickFixTests.kt +++ b/plugins/kotlin/util/test-generator-fir/test/org/jetbrains/kotlin/fir/testGenerator/codeinsight/GenerateK2QuickFixTests.kt @@ -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) }