AndroidViewConstructorFix: do not reformat while creating constructor

Before this commit, it was done by createDeclarationByPattern,
preventing later reformatting in addAnnotation

Related to KT-27945
This commit is contained in:
Mikhail Glukhikh
2018-11-06 12:33:30 +03:00
parent d0d98dc283
commit e597dda526
@@ -52,18 +52,15 @@ class KotlinAndroidViewConstructorFix(element: KtSuperTypeEntry) : KotlinQuickFi
val factory = KtPsiFactory(element) val factory = KtPsiFactory(element)
val newPrimaryConstructor = factory.createDeclarationByPattern<KtClass>( val newPrimaryConstructor = factory.createPrimaryConstructor(
"class A constructor(\n $0, $1, $2\n)", """(
factory.createParameter("context: android.content.Context"), context: android.content.Context, attrs: android.util.AttributeSet? = null, defStyleAttr: Int = 0
factory.createParameter("attrs: android.util.AttributeSet? = null"), )""".trimIndent()
factory.createParameter("defStyleAttr: Int = 0") )
).primaryConstructor ?: return
val primaryConstructor = ktClass.createPrimaryConstructorIfAbsent().replaced(newPrimaryConstructor) val primaryConstructor = ktClass.createPrimaryConstructorIfAbsent().replaced(newPrimaryConstructor)
primaryConstructor.valueParameterList?.let { ShortenReferences.DEFAULT.process(it) } primaryConstructor.valueParameterList?.let { ShortenReferences.DEFAULT.process(it) }
primaryConstructor.addAnnotation(fqNameAnnotation)
primaryConstructor.addAnnotation(fqNameAnnotation, whiteSpaceText = " ")
primaryConstructor.addAfter(factory.createWhiteSpace(" "), primaryConstructor.modifierList)
element.replace(factory.createSuperTypeCallEntry(element.text + "(context, attrs, defStyleAttr)")) element.replace(factory.createSuperTypeCallEntry(element.text + "(context, attrs, defStyleAttr)"))
} }
@@ -73,7 +70,7 @@ class KotlinAndroidViewConstructorFix(element: KtSuperTypeEntry) : KotlinQuickFi
private val fqNameAnnotation = FqName("kotlin.jvm.JvmOverloads") private val fqNameAnnotation = FqName("kotlin.jvm.JvmOverloads")
private val requiredConstructorParameterTypes = private val requiredConstructorParameterTypes =
listOf("android.content.Context", "android.util.AttributeSet", "kotlin.Int") listOf("android.content.Context", "android.util.AttributeSet", "kotlin.Int")
override fun createAction(diagnostic: Diagnostic): IntentionAction? { override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val superTypeEntry = SUPERTYPE_NOT_INITIALIZED.cast(diagnostic).psiElement val superTypeEntry = SUPERTYPE_NOT_INITIALIZED.cast(diagnostic).psiElement
@@ -99,7 +96,7 @@ class KotlinAndroidViewConstructorFix(element: KtSuperTypeEntry) : KotlinQuickFi
private fun KotlinType.constructorParameters(): List<List<String?>>? { private fun KotlinType.constructorParameters(): List<List<String?>>? {
val classDescriptor = constructor.declarationDescriptor as? ClassDescriptor ?: return null val classDescriptor = constructor.declarationDescriptor as? ClassDescriptor ?: return null
return classDescriptor.constructors.map { return classDescriptor.constructors.map {
it.valueParameters.map { it.type.getFqNameAsString() } it.valueParameters.map { parameter -> parameter.type.getFqNameAsString() }
} }
} }
} }