From 08bd212d8289517df8dece08bcbf7392337094c5 Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Mon, 29 May 2017 21:17:14 +0300 Subject: [PATCH] Add quickfix for @receiver applied to extension member instead of type So #KT-14648 Fixed --- .../quickfix/MoveReceiverAnnotationFix.kt | 59 +++++++++++++++++++ .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 2 + .../quickfix/moveReceiverAnnotation/fun.kt | 7 +++ .../moveReceiverAnnotation/fun.kt.after | 6 ++ .../moveReceiverAnnotation/notExtensionFun.kt | 11 ++++ .../moveReceiverAnnotation/notExtensionVal.kt | 10 ++++ .../quickfix/moveReceiverAnnotation/val.kt | 6 ++ .../moveReceiverAnnotation/val.kt.after | 5 ++ .../idea/quickfix/QuickFixTestGenerated.java | 33 +++++++++++ 9 files changed, 139 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFix.kt create mode 100644 idea/testData/quickfix/moveReceiverAnnotation/fun.kt create mode 100644 idea/testData/quickfix/moveReceiverAnnotation/fun.kt.after create mode 100644 idea/testData/quickfix/moveReceiverAnnotation/notExtensionFun.kt create mode 100644 idea/testData/quickfix/moveReceiverAnnotation/notExtensionVal.kt create mode 100644 idea/testData/quickfix/moveReceiverAnnotation/val.kt create mode 100644 idea/testData/quickfix/moveReceiverAnnotation/val.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFix.kt new file mode 100644 index 00000000000..769a55a94be --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/MoveReceiverAnnotationFix.kt @@ -0,0 +1,59 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.quickfix + +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.diagnostics.Errors +import org.jetbrains.kotlin.psi.KtAnnotationEntry +import org.jetbrains.kotlin.psi.KtCallableDeclaration +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.psiUtil.getParentOfType + +class MoveReceiverAnnotationFix(element: KtAnnotationEntry) : KotlinQuickFixAction(element) { + + override fun getFamilyName() = "Move annotation to receiver type" + override fun getText() = familyName + + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + val element = element ?: return + + val declaration = element.getParentOfType(true) ?: return + val receiverTypeRef = declaration.receiverTypeReference ?: return + + receiverTypeRef.addAnnotationEntry(element) + element.delete() + } + + companion object Factory : KotlinSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): IntentionAction? { + val diag = Errors.INAPPLICABLE_RECEIVER_TARGET.cast(diagnostic) + val entry = diag.psiElement as? KtAnnotationEntry ?: return null + + val declaration = entry.getParentOfType(true) ?: return null + if (declaration.receiverTypeReference == null) return null + + return MoveReceiverAnnotationFix(entry) + } + } +} + +fun String.foo() { + +} \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 897bbb2d3fe..042faacd144 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -494,5 +494,7 @@ class QuickFixRegistrar : QuickFixContributor { MUST_BE_INITIALIZED_OR_BE_ABSTRACT.registerFactory(AddModifierFix.AddLateinitFactory) RETURN_NOT_ALLOWED.registerFactory(ChangeToLabeledReturnFix) + + INAPPLICABLE_RECEIVER_TARGET.registerFactory(MoveReceiverAnnotationFix) } } diff --git a/idea/testData/quickfix/moveReceiverAnnotation/fun.kt b/idea/testData/quickfix/moveReceiverAnnotation/fun.kt new file mode 100644 index 00000000000..1d1ebb71460 --- /dev/null +++ b/idea/testData/quickfix/moveReceiverAnnotation/fun.kt @@ -0,0 +1,7 @@ +// "Move annotation to receiver type" "true" + +annotation class Ann + +@receiver:Ann +fun String.foo() { +} \ No newline at end of file diff --git a/idea/testData/quickfix/moveReceiverAnnotation/fun.kt.after b/idea/testData/quickfix/moveReceiverAnnotation/fun.kt.after new file mode 100644 index 00000000000..eb29015e2a2 --- /dev/null +++ b/idea/testData/quickfix/moveReceiverAnnotation/fun.kt.after @@ -0,0 +1,6 @@ +// "Move annotation to receiver type" "true" + +annotation class Ann + +fun @receiver:Ann String.foo() { +} \ No newline at end of file diff --git a/idea/testData/quickfix/moveReceiverAnnotation/notExtensionFun.kt b/idea/testData/quickfix/moveReceiverAnnotation/notExtensionFun.kt new file mode 100644 index 00000000000..0fee8dbf7ad --- /dev/null +++ b/idea/testData/quickfix/moveReceiverAnnotation/notExtensionFun.kt @@ -0,0 +1,11 @@ +// "Move annotation to receiver type" "false" +// ERROR: '@receiver:' annotations could be applied only to extension function or extension property declarations +// ACTION: Convert to expression body +// ACTION: Make internal +// ACTION: Make private + +annotation class Ann + +@receiver:Ann +fun foo() { +} \ No newline at end of file diff --git a/idea/testData/quickfix/moveReceiverAnnotation/notExtensionVal.kt b/idea/testData/quickfix/moveReceiverAnnotation/notExtensionVal.kt new file mode 100644 index 00000000000..2143b19947b --- /dev/null +++ b/idea/testData/quickfix/moveReceiverAnnotation/notExtensionVal.kt @@ -0,0 +1,10 @@ +// "Move annotation to receiver type" "false" +// ERROR: '@receiver:' annotations could be applied only to extension function or extension property declarations +// ACTION: Make internal +// ACTION: Make private +// ACTION: Specify type explicitly + +annotation class Ann + +@receiver:Ann +val bar get() = "" \ No newline at end of file diff --git a/idea/testData/quickfix/moveReceiverAnnotation/val.kt b/idea/testData/quickfix/moveReceiverAnnotation/val.kt new file mode 100644 index 00000000000..be9c28c5ccd --- /dev/null +++ b/idea/testData/quickfix/moveReceiverAnnotation/val.kt @@ -0,0 +1,6 @@ +// "Move annotation to receiver type" "true" + +annotation class Ann + +@receiver:Ann +val String.bar get() = "" \ No newline at end of file diff --git a/idea/testData/quickfix/moveReceiverAnnotation/val.kt.after b/idea/testData/quickfix/moveReceiverAnnotation/val.kt.after new file mode 100644 index 00000000000..d8a76840450 --- /dev/null +++ b/idea/testData/quickfix/moveReceiverAnnotation/val.kt.after @@ -0,0 +1,5 @@ +// "Move annotation to receiver type" "true" + +annotation class Ann + +val @receiver:Ann String.bar get() = "" \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index bc3d86831e6..eca5ff02a53 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -7152,6 +7152,39 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/moveReceiverAnnotation") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class MoveReceiverAnnotation extends AbstractQuickFixTest { + public void testAllFilesPresentInMoveReceiverAnnotation() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/moveReceiverAnnotation"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("fun.kt") + public void testFun() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/moveReceiverAnnotation/fun.kt"); + doTest(fileName); + } + + @TestMetadata("notExtensionFun.kt") + public void testNotExtensionFun() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/moveReceiverAnnotation/notExtensionFun.kt"); + doTest(fileName); + } + + @TestMetadata("notExtensionVal.kt") + public void testNotExtensionVal() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/moveReceiverAnnotation/notExtensionVal.kt"); + doTest(fileName); + } + + @TestMetadata("val.kt") + public void testVal() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/moveReceiverAnnotation/val.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/quickfix/moveToConstructorParameters") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)