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 import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
fun KtModifierListOwner.addAnnotation( fun KtModifierListOwner.addAnnotation(
annotationFqName: FqName, annotationFqName: FqName,
annotationInnerText: String? = null, annotationInnerText: String? = null,
whiteSpaceText: String = "\n", whiteSpaceText: String = "\n",
addToExistingAnnotation: ((KtAnnotationEntry) -> Boolean)? = null): Boolean { addToExistingAnnotation: ((KtAnnotationEntry) -> Boolean)? = null
): Boolean {
val annotationText = when (annotationInnerText) { val annotationText = when (annotationInnerText) {
null -> "@${annotationFqName.asString()}" null -> "@${annotationFqName.asString()}"
else -> "@${annotationFqName.asString()}($annotationInnerText)" else -> "@${annotationFqName.asString()}($annotationInnerText)"
@@ -37,14 +38,8 @@ fun KtModifierListOwner.addAnnotation(
val modifierList = modifierList val modifierList = modifierList
if (modifierList == null) { if (modifierList == null) {
// create a modifier list from scratch val addedAnnotation = addAnnotationEntry(psiFactory.createAnnotationEntry(annotationText))
val newModifierList = psiFactory.createModifierList(annotationText) ShortenReferences.DEFAULT.process(addedAnnotation)
val replaced = KtPsiUtil.replaceModifierList(this, newModifierList)!!
val whiteSpace = psiFactory.createWhiteSpace(whiteSpaceText)
addAfter(whiteSpace, replaced)
ShortenReferences.DEFAULT.process(replaced)
return true return true
} }
@@ -57,7 +52,6 @@ fun KtModifierListOwner.addAnnotation(
modifierList.addAfter(whiteSpace, addedAnnotation) modifierList.addAfter(whiteSpace, addedAnnotation)
ShortenReferences.DEFAULT.process(addedAnnotation) ShortenReferences.DEFAULT.process(addedAnnotation)
return true return true
} }
@@ -7,11 +7,10 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project 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.name.FqName
import org.jetbrains.kotlin.psi.KtDeclaration import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPsiFactory
open class AddAnnotationFix( open class AddAnnotationFix(
element: KtDeclaration, element: KtDeclaration,
@@ -27,13 +26,6 @@ open class AddAnnotationFix(
override fun getFamilyName(): String = "Add annotation" override fun getFamilyName(): String = "Add annotation"
override fun invoke(project: Project, editor: Editor?, file: KtFile) { override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val factory = KtPsiFactory(project) element?.addAnnotation(annotationFqName, annotationInnerText = argumentClassFqName?.let { "$it::class" })
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) }
} }
} }
@@ -1,6 +1,5 @@
// "Suppress 'REDUNDANT_NULLABLE' for val a" "true" // "Suppress 'REDUNDANT_NULLABLE' for val a" "true"
fun foo() { fun foo() {
@Suppress("REDUNDANT_NULLABLE") @Suppress("REDUNDANT_NULLABLE") val a: String?<caret>? = null
val a: String?<caret>? = null
} }