mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[kotlin] Port MoveReceiverAnnotationFix to K2
^KTIJ-29515 GitOrigin-RevId: 1ee77db7431ba0bed9ed70faa5b850aac22968f9
This commit is contained in:
committed by
intellij-monorepo-bot
parent
96753eba04
commit
b9ddf4e737
+1
@@ -130,6 +130,7 @@ class KotlinK2QuickFixRegistrar : KotlinQuickFixRegistrar() {
|
||||
registerFactory(MoveWhenElseBranchFixFactory.moveWhenElseBranchFixFactory)
|
||||
registerFactory(TypeVarianceConflictErrorFixFactory.removeTypeVariance)
|
||||
registerFactory(FinalUpperBoundFixFactory.removeFinalUpperBound)
|
||||
registerFactory(WrongAnnotationTargetWithUseSiteTargetFixFactory.moveReceiverAnnotation)
|
||||
}
|
||||
|
||||
private val addAbstract = KtQuickFixesListBuilder.registerPsiQuickFix {
|
||||
|
||||
+15
@@ -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)
|
||||
)
|
||||
}
|
||||
}
|
||||
+32
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
+12
-1
@@ -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<KtAnnotationEntry, Unit>(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<KtCallableDeclaration>(true) ?: return null
|
||||
if (declaration.receiverTypeReference == null) return null
|
||||
|
||||
return MoveReceiverAnnotationFix(element)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-10
@@ -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<KtCallableDeclaration>(true) ?: return null
|
||||
if (declaration.receiverTypeReference == null) return null
|
||||
|
||||
return MoveReceiverAnnotationFix(entry).asIntention()
|
||||
return MoveReceiverAnnotationFix.createIfApplicable(entry)?.asIntention()
|
||||
}
|
||||
}
|
||||
@@ -5,4 +5,5 @@ annotation class Ann
|
||||
@receiver:Ann<caret>
|
||||
fun String.foo() {
|
||||
}
|
||||
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix
|
||||
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix
|
||||
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix
|
||||
@@ -4,4 +4,5 @@ annotation class Ann
|
||||
|
||||
<caret>fun @receiver:Ann String.foo() {
|
||||
}
|
||||
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix
|
||||
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix
|
||||
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix
|
||||
@@ -4,4 +4,5 @@ annotation class Ann
|
||||
|
||||
@receiver:Ann<caret>
|
||||
val String.bar get() = ""
|
||||
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix
|
||||
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix
|
||||
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix
|
||||
@@ -3,4 +3,5 @@
|
||||
annotation class Ann
|
||||
|
||||
<caret>val @receiver:Ann String.bar get() = ""
|
||||
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix
|
||||
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix
|
||||
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.MoveReceiverAnnotationFix
|
||||
+2
-2
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user