diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/DeclarationCantBeInlinedFixFactories.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/DeclarationCantBeInlinedFixFactories.kt new file mode 100644 index 000000000000..5a24600065f4 --- /dev/null +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/DeclarationCantBeInlinedFixFactories.kt @@ -0,0 +1,23 @@ +// 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.RemoveModifierFixBase +import org.jetbrains.kotlin.lexer.KtTokens +import org.jetbrains.kotlin.psi.KtNamedFunction +import org.jetbrains.kotlin.psi.psiUtil.containingClass + +internal object DeclarationCantBeInlinedFixFactories { + + val removeOpenModifierFixFactory = KotlinQuickFixFactory.IntentionBased { diagnostic: KaFirDiagnostic.DeclarationCantBeInlined -> + val function = diagnostic.psi as? KtNamedFunction ?: return@IntentionBased emptyList() + val containingClass = function.containingClass() ?: return@IntentionBased emptyList() + if (containingClass.isInterface()) return@IntentionBased emptyList() + if (!function.hasModifier(KtTokens.OPEN_KEYWORD)) return@IntentionBased emptyList() + + listOf( + RemoveModifierFixBase(function, KtTokens.OPEN_KEYWORD, false) + ) + } +} 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 aeec81b168ab..ea5662f7d80f 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 @@ -172,6 +172,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() { registerFactory(ConfusingBranchConditionErrorFixFactories.wrapExpressionInParenthesesFixFactory) registerFactory(WrapWithParenthesesFixFactories.suspendFixFactory) registerFactory(WrapWithParenthesesFixFactories.suspendFunErrorFixFactory) + registerFactory(DeclarationCantBeInlinedFixFactories.removeOpenModifierFixFactory) } 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 96a04150a4f7..36282b30e737 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 @@ -7324,7 +7324,29 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu } } + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("../../../idea/tests/testData/quickfix/declarationCantBeInlined") + public static class DeclarationCantBeInlined 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("inClass.kt") + public void testInClass() throws Exception { + runTest("../../../idea/tests/testData/quickfix/declarationCantBeInlined/inClass.kt"); + } + + @TestMetadata("inInterface.kt") + public void testInInterface() throws Exception { + runTest("../../../idea/tests/testData/quickfix/declarationCantBeInlined/inInterface.kt"); + } + } diff --git a/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inClass.kt b/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inClass.kt index 527e41b0c96d..8bfd9ffc044d 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inClass.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inClass.kt @@ -2,4 +2,5 @@ open class A { inline open fun foo() {} } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveModifierFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveModifierFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveModifierFixBase \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inClass.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inClass.kt.after index 40d9a805677a..efdaec8dbe22 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inClass.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inClass.kt.after @@ -2,4 +2,5 @@ open class A { inline fun foo() {} } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveModifierFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveModifierFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveModifierFixBase \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inInterface.kt b/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inInterface.kt index a4ff5343ed3b..1a36bbb669d3 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inInterface.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inInterface.kt @@ -3,4 +3,6 @@ interface B { inline fun foo() } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertMemberToExtensionFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertMemberToExtensionFix +// TODO: KTIJ-29625 +/* IGNORE_K2 */ \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inInterface.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inInterface.kt.after index 2acc2694c1ac..07d261ab071b 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inInterface.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/declarationCantBeInlined/inInterface.kt.after @@ -6,4 +6,6 @@ interface B { inline fun B.foo() { TODO("Not yet implemented") } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertMemberToExtensionFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ConvertMemberToExtensionFix +// TODO: KTIJ-29625 +/* IGNORE_K2 */ \ 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 e53ac2e6467c..c29c530be245 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 @@ -112,7 +112,7 @@ internal fun MutableTWorkspace.generateK2FixTests() { model("$idea/quickfix/convertToIsArrayOfCall", pattern = pattern) model("$idea/quickfix/createFromUsage", pattern = pattern, isIgnored = true) model("$idea/quickfix/createLabel", pattern = pattern) - model("$idea/quickfix/declarationCantBeInlined", pattern = pattern, isIgnored = true) + model("$idea/quickfix/declarationCantBeInlined", pattern = pattern) model("$idea/quickfix/declaringJavaClass", pattern = pattern, isIgnored = true) model("$idea/quickfix/decreaseVisibility", pattern = pattern) model("$idea/quickfix/deprecatedJavaAnnotation", pattern = pattern, isIgnored = true)