Add annotations to primary constructor together with 'constructor'

This fixes incorrect behaviour if primary constructor has no explicit
'constructor' keywork
#KT-25548 Fixed
This commit is contained in:
Mikhail Glukhikh
2018-08-20 17:22:42 +03:00
parent c12f29ae30
commit 564ea629d1
5 changed files with 40 additions and 4 deletions
@@ -7,10 +7,11 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.util.addAnnotation
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPsiFactory
open class AddAnnotationFix(
element: KtDeclaration,
@@ -26,6 +27,13 @@ open class AddAnnotationFix(
override fun getFamilyName(): String = "Add annotation"
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
element?.addAnnotation(annotationFqName, annotationInnerText = argumentClassFqName?.let { "$it::class" })
val factory = KtPsiFactory(project)
val annotationInnerText = argumentClassFqName?.let { "$it::class" }
val annotationText = when (annotationInnerText) {
null -> "@${annotationFqName.asString()}"
else -> "@${annotationFqName.asString()}($annotationInnerText)"
}
val addedAnnotation = element?.addAnnotationEntry(factory.createAnnotationEntry(annotationText))
addedAnnotation?.let { ShortenReferences.DEFAULT.process(it) }
}
}