From b9ddf4e737fffb866c9a97dad7b13306f1b6a573 Mon Sep 17 00:00:00 2001 From: Andrey Cherkasov Date: Thu, 25 Jul 2024 03:13:07 +0400 Subject: [PATCH] [kotlin] Port MoveReceiverAnnotationFix to K2 ^KTIJ-29515 GitOrigin-RevId: 1ee77db7431ba0bed9ed70faa5b850aac22968f9 --- .../fixes/KotlinK2QuickFixRegistrar.kt | 1 + ...tationTargetWithUseSiteTargetFixFactory.kt | 15 +++++++++ .../fixes/HighLevelQuickFixTestGenerated.java | 32 +++++++++++++++++++ .../quickfix/MoveReceiverAnnotationFix.kt | 13 +++++++- .../MoveReceiverAnnotationFixFactory.kt | 11 +------ .../quickfix/moveReceiverAnnotation/fun.kt | 3 +- .../moveReceiverAnnotation/fun.kt.after | 3 +- .../quickfix/moveReceiverAnnotation/val.kt | 3 +- .../moveReceiverAnnotation/val.kt.after | 3 +- .../codeinsight/GenerateK2QuickFixTests.kt | 4 +-- 10 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/WrongAnnotationTargetWithUseSiteTargetFixFactory.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 a041b891eae0..0b82d40131fc 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 @@ -130,6 +130,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() { registerFactory(MoveWhenElseBranchFixFactory.moveWhenElseBranchFixFactory) registerFactory(TypeVarianceConflictErrorFixFactory.removeTypeVariance) registerFactory(FinalUpperBoundFixFactory.removeFinalUpperBound) + registerFactory(WrongAnnotationTargetWithUseSiteTargetFixFactory.moveReceiverAnnotation) } private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix { diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/WrongAnnotationTargetWithUseSiteTargetFixFactory.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/WrongAnnotationTargetWithUseSiteTargetFixFactory.kt new file mode 100644 index 000000000000..3b8c80a678ba --- /dev/null +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/WrongAnnotationTargetWithUseSiteTargetFixFactory.kt @@ -0,0 +1,15 @@ +// 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.MoveReceiverAnnotationFix + +internal object WrongAnnotationTargetWithUseSiteTargetFixFactory { + + val moveReceiverAnnotation = KotlinQuickFixFactory.ModCommandBased { diagnostic: KaFirDiagnostic.WrongAnnotationTargetWithUseSiteTarget -> + listOfNotNull( + MoveReceiverAnnotationFix.createIfApplicable(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 2ca3c7d73b46..7819e29fc49c 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 @@ -9100,7 +9100,39 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("../../../idea/tests/testData/quickfix/moveReceiverAnnotation") + public static class MoveReceiverAnnotation 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("fun.kt") + public void testFun() throws Exception { + runTest("../../../idea/tests/testData/quickfix/moveReceiverAnnotation/fun.kt"); + } + + @TestMetadata("notExtensionFun.kt") + public void testNotExtensionFun() throws Exception { + runTest("../../../idea/tests/testData/quickfix/moveReceiverAnnotation/notExtensionFun.kt"); + } + + @TestMetadata("notExtensionVal.kt") + public void testNotExtensionVal() throws Exception { + runTest("../../../idea/tests/testData/quickfix/moveReceiverAnnotation/notExtensionVal.kt"); + } + + @TestMetadata("val.kt") + public void testVal() throws Exception { + runTest("../../../idea/tests/testData/quickfix/moveReceiverAnnotation/val.kt"); + } + } diff --git a/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFix.kt b/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFix.kt index 03a233b4d8f1..c4daa37da582 100644 --- a/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFix.kt +++ b/plugins/kotlin/frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFix.kt @@ -3,13 +3,14 @@ package org.jetbrains.kotlin.idea.quickfix import com.intellij.modcommand.ActionContext import com.intellij.modcommand.ModPsiUpdater +import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget import org.jetbrains.kotlin.idea.base.resources.KotlinBundle import org.jetbrains.kotlin.idea.codeinsight.api.applicable.intentions.KotlinPsiUpdateModCommandAction import org.jetbrains.kotlin.psi.KtAnnotationEntry import org.jetbrains.kotlin.psi.KtCallableDeclaration import org.jetbrains.kotlin.psi.psiUtil.getParentOfType -class MoveReceiverAnnotationFix( +class MoveReceiverAnnotationFix private constructor( element: KtAnnotationEntry, ) : KotlinPsiUpdateModCommandAction.ElementBased(element, Unit) { @@ -27,4 +28,14 @@ class MoveReceiverAnnotationFix( receiverTypeRef.addAnnotationEntry(element) element.delete() } + + companion object { + fun createIfApplicable(element: KtAnnotationEntry): MoveReceiverAnnotationFix? { + if (element.useSiteTarget?.getAnnotationUseSiteTarget() != AnnotationUseSiteTarget.RECEIVER) return null + val declaration = element.getParentOfType(true) ?: return null + if (declaration.receiverTypeReference == null) return null + + return MoveReceiverAnnotationFix(element) + } + } } diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFixFactory.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFixFactory.kt index a7e988651a8c..9ec7d6f0efbb 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFixFactory.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFixFactory.kt @@ -3,21 +3,12 @@ package org.jetbrains.kotlin.idea.quickfix import com.intellij.codeInsight.intention.IntentionAction -import org.jetbrains.kotlin.descriptors.annotations.AnnotationUseSiteTarget import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.psi.KtAnnotationEntry -import org.jetbrains.kotlin.psi.KtCallableDeclaration -import org.jetbrains.kotlin.psi.psiUtil.getParentOfType internal object MoveReceiverAnnotationFixFactory : KotlinSingleIntentionActionFactory() { override fun createAction(diagnostic: Diagnostic): IntentionAction? { val entry = diagnostic.psiElement as? KtAnnotationEntry ?: return null - - if (entry.useSiteTarget?.getAnnotationUseSiteTarget() != AnnotationUseSiteTarget.RECEIVER) return null - - val declaration = entry.getParentOfType(true) ?: return null - if (declaration.receiverTypeReference == null) return null - - return MoveReceiverAnnotationFix(entry).asIntention() + return MoveReceiverAnnotationFix.createIfApplicable(entry)?.asIntention() } } \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/fun.kt b/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/fun.kt index 11ceaaadc984..b487feafc6e9 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/fun.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/fun.kt @@ -5,4 +5,5 @@ annotation class Ann @receiver:Ann fun String.foo() { } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/fun.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/fun.kt.after index 3257c4ef1a80..b792fffe8918 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/fun.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/fun.kt.after @@ -4,4 +4,5 @@ annotation class Ann fun @receiver:Ann String.foo() { } -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/val.kt b/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/val.kt index f0cc91a4d1d9..397586b99f1c 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/val.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/val.kt @@ -4,4 +4,5 @@ annotation class Ann @receiver:Ann val String.bar get() = "" -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/val.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/val.kt.after index dece265cd4bb..65ab3a1679a1 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/val.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/moveReceiverAnnotation/val.kt.after @@ -3,4 +3,5 @@ annotation class Ann val @receiver:Ann String.bar get() = "" -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix \ 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 9651944b876d..8e28142881bf 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 @@ -5,7 +5,7 @@ import org.jetbrains.kotlin.idea.k2.codeinsight.fixes.AbstractHighLevelQuickFixM import org.jetbrains.kotlin.idea.k2.codeinsight.fixes.AbstractHighLevelQuickFixMultiModuleTest import org.jetbrains.kotlin.idea.k2.codeinsight.fixes.AbstractHighLevelQuickFixTest import org.jetbrains.kotlin.testGenerator.model.* -import org.jetbrains.kotlin.testGenerator.model.GroupCategory.* +import org.jetbrains.kotlin.testGenerator.model.GroupCategory.QUICKFIXES import org.jetbrains.kotlin.testGenerator.model.Patterns.DIRECTORY import org.jetbrains.kotlin.testGenerator.model.Patterns.KT_WITHOUT_DOTS @@ -140,7 +140,7 @@ internal fun MutableTWorkspace.generateK2FixTests() { model("$idea/quickfix/migration/missingConstructorKeyword", pattern = pattern) model("$idea/quickfix/missingConstructorBrackets", pattern = pattern) model("$idea/quickfix/moveMemberToCompanionObject", pattern = pattern, isIgnored = true) - model("$idea/quickfix/moveReceiverAnnotation", pattern = pattern, isIgnored = true) + model("$idea/quickfix/moveReceiverAnnotation", pattern = pattern) model("$idea/quickfix/moveToConstructorParameters", pattern = pattern, isIgnored = true) model("$idea/quickfix/moveToSealedParent", pattern = pattern, isIgnored = true) model("$idea/quickfix/moveTypeAliasToTopLevel", pattern = pattern)