Add quickfix for @receiver applied to extension member instead of type
So #KT-14648 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
b206f6288d
commit
08bd212d82
@@ -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<KtAnnotationEntry>(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<KtCallableDeclaration>(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<KtCallableDeclaration>(true) ?: return null
|
||||||
|
if (declaration.receiverTypeReference == null) return null
|
||||||
|
|
||||||
|
return MoveReceiverAnnotationFix(entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun String.foo() {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -494,5 +494,7 @@ class QuickFixRegistrar : QuickFixContributor {
|
|||||||
MUST_BE_INITIALIZED_OR_BE_ABSTRACT.registerFactory(AddModifierFix.AddLateinitFactory)
|
MUST_BE_INITIALIZED_OR_BE_ABSTRACT.registerFactory(AddModifierFix.AddLateinitFactory)
|
||||||
|
|
||||||
RETURN_NOT_ALLOWED.registerFactory(ChangeToLabeledReturnFix)
|
RETURN_NOT_ALLOWED.registerFactory(ChangeToLabeledReturnFix)
|
||||||
|
|
||||||
|
INAPPLICABLE_RECEIVER_TARGET.registerFactory(MoveReceiverAnnotationFix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// "Move annotation to receiver type" "true"
|
||||||
|
|
||||||
|
annotation class Ann
|
||||||
|
|
||||||
|
@receiver:Ann<caret>
|
||||||
|
fun String.foo() {
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// "Move annotation to receiver type" "true"
|
||||||
|
|
||||||
|
annotation class Ann
|
||||||
|
|
||||||
|
<caret>fun @receiver:Ann String.foo() {
|
||||||
|
}
|
||||||
@@ -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<caret>
|
||||||
|
fun foo() {
|
||||||
|
}
|
||||||
@@ -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<caret>
|
||||||
|
val bar get() = ""
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// "Move annotation to receiver type" "true"
|
||||||
|
|
||||||
|
annotation class Ann
|
||||||
|
|
||||||
|
@receiver:Ann<caret>
|
||||||
|
val String.bar get() = ""
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Move annotation to receiver type" "true"
|
||||||
|
|
||||||
|
annotation class Ann
|
||||||
|
|
||||||
|
<caret>val @receiver:Ann String.bar get() = ""
|
||||||
@@ -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")
|
@TestMetadata("idea/testData/quickfix/moveToConstructorParameters")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user