[kotlin] Move fix classes and findExpressionTargetArgument function to kotlin.fir.frontend-independent module

...to share them between K1 and K2.

^KTIJ-29620

GitOrigin-RevId: 80d69971c69d35600d9f9328efa12295f37e0b7d
This commit is contained in:
Andrey Cherkasov
2024-10-17 21:22:20 +00:00
committed by intellij-monorepo-bot
parent c5fa4d57b7
commit 5040f36b78
26 changed files with 117 additions and 112 deletions
@@ -0,0 +1,91 @@
// 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.quickfix
import com.intellij.modcommand.ActionContext
import com.intellij.modcommand.ModPsiUpdater
import com.intellij.modcommand.PsiUpdateModCommandAction
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
import org.jetbrains.kotlin.idea.base.codeInsight.ShortenReferencesFacility
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.psi.*
private val sourceRetention = "${StandardNames.FqNames.annotationRetention.asString()}.${AnnotationRetention.SOURCE.name}"
private val sourceRetentionAnnotation = "@${StandardNames.FqNames.retention.asString()}($sourceRetention)"
class AddSourceRetentionFix(
element: KtClass,
) : PsiUpdateModCommandAction<KtClass>(element) {
override fun getFamilyName(): String = KotlinBundle.message("add.source.retention")
override fun invoke(
context: ActionContext,
element: KtClass,
updater: ModPsiUpdater,
) {
val added = element.addAnnotationEntry(KtPsiFactory(context.project).createAnnotationEntry(sourceRetentionAnnotation))
ShortenReferencesFacility.getInstance().shorten(added)
}
}
class ChangeRetentionToSourceFix(
element: KtAnnotationEntry,
) : PsiUpdateModCommandAction<KtAnnotationEntry>(element) {
override fun getFamilyName(): String = KotlinBundle.message("change.existent.retention.to.source")
override fun invoke(
context: ActionContext,
element: KtAnnotationEntry,
updater: ModPsiUpdater,
) {
val psiFactory = KtPsiFactory(context.project)
val added = if (element.valueArgumentList == null) {
element.add(psiFactory.createCallArguments("($sourceRetention)")) as KtValueArgumentList
} else {
if (element.valueArguments.isNotEmpty()) {
element.valueArgumentList?.removeArgument(0)
}
element.valueArgumentList?.addArgument(psiFactory.createArgument(sourceRetention))
}
if (added != null) {
ShortenReferencesFacility.getInstance().shorten(added)
}
}
}
class RemoveExpressionTargetFix(
element: KtValueArgument,
) : PsiUpdateModCommandAction<KtValueArgument>(element) {
override fun getFamilyName(): String = KotlinBundle.message("remove.expression.target")
override fun invoke(
context: ActionContext,
element: KtValueArgument,
updater: ModPsiUpdater,
) {
val argumentList = element.parent as? KtValueArgumentList ?: return
if (argumentList.arguments.size == 1) {
val annotation = argumentList.parent as? KtAnnotationEntry ?: return
annotation.delete()
} else {
argumentList.removeArgument(element)
}
}
}
fun findExpressionTargetArgument(targetAnnotation: KtAnnotationEntry): KtValueArgument? {
val valueArgumentList = targetAnnotation.valueArgumentList ?: return null
if (targetAnnotation.lambdaArguments.isNotEmpty()) return null
for (valueArgument in valueArgumentList.arguments) {
val argumentExpression = valueArgument.getArgumentExpression() ?: continue
if (argumentExpression.text.contains(KotlinTarget.EXPRESSION.toString())) {
return valueArgument
}
}
return null
}
@@ -3,26 +3,18 @@
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.modcommand.ActionContext
import com.intellij.modcommand.ModPsiUpdater
import com.intellij.modcommand.PsiUpdateModCommandAction
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.base.codeInsight.ShortenReferencesFacility
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.psiUtil.containingClass
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
object RestrictedRetentionForExpressionAnnotationFactory : KotlinIntentionActionsFactory() {
private val sourceRetention = "${StandardNames.FqNames.annotationRetention.asString()}.${AnnotationRetention.SOURCE.name}"
private val sourceRetentionAnnotation = "@${StandardNames.FqNames.retention.asString()}($sourceRetention)"
override fun doCreateActions(diagnostic: Diagnostic): List<IntentionAction> {
val annotationEntry = diagnostic.psiElement as? KtAnnotationEntry ?: return emptyList()
val containingClass = annotationEntry.containingClass() ?: return emptyList()
@@ -46,82 +38,4 @@ object RestrictedRetentionForExpressionAnnotationFactory : KotlinIntentionAction
&& analyze()[BindingContext.TYPE, it.typeReference]?.constructor?.declarationDescriptor?.fqNameSafe == fqName
}
}
private fun findExpressionTargetArgument(targetAnnotation: KtAnnotationEntry): KtValueArgument? {
val valueArgumentList = targetAnnotation.valueArgumentList ?: return null
if (targetAnnotation.lambdaArguments.isNotEmpty()) return null
for (valueArgument in valueArgumentList.arguments) {
val argumentExpression = valueArgument.getArgumentExpression() ?: continue
if (argumentExpression.text.contains(KotlinTarget.EXPRESSION.toString())) {
return valueArgument
}
}
return null
}
private class AddSourceRetentionFix(
element: KtClass,
) : PsiUpdateModCommandAction<KtClass>(element) {
override fun getFamilyName(): String = KotlinBundle.message("add.source.retention")
override fun invoke(
context: ActionContext,
element: KtClass,
updater: ModPsiUpdater,
) {
val added = element.addAnnotationEntry(KtPsiFactory(context.project).createAnnotationEntry(sourceRetentionAnnotation))
ShortenReferencesFacility.getInstance().shorten(added)
}
}
private class ChangeRetentionToSourceFix(
element: KtAnnotationEntry
) : PsiUpdateModCommandAction<KtAnnotationEntry>(element) {
override fun getFamilyName(): String = KotlinBundle.message("change.existent.retention.to.source")
override fun invoke(
context: ActionContext,
element: KtAnnotationEntry,
updater: ModPsiUpdater,
) {
val psiFactory = KtPsiFactory(context.project)
val added = if (element.valueArgumentList == null) {
element.add(psiFactory.createCallArguments("($sourceRetention)")) as KtValueArgumentList
} else {
if (element.valueArguments.isNotEmpty()) {
element.valueArgumentList?.removeArgument(0)
}
element.valueArgumentList?.addArgument(psiFactory.createArgument(sourceRetention))
}
if (added != null) {
ShortenReferencesFacility.getInstance().shorten(added)
}
}
}
private class RemoveExpressionTargetFix(
element: KtValueArgument,
) : PsiUpdateModCommandAction<KtValueArgument>(element) {
override fun getFamilyName(): String = KotlinBundle.message("remove.expression.target")
override fun invoke(
context: ActionContext,
element: KtValueArgument,
updater: ModPsiUpdater,
) {
val argumentList = element.parent as? KtValueArgumentList ?: return
if (argumentList.arguments.size == 1) {
val annotation = argumentList.parent as? KtAnnotationEntry ?: return
annotation.delete()
} else {
argumentList.removeArgument(element)
}
}
}
}
@@ -1,4 +1,4 @@
// "Add SOURCE retention" "true"
<caret>@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$AddSourceRetentionFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddSourceRetentionFix
@@ -2,4 +2,4 @@
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$AddSourceRetentionFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddSourceRetentionFix
@@ -4,4 +4,4 @@ annotation class Retention
@Retention
<caret>@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$AddSourceRetentionFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddSourceRetentionFix
@@ -5,4 +5,4 @@ annotation class Retention
@Retention
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$AddSourceRetentionFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.AddSourceRetentionFix
@@ -2,4 +2,4 @@
<caret>@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$ChangeRetentionToSourceFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeRetentionToSourceFix
@@ -2,4 +2,4 @@
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$ChangeRetentionToSourceFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeRetentionToSourceFix
@@ -2,4 +2,4 @@
<caret>@Retention
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$ChangeRetentionToSourceFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeRetentionToSourceFix
@@ -2,4 +2,4 @@
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$ChangeRetentionToSourceFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeRetentionToSourceFix
@@ -2,4 +2,4 @@
<caret>@Retention()
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$ChangeRetentionToSourceFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeRetentionToSourceFix
@@ -2,4 +2,4 @@
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$ChangeRetentionToSourceFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeRetentionToSourceFix
@@ -4,4 +4,4 @@ class AnnotationRetention
<caret>@Retention()
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$ChangeRetentionToSourceFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeRetentionToSourceFix
@@ -6,4 +6,4 @@ class AnnotationRetention
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$ChangeRetentionToSourceFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.ChangeRetentionToSourceFix
@@ -2,4 +2,4 @@
<caret>@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$RemoveExpressionTargetFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveExpressionTargetFix
@@ -1,4 +1,4 @@
// "Remove EXPRESSION target" "true"
@Retention(AnnotationRetention.BINARY)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$RemoveExpressionTargetFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveExpressionTargetFix
@@ -2,4 +2,4 @@
<caret>@Retention
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$RemoveExpressionTargetFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveExpressionTargetFix
@@ -1,4 +1,4 @@
// "Remove EXPRESSION target" "true"
@Retention
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$RemoveExpressionTargetFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveExpressionTargetFix
@@ -2,4 +2,4 @@
<caret>@Retention()
@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$RemoveExpressionTargetFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveExpressionTargetFix
@@ -1,4 +1,4 @@
// "Remove EXPRESSION target" "true"
@Retention()
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$RemoveExpressionTargetFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveExpressionTargetFix
@@ -4,4 +4,4 @@ import kotlin.annotation.AnnotationTarget.*
<caret>@Retention
@Target(FIELD, EXPRESSION, PROPERTY)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$RemoveExpressionTargetFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveExpressionTargetFix
@@ -4,4 +4,4 @@ import kotlin.annotation.AnnotationTarget.*
<caret>@Retention
@Target(FIELD, PROPERTY)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$RemoveExpressionTargetFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveExpressionTargetFix
@@ -2,4 +2,4 @@
<caret>@Retention
@Target(AnnotationTarget.FIELD, AnnotationTarget.EXPRESSION, AnnotationTarget.PROPERTY)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$RemoveExpressionTargetFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveExpressionTargetFix
@@ -2,4 +2,4 @@
<caret>@Retention
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$RemoveExpressionTargetFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveExpressionTargetFix
@@ -1,4 +1,4 @@
// "Remove EXPRESSION target" "true"
<caret>@Target(AnnotationTarget.EXPRESSION)
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$RemoveExpressionTargetFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveExpressionTargetFix
@@ -1,3 +1,3 @@
// "Remove EXPRESSION target" "true"
annotation class Ann
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RestrictedRetentionForExpressionAnnotationFactory$RemoveExpressionTargetFix
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.quickfix.RemoveExpressionTargetFix