From 8c7b187214022cf829fc0e1ecd317e5405ddd165 Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Sat, 13 Dec 2025 01:54:26 +0100 Subject: [PATCH] [kotlin] KTIJ-36850 Split `KotlinIntentionPolicy` into K1 and K2 GitOrigin-RevId: af770fa567d472dac654134ce11f859c778ce11c --- .../FirKotlinCodeInsightSanityTest.kt | 3 +++ .../fir/propertyBased/K2IntentionPolicy.kt | 23 +++++++++++++++++++ .../propertyBased/K1CodeInsightSanityTest.kt | 6 +++++ .../idea/propertyBased/K1IntentionPolicy.kt | 20 ++++++++++++++++ .../KotlinCodeInsightSanityTest.kt | 6 +++-- .../propertyBased/KotlinIntentionPolicy.kt | 20 +--------------- 6 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/propertyBased/K2IntentionPolicy.kt create mode 100644 plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/K1CodeInsightSanityTest.kt create mode 100644 plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/K1IntentionPolicy.kt diff --git a/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/propertyBased/FirKotlinCodeInsightSanityTest.kt b/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/propertyBased/FirKotlinCodeInsightSanityTest.kt index 9f01324398ca..07c2b0984bb5 100644 --- a/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/propertyBased/FirKotlinCodeInsightSanityTest.kt +++ b/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/propertyBased/FirKotlinCodeInsightSanityTest.kt @@ -3,6 +3,7 @@ package org.jetbrains.kotlin.idea.fir.propertyBased import org.jetbrains.kotlin.idea.base.plugin.KotlinPluginMode import org.jetbrains.kotlin.idea.propertyBased.KotlinCodeInsightSanityTest +import org.jetbrains.kotlin.idea.propertyBased.KotlinIntentionPolicy import org.jetbrains.kotlin.idea.test.setUpWithKotlinPlugin class FirKotlinCodeInsightSanityTest: KotlinCodeInsightSanityTest() { @@ -10,4 +11,6 @@ class FirKotlinCodeInsightSanityTest: KotlinCodeInsightSanityTest() { override fun setUp() { setUpWithKotlinPlugin { super.setUp() } } + + override fun createIntentionPolicy(): KotlinIntentionPolicy = K2IntentionPolicy() } diff --git a/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/propertyBased/K2IntentionPolicy.kt b/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/propertyBased/K2IntentionPolicy.kt new file mode 100644 index 000000000000..cd83d0386f00 --- /dev/null +++ b/plugins/kotlin/fir/tests/test/org/jetbrains/kotlin/idea/fir/propertyBased/K2IntentionPolicy.kt @@ -0,0 +1,23 @@ +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.kotlin.idea.fir.propertyBased + +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.codeInsight.intention.IntentionActionDelegate +import org.jetbrains.kotlin.idea.propertyBased.KotlinIntentionPolicy + +internal class K2IntentionPolicy : KotlinIntentionPolicy() { + override fun shouldCheckPreview(action: IntentionAction): Boolean { + val unwrapped = IntentionActionDelegate.unwrap(action) + val skipPreview = + action.familyName == "Create from usage" || // Starts template but may also perform modifications before that; thus not so easy to support + unwrapped.javaClass.name in skipPreviewIntentionClassNames + return !skipPreview + } + + private val skipPreviewIntentionClassNames = + setOf( + "org.jetbrains.kotlin.idea.k2.codeinsight.intentions.ChangePackageIntention", // Just starts the template; no reasonable preview could be displayed + "org.jetbrains.kotlin.idea.k2.codeinsight.fixes.imprt.ImportQuickFix", + "org.jetbrains.kotlin.idea.quickfix.K2EnableUnsupportedFeatureFix" + ) +} diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/K1CodeInsightSanityTest.kt b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/K1CodeInsightSanityTest.kt new file mode 100644 index 000000000000..183bb1110b14 --- /dev/null +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/K1CodeInsightSanityTest.kt @@ -0,0 +1,6 @@ +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.kotlin.idea.propertyBased + +class K1CodeInsightSanityTest : KotlinCodeInsightSanityTest() { + override fun createIntentionPolicy(): KotlinIntentionPolicy = K1IntentionPolicy() +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/K1IntentionPolicy.kt b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/K1IntentionPolicy.kt new file mode 100644 index 000000000000..f6f1598eb7d0 --- /dev/null +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/K1IntentionPolicy.kt @@ -0,0 +1,20 @@ +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.kotlin.idea.propertyBased + +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.codeInsight.intention.IntentionActionDelegate +import org.jetbrains.kotlin.idea.codeInsight.intentions.shared.ConvertToScopeIntention +import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createCallable.CreateCallableFromUsageFixBase +import org.jetbrains.kotlin.idea.refactoring.move.changePackage.ChangePackageIntention + +class K1IntentionPolicy : KotlinIntentionPolicy() { + override fun shouldCheckPreview(action: IntentionAction): Boolean { + val unwrapped = IntentionActionDelegate.unwrap(action) + val skipPreview = + action.familyName == "Create from usage" || // Starts template but may also perform modifications before that; thus not so easy to support + unwrapped is ConvertToScopeIntention || // Performs reference search which must be run under progress. Probably we can generate diff excluding references?.. + unwrapped is CreateCallableFromUsageFixBase<*> || // Performs too much of complex stuff. Not sure whether it should start in write action... + unwrapped is ChangePackageIntention // Just starts the template; no reasonable preview could be displayed + return !skipPreview + } +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinCodeInsightSanityTest.kt b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinCodeInsightSanityTest.kt index d9a05df940dd..69bb42c0fa46 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinCodeInsightSanityTest.kt +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinCodeInsightSanityTest.kt @@ -19,7 +19,7 @@ import java.util.function.Function import java.util.function.Supplier @SkipSlowTestLocally -open class KotlinCodeInsightSanityTest : KotlinLightCodeInsightFixtureTestCase() { +abstract class KotlinCodeInsightSanityTest : KotlinLightCodeInsightFixtureTestCase() { private val seed: String? = System.getProperty("seed") override fun setUp() { @@ -52,7 +52,7 @@ open class KotlinCodeInsightSanityTest : KotlinLightCodeInsightFixtureTestCase() AbstractImportFixInfo.ignoreModuleError(testRootDisposable) val actionSupplier = actionOnKotlinFiles { file: PsiFile -> Generator.sampledFrom( - InvokeIntention(file, KotlinIntentionPolicy()), + InvokeIntention(file, createIntentionPolicy()), //TODO: support completion mutators //InvokeCompletion(file, KotlinCompletionPolicy()), StripTestDataMarkup(file), @@ -67,6 +67,8 @@ open class KotlinCodeInsightSanityTest : KotlinLightCodeInsightFixtureTestCase() .checkScenarios(actionSupplier) } + protected abstract fun createIntentionPolicy(): KotlinIntentionPolicy + private fun enableInspections() { MadTestingUtil.enableAllInspections(project, KotlinLanguage.INSTANCE) } diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinIntentionPolicy.kt b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinIntentionPolicy.kt index 5be74ca658e9..b30efb4ae8c4 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinIntentionPolicy.kt +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/propertyBased/KotlinIntentionPolicy.kt @@ -11,7 +11,7 @@ import org.jetbrains.kotlin.idea.codeInsight.intentions.shared.ConvertToScopeInt import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createCallable.CreateCallableFromUsageFixBase import org.jetbrains.kotlin.idea.refactoring.move.changePackage.ChangePackageIntention -internal class KotlinIntentionPolicy : IntentionPolicy() { +abstract class KotlinIntentionPolicy : IntentionPolicy() { override fun shouldSkipIntention(actionText: String): Boolean = // These intentions don't modify code (probably should not start in write-action?) actionText == "Enable a trailing comma by default in the formatter" || @@ -23,22 +23,4 @@ internal class KotlinIntentionPolicy : IntentionPolicy() { override fun mayBreakCode(action: IntentionAction, editor: Editor, file: PsiFile): Boolean = false override fun shouldTolerateIntroducedError(info: HighlightInfo): Boolean = false - - override fun shouldCheckPreview(action: IntentionAction): Boolean { - val unwrapped = IntentionActionDelegate.unwrap(action) - val skipPreview = - action.familyName == "Create from usage" || // Starts template but may also perform modifications before that; thus not so easy to support - unwrapped is ConvertToScopeIntention || // Performs reference search which must be run under progress. Probably we can generate diff excluding references?.. - unwrapped is CreateCallableFromUsageFixBase<*> || // Performs too much of complex stuff. Not sure whether it should start in write action... - unwrapped is ChangePackageIntention || // Just starts the template; no reasonable preview could be displayed - unwrapped.javaClass.name in skipPreviewIntentionClassNames - return !skipPreview - } - - private val skipPreviewIntentionClassNames = - setOf( - "org.jetbrains.kotlin.idea.k2.codeinsight.intentions.ChangePackageIntention", - "org.jetbrains.kotlin.idea.k2.codeinsight.fixes.imprt.ImportQuickFix", - "org.jetbrains.kotlin.idea.quickfix.K2EnableUnsupportedFeatureFix" - ) }