From 7b4e57696d330b19b174e8265bdee8d2e007bd21 Mon Sep 17 00:00:00 2001 From: Andrey Cherkasov Date: Sat, 27 Jul 2024 20:59:47 +0400 Subject: [PATCH] [kotlin] Port MoveTypeParameterConstraintFix to K2 This quick fix is for the MISPLACED_TYPE_PARAMETER_CONSTRAINTS compiler warning. ^KTIJ-29517 GitOrigin-RevId: 6737678923ff3a19f835006e71589b41aff24007 --- .../fixes/KotlinK2QuickFixRegistrar.kt | 1 + .../MoveTypeParameterConstraintFixFactory.kt | 13 +++++++++++ .../fixes/HighLevelQuickFixTestGenerated.java | 22 +++++++++++++++++++ .../misplacedClassTypeParameter.kt | 3 ++- .../misplacedClassTypeParameter.kt.after | 3 ++- .../misplacedFunctionTypeParameter.kt | 3 ++- .../misplacedFunctionTypeParameter.kt.after | 3 ++- .../codeinsight/GenerateK2QuickFixTests.kt | 2 +- 8 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/MoveTypeParameterConstraintFixFactory.kt 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 b4946469395e..12e13a22cb06 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 @@ -135,6 +135,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() { registerFactory(IsEnumEntryFixFactory.factory) registerFactory(NoReturnInFunctionWithBlockBodyFixFactory.addReturnToLastExpression) registerFactory(InlineClassDeprecatedFixFactory.replaceWithValue) + registerFactory(MisplacedTypeParameterConstraintsFixFactory.moveConstraintToWhereClause) } private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix { diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/MoveTypeParameterConstraintFixFactory.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/MoveTypeParameterConstraintFixFactory.kt new file mode 100644 index 000000000000..4f0188a62eec --- /dev/null +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/MoveTypeParameterConstraintFixFactory.kt @@ -0,0 +1,13 @@ +// 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.MoveTypeParameterConstraintFix + +internal object MisplacedTypeParameterConstraintsFixFactory { + + val moveConstraintToWhereClause = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.MisplacedTypeParameterConstraints -> + listOf(MoveTypeParameterConstraintFix(diagnostic.psi)) + } +} \ No newline at end of file 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 6ef0d0773478..309c4d85653d 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 @@ -10213,7 +10213,29 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu } } + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("../../../idea/tests/testData/quickfix/typeParameters") + public static class TypeParameters 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("misplacedClassTypeParameter.kt") + public void testMisplacedClassTypeParameter() throws Exception { + runTest("../../../idea/tests/testData/quickfix/typeParameters/misplacedClassTypeParameter.kt"); + } + + @TestMetadata("misplacedFunctionTypeParameter.kt") + public void testMisplacedFunctionTypeParameter() throws Exception { + runTest("../../../idea/tests/testData/quickfix/typeParameters/misplacedFunctionTypeParameter.kt"); + } + } diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedClassTypeParameter.kt b/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedClassTypeParameter.kt index e8d22f192b77..03b0337da4f4 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedClassTypeParameter.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedClassTypeParameter.kt @@ -2,4 +2,5 @@ class A<T : Cloneable> where T : Comparable<*> { } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeParameterConstraintFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeParameterConstraintFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeParameterConstraintFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedClassTypeParameter.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedClassTypeParameter.kt.after index c1ca8484d321..6ce8591be781 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedClassTypeParameter.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedClassTypeParameter.kt.after @@ -2,4 +2,5 @@ class A where T : Cloneable, T : Comparable<*> { } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeParameterConstraintFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeParameterConstraintFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeParameterConstraintFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedFunctionTypeParameter.kt b/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedFunctionTypeParameter.kt index 003fe2b4c13a..9a8128d2382d 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedFunctionTypeParameter.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedFunctionTypeParameter.kt @@ -2,4 +2,5 @@ fun <T : Cloneable> foo() where T : Comparable<*> { } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeParameterConstraintFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeParameterConstraintFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeParameterConstraintFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedFunctionTypeParameter.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedFunctionTypeParameter.kt.after index ee8ea3f3ee88..7cb6c64937a5 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedFunctionTypeParameter.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeParameters/misplacedFunctionTypeParameter.kt.after @@ -2,4 +2,5 @@ fun foo() where T : Cloneable, T : Comparable<*> { } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeParameterConstraintFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeParameterConstraintFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveTypeParameterConstraintFix \ 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 5e90e57828bd..3a3c59fe3b72 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 @@ -193,7 +193,7 @@ internal fun MutableTWorkspace.generateK2FixTests() { model("$idea/quickfix/typeImports", pattern = pattern) model("$idea/quickfix/typeInferenceExpectedTypeMismatch", pattern = pattern, isIgnored = true) model("$idea/quickfix/typeOfAnnotationMember", pattern = pattern) - model("$idea/quickfix/typeParameters", pattern = pattern, isIgnored = true) + model("$idea/quickfix/typeParameters", pattern = pattern) model("$idea/quickfix/typeProjection", pattern = pattern, isIgnored = true) model("$idea/quickfix/unnecessaryLateinit", pattern = pattern, isIgnored = true) model("$idea/quickfix/unusedSuppressAnnotation", pattern = pattern, isIgnored = true)