Reformat: Convert to primary constructor

This commit is contained in:
Mikhail Glukhikh
2018-05-08 17:42:59 +03:00
parent a41064295b
commit 67d2abbe5e
@@ -100,7 +100,8 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingRangeIntentio
val constructorDescriptor = context[BindingContext.CONSTRUCTOR, this] ?: return null val constructorDescriptor = context[BindingContext.CONSTRUCTOR, this] ?: return null
val initializer = factory.createAnonymousInitializer() as? KtClassInitializer val initializer = factory.createAnonymousInitializer() as? KtClassInitializer
for (statement in bodyExpression?.statements ?: emptyList()) { for (statement in bodyExpression?.statements ?: emptyList()) {
val (rightDescriptor, leftDescriptor) = statement.tryConvertToPropertyByParameterInitialization(constructorDescriptor, context) val (rightDescriptor, leftDescriptor) =
statement.tryConvertToPropertyByParameterInitialization(constructorDescriptor, context)
?: with(initializer) { ?: with(initializer) {
(initializer?.body as? KtBlockExpression)?.let { (initializer?.body as? KtBlockExpression)?.let {
it.addBefore(statement.copy(), it.rBrace) it.addBefore(statement.copy(), it.rBrace)
@@ -131,7 +132,8 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingRangeIntentio
if (property != null) { if (property != null) {
if (propertyDescriptor.name == parameterDescriptor.name && if (propertyDescriptor.name == parameterDescriptor.name &&
propertyDescriptor.type == parameterDescriptor.type && propertyDescriptor.type == parameterDescriptor.type &&
propertyDescriptor.accessors.all { it.isDefault }) { propertyDescriptor.accessors.all { it.isDefault }
) {
propertyCommentSaver = CommentSaver(property) propertyCommentSaver = CommentSaver(property)
val valOrVar = if (property.isVar) factory.createVarKeyword() else factory.createValKeyword() val valOrVar = if (property.isVar) factory.createVarKeyword() else factory.createValKeyword()
newParameter.addBefore(valOrVar, newParameter.nameIdentifier) newParameter.addBefore(valOrVar, newParameter.nameIdentifier)
@@ -141,8 +143,7 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingRangeIntentio
newParameter.addBefore(newModifiers, newParameter.valOrVarKeyword) newParameter.addBefore(newModifiers, newParameter.valOrVarKeyword)
} }
property.delete() property.delete()
} } else {
else {
property.initializer = factory.createSimpleName(parameterDescriptor.name.asString()) property.initializer = factory.createSimpleName(parameterDescriptor.name.asString())
} }
} }
@@ -179,7 +180,7 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingRangeIntentio
} }
} }
with (constructorInClass.replace(constructor)) { with(constructorInClass.replace(constructor)) {
constructorCommentSaver.restore(this) constructorCommentSaver.restore(this)
} }