Fix KtModifierListOwner.addAnnotation (case without modifier list)

Now add annotation to primary constructor should work correctly,
and may be some other related cases too.

Related to KT-25548
This commit is contained in:
Mikhail Glukhikh
2018-08-23 18:32:07 +03:00
parent 5f2c7d3c84
commit 55ff519aa8
3 changed files with 10 additions and 25 deletions
@@ -24,10 +24,11 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
fun KtModifierListOwner.addAnnotation(
annotationFqName: FqName,
annotationInnerText: String? = null,
whiteSpaceText: String = "\n",
addToExistingAnnotation: ((KtAnnotationEntry) -> Boolean)? = null): Boolean {
annotationFqName: FqName,
annotationInnerText: String? = null,
whiteSpaceText: String = "\n",
addToExistingAnnotation: ((KtAnnotationEntry) -> Boolean)? = null
): Boolean {
val annotationText = when (annotationInnerText) {
null -> "@${annotationFqName.asString()}"
else -> "@${annotationFqName.asString()}($annotationInnerText)"
@@ -37,14 +38,8 @@ fun KtModifierListOwner.addAnnotation(
val modifierList = modifierList
if (modifierList == null) {
// create a modifier list from scratch
val newModifierList = psiFactory.createModifierList(annotationText)
val replaced = KtPsiUtil.replaceModifierList(this, newModifierList)!!
val whiteSpace = psiFactory.createWhiteSpace(whiteSpaceText)
addAfter(whiteSpace, replaced)
ShortenReferences.DEFAULT.process(replaced)
val addedAnnotation = addAnnotationEntry(psiFactory.createAnnotationEntry(annotationText))
ShortenReferences.DEFAULT.process(addedAnnotation)
return true
}
@@ -57,7 +52,6 @@ fun KtModifierListOwner.addAnnotation(
modifierList.addAfter(whiteSpace, addedAnnotation)
ShortenReferences.DEFAULT.process(addedAnnotation)
return true
}