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
}
@@ -7,11 +7,10 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.util.addAnnotation
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,
@@ -27,13 +26,6 @@ open class AddAnnotationFix(
override fun getFamilyName(): String = "Add annotation"
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
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) }
element?.addAnnotation(annotationFqName, annotationInnerText = argumentClassFqName?.let { "$it::class" })
}
}
@@ -1,6 +1,5 @@
// "Suppress 'REDUNDANT_NULLABLE' for val a" "true"
fun foo() {
@Suppress("REDUNDANT_NULLABLE")
val a: String?<caret>? = null
@Suppress("REDUNDANT_NULLABLE") val a: String?<caret>? = null
}