"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))