From 40f0a6d8335d42f0dd9da7dfc523f1d4d01c2e62 Mon Sep 17 00:00:00 2001 From: Andrey Cherkasov Date: Thu, 23 May 2024 00:09:13 +0400 Subject: [PATCH] [kotlin] Port TypeOfAnnotationMemberFix to K2 This quick fix is for the INVALID_TYPE_OF_ANNOTATION_MEMBER error. ^KTIJ-29542 GitOrigin-RevId: b908ccd2bfe2e54031bdfff75d817608575f8ead --- .../fixes/KotlinK2QuickFixRegistrar.kt | 1 + .../fixes/TypeOfAnnotationMemberFixFactory.kt | 24 +++++++++ .../fixes/HighLevelQuickFixTestGenerated.java | 51 +++++++++++++++++++ .../typeOfAnnotationMember/boolean.kt | 3 +- .../typeOfAnnotationMember/boolean.kt.after | 3 +- .../quickfix/typeOfAnnotationMember/byte.kt | 3 +- .../typeOfAnnotationMember/byte.kt.after | 3 +- .../quickfix/typeOfAnnotationMember/char.kt | 3 +- .../typeOfAnnotationMember/char.kt.after | 3 +- .../quickfix/typeOfAnnotationMember/float.kt | 3 +- .../typeOfAnnotationMember/float.kt.after | 3 +- .../quickfix/typeOfAnnotationMember/int.kt | 3 +- .../typeOfAnnotationMember/int.kt.after | 3 +- .../quickfix/typeOfAnnotationMember/long.kt | 3 +- .../typeOfAnnotationMember/long.kt.after | 3 +- .../quickfix/typeOfAnnotationMember/short.kt | 3 +- .../typeOfAnnotationMember/short.kt.after | 3 +- .../codeinsight/GenerateK2QuickFixTests.kt | 2 +- 18 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/TypeOfAnnotationMemberFixFactory.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 79b54bcf3ded..4ae3ca1780c2 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 @@ -94,6 +94,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() { registerFactory(AddStarProjectionsFixFactory.addStarProjectionsFixFactory) registerFactory(AddTypeAnnotationToValueParameterFixFactory.addTypeAnnotationToValueParameterFixFactory) registerFactory(ChangeToFunctionInvocationFixFactory.changeToFunctionInvocationFixFactory) + registerFactory(TypeOfAnnotationMemberFixFactory.typeOfAnnotationMemberFixFactory) } private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix { diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/TypeOfAnnotationMemberFixFactory.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/TypeOfAnnotationMemberFixFactory.kt new file mode 100644 index 000000000000..9e70c4e091b8 --- /dev/null +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/TypeOfAnnotationMemberFixFactory.kt @@ -0,0 +1,24 @@ +// 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.KtFirDiagnostic +import org.jetbrains.kotlin.analysis.api.types.KtNonErrorClassType +import org.jetbrains.kotlin.idea.codeinsight.api.applicators.fixes.KotlinQuickFixFactory +import org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix + +internal object TypeOfAnnotationMemberFixFactory { + + val typeOfAnnotationMemberFixFactory = KotlinQuickFixFactory.ModCommandBased { diagnostic: KtFirDiagnostic.InvalidTypeOfAnnotationMember -> + val typeReference = diagnostic.psi + + val arrayElementType = typeReference.getKtType().getArrayElementType() ?: return@ModCommandBased emptyList() + if (!arrayElementType.isPrimitive) return@ModCommandBased emptyList() + + val classId = (arrayElementType as KtNonErrorClassType).classId + val fixedArrayTypeText = "${classId.shortClassName}Array" + + listOf( + TypeOfAnnotationMemberFix(typeReference, fixedArrayTypeText) + ) + } +} 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 6656d343d4cb..e910304c8959 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 @@ -5957,7 +5957,58 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("../../../idea/tests/testData/quickfix/typeOfAnnotationMember") + public static class TypeOfAnnotationMember extends AbstractHighLevelQuickFixTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + @TestMetadata("boolean.kt") + public void testBoolean() throws Exception { + runTest("../../../idea/tests/testData/quickfix/typeOfAnnotationMember/boolean.kt"); + } + + @TestMetadata("byte.kt") + public void testByte() throws Exception { + runTest("../../../idea/tests/testData/quickfix/typeOfAnnotationMember/byte.kt"); + } + + @TestMetadata("char.kt") + public void testChar() throws Exception { + runTest("../../../idea/tests/testData/quickfix/typeOfAnnotationMember/char.kt"); + } + + @TestMetadata("float.kt") + public void testFloat() throws Exception { + runTest("../../../idea/tests/testData/quickfix/typeOfAnnotationMember/float.kt"); + } + + @TestMetadata("int.kt") + public void testInt() throws Exception { + runTest("../../../idea/tests/testData/quickfix/typeOfAnnotationMember/int.kt"); + } + + @TestMetadata("long.kt") + public void testLong() throws Exception { + runTest("../../../idea/tests/testData/quickfix/typeOfAnnotationMember/long.kt"); + } + + @TestMetadata("short.kt") + public void testShort() throws Exception { + runTest("../../../idea/tests/testData/quickfix/typeOfAnnotationMember/short.kt"); + } + + @TestMetadata("star.kt") + public void testStar() throws Exception { + runTest("../../../idea/tests/testData/quickfix/typeOfAnnotationMember/star.kt"); + } + + @TestMetadata("string.kt") + public void testString() throws Exception { + runTest("../../../idea/tests/testData/quickfix/typeOfAnnotationMember/string.kt"); + } + } diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/boolean.kt b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/boolean.kt index 325b6a138a35..b8526db200d7 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/boolean.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/boolean.kt @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val boo: Array, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/boolean.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/boolean.kt.after index 353afbe07930..8daf00930857 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/boolean.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/boolean.kt.after @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val boo: BooleanArray, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/byte.kt b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/byte.kt index 4b43a1c801e7..518af342df25 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/byte.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/byte.kt @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val b: Array, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/byte.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/byte.kt.after index 38c4e91f79e6..f87d8621f7bf 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/byte.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/byte.kt.after @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val b: ByteArray, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/char.kt b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/char.kt index c23c254324de..3cafa17bb993 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/char.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/char.kt @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val c: Array, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/char.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/char.kt.after index 47381c7aa584..53f7bd41f755 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/char.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/char.kt.after @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val c: CharArray, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/float.kt b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/float.kt index e4d70028e9ef..02225e5b12e0 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/float.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/float.kt @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val f: Array, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/float.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/float.kt.after index d909ae0debd5..ca2659a7f846 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/float.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/float.kt.after @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val f: FloatArray, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/int.kt b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/int.kt index 47d26976bb8a..0b51d13d992d 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/int.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/int.kt @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val i: Array, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/int.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/int.kt.after index f6c04b990e39..16da5c237426 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/int.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/int.kt.after @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val i: IntArray, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/long.kt b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/long.kt index 7ec29fa0c61d..81341ff52be8 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/long.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/long.kt @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val l: Array, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/long.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/long.kt.after index 924633813d3e..5f7dec278435 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/long.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/long.kt.after @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val l: LongArray, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/short.kt b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/short.kt index 181f37e59d2e..55b864769e7f 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/short.kt +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/short.kt @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val s: Array, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/short.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/short.kt.after index 7496daaa72f4..003fecf817e7 100644 --- a/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/short.kt.after +++ b/plugins/kotlin/idea/tests/testData/quickfix/typeOfAnnotationMember/short.kt.after @@ -3,4 +3,5 @@ annotation class SuperAnnotation( val s: ShortArray, val str: Array ) -// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ No newline at end of file +// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.TypeOfAnnotationMemberFix \ 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 694fa490d3d8..86f693f84e07 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/tooLongCharLiteralToString", pattern = pattern, isIgnored = true) model("$idea/quickfix/typeImports", pattern = pattern, isIgnored = true) model("$idea/quickfix/typeInferenceExpectedTypeMismatch", pattern = pattern, isIgnored = true) - model("$idea/quickfix/typeOfAnnotationMember", pattern = pattern, isIgnored = true) + model("$idea/quickfix/typeOfAnnotationMember", pattern = pattern) model("$idea/quickfix/typeParameters", pattern = pattern, isIgnored = true) model("$idea/quickfix/typeProjection", pattern = pattern, isIgnored = true) model("$idea/quickfix/unnecessaryLateinit", pattern = pattern, isIgnored = true)