mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[kotlin] Port ConfusingExpressionInWhenBranchFix to K2
^KTIJ-29486 GitOrigin-RevId: e9643fd3909d395cfc4850bfeb5961854d102e0f
This commit is contained in:
committed by
intellij-monorepo-bot
parent
24587f775a
commit
fe5183a84e
+36
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -169,6 +169,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() {
|
||||
registerFactory(CallFromPublicInlineFixFactories.nonPublicCallFromPublicInlineFixFactory)
|
||||
registerFactory(CallFromPublicInlineFixFactories.protectedCallFromPublicInlineErrorFixFactory)
|
||||
registerFactory(CallFromPublicInlineFixFactories.superCallFromPublicInlineFixFactory)
|
||||
registerFactory(ConfusingBranchConditionErrorFixFactories.wrapExpressionInParenthesesFixFactory)
|
||||
}
|
||||
|
||||
private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix {
|
||||
|
||||
+17
@@ -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")
|
||||
|
||||
+2
-1
@@ -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
|
||||
+2
-1
@@ -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
|
||||
+1
-1
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user