"Add annotation target" quickfix: add SOURCE retention when target is EXPRESSION #KT-26306 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-08-29 13:10:36 +09:00
committed by Mikhail Glukhikh
parent ddf92ef187
commit 3ea0222d43
6 changed files with 56 additions and 4 deletions
@@ -119,9 +119,9 @@ private fun PsiAnnotation.getActualTargetList(): List<KotlinTarget> {
private fun KtAnnotationEntry.getActualTargetList(): List<KotlinTarget> {
val annotatedElement = getStrictParentOfType<KtModifierList>()?.owner as? KtElement
?: getStrictParentOfType<KtAnnotatedExpression>()?.baseExpression
?: getStrictParentOfType<KtFile>()
?: return emptyList()
?: getStrictParentOfType<KtAnnotatedExpression>()?.baseExpression
?: getStrictParentOfType<KtFile>()
?: return emptyList()
val targetList = AnnotationChecker.getActualTargetList(annotatedElement, null, BindingTraceContext().bindingContext)
@@ -147,8 +147,17 @@ private fun KtAnnotationEntry.getActualTargetList(): List<KotlinTarget> {
}
private fun KtClass.addAnnotationTargets(annotationTargets: List<KotlinTarget>, psiFactory: KtPsiFactory) {
val targetAnnotationName = KotlinBuiltIns.FQ_NAMES.target.shortName().asString()
val retentionAnnotationName = KotlinBuiltIns.FQ_NAMES.retention.shortName().asString()
if (annotationTargets.any { it == KotlinTarget.EXPRESSION }
&& annotationEntries.none { it.typeReference?.text == retentionAnnotationName }) {
addAnnotationEntry(
psiFactory.createAnnotationEntry(
"@$retentionAnnotationName(${KotlinBuiltIns.FQ_NAMES.annotationRetention.shortName()}.${AnnotationRetention.SOURCE.name})"
)
)
}
val targetAnnotationName = KotlinBuiltIns.FQ_NAMES.target.shortName().asString()
val targetAnnotationEntry = annotationEntries.find { it.typeReference?.text == targetAnnotationName } ?: run {
val text = "@$targetAnnotationName${annotationTargets.toArgumentListString()}"
addAnnotationEntry(psiFactory.createAnnotationEntry(text))
@@ -0,0 +1,7 @@
// "Add annotation target" "true"
annotation class Foo
fun test() {
var v = 0
<caret>@Foo v++
}
@@ -0,0 +1,9 @@
// "Add annotation target" "true"
@Target(AnnotationTarget.EXPRESSION)
@Retention(AnnotationRetention.SOURCE)
annotation class Foo
fun test() {
var v = 0
@Foo v++
}
@@ -0,0 +1,8 @@
// "Add annotation target" "true"
@Retention
annotation class Foo
fun test() {
var v = 0
<caret>@Foo v++
}
@@ -0,0 +1,9 @@
// "Add annotation target" "true"
@Target(AnnotationTarget.EXPRESSION)
@Retention
annotation class Foo
fun test() {
var v = 0
@Foo v++
}
@@ -239,6 +239,16 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/addAnnotationTarget/expression.kt");
}
@TestMetadata("expression2.kt")
public void testExpression2() throws Exception {
runTest("idea/testData/quickfix/addAnnotationTarget/expression2.kt");
}
@TestMetadata("expression3.kt")
public void testExpression3() throws Exception {
runTest("idea/testData/quickfix/addAnnotationTarget/expression3.kt");
}
@TestMetadata("file.kt")
public void testFile() throws Exception {
runTest("idea/testData/quickfix/addAnnotationTarget/file.kt");