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:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user