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
@@ -62,9 +62,8 @@ class KtPrimaryConstructor : KtConstructor<KtPrimaryConstructor> {
return if (modifierList != null) {
modifierList.addBefore(annotationEntry, modifierList.firstChild) as KtAnnotationEntry
} else {
val parameterList = valueParameterList!!
val newModifierList = KtPsiFactory(project).createModifierList(annotationEntry.text)
(addBefore(newModifierList, parameterList) as KtModifierList).annotationEntries.first()
(addBefore(newModifierList, getOrCreateConstructorKeyword()) as KtModifierList).annotationEntries.first()
}
}
}
@@ -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) }
}
}
@@ -0,0 +1,12 @@
// "Add '@PropertyTypeMarker' annotation to 'PropertyTypeContainer'" "true"
// COMPILER_ARGUMENTS: -Xuse-experimental=kotlin.Experimental
// WITH_RUNTIME
// ACTION: Add '@PropertyTypeMarker' annotation to containing class 'PropertyTypeContainer'
@Experimental
annotation class PropertyTypeMarker
@PropertyTypeMarker
class PropertyTypeMarked
class PropertyTypeContainer(val subject: Property<caret>TypeMarked)
@@ -0,0 +1,12 @@
// "Add '@PropertyTypeMarker' annotation to 'PropertyTypeContainer'" "true"
// COMPILER_ARGUMENTS: -Xuse-experimental=kotlin.Experimental
// WITH_RUNTIME
// ACTION: Add '@PropertyTypeMarker' annotation to containing class 'PropertyTypeContainer'
@Experimental
annotation class PropertyTypeMarker
@PropertyTypeMarker
class PropertyTypeMarked
class PropertyTypeContainer @PropertyTypeMarker constructor(val subject: PropertyTypeMarked)
@@ -6234,6 +6234,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/experimental/override.kt");
}
@TestMetadata("propertyInConstructor.kt")
public void testPropertyInConstructor() throws Exception {
runTest("idea/testData/quickfix/experimental/propertyInConstructor.kt");
}
@TestMetadata("switchOn.kt")
public void testSwitchOn() throws Exception {
runTest("idea/testData/quickfix/experimental/switchOn.kt");