Minor refactoring: convert secondary constructor to primary
(cherry picked from commit 47446b7)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
e417a85276
commit
ee5202f2ea
+37
-17
@@ -90,20 +90,16 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingRangeIntentio
|
|||||||
return rightDescriptor to leftDescriptor
|
return rightDescriptor to leftDescriptor
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtSecondaryConstructor, editor: Editor?) {
|
private fun KtSecondaryConstructor.extractInitializer(
|
||||||
val klass = element.containingClassOrObject as? KtClass ?: return
|
parameterToPropertyMap: MutableMap<ValueParameterDescriptor, PropertyDescriptor>,
|
||||||
val context = klass.analyzeFully()
|
context: BindingContext,
|
||||||
val constructorDescriptor = context[BindingContext.CONSTRUCTOR, element] ?: return
|
factory: KtPsiFactory
|
||||||
val factory = KtPsiFactory(klass)
|
): KtClassInitializer? {
|
||||||
val constructorCommentSaver = CommentSaver(element)
|
val constructorDescriptor = context[BindingContext.CONSTRUCTOR, this] ?: return null
|
||||||
val constructorInClass = klass.createPrimaryConstructorIfAbsent()
|
|
||||||
val constructor = factory.createPrimaryConstructor(element.modifierList?.text?.replace("\n", " "))
|
|
||||||
val parameterList = constructor.valueParameterList!!
|
|
||||||
val parameterToPropertyMap = mutableMapOf<ValueParameterDescriptor, PropertyDescriptor>()
|
|
||||||
val initializer = factory.createAnonymousInitializer() as? KtClassInitializer
|
val initializer = factory.createAnonymousInitializer() as? KtClassInitializer
|
||||||
for (statement in element.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)
|
||||||
it.addBefore(factory.createNewLine(), it.rBrace)
|
it.addBefore(factory.createNewLine(), it.rBrace)
|
||||||
@@ -113,8 +109,17 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingRangeIntentio
|
|||||||
if (rightDescriptor == null || leftDescriptor == null) continue
|
if (rightDescriptor == null || leftDescriptor == null) continue
|
||||||
parameterToPropertyMap[rightDescriptor] = leftDescriptor
|
parameterToPropertyMap[rightDescriptor] = leftDescriptor
|
||||||
}
|
}
|
||||||
|
return initializer
|
||||||
|
}
|
||||||
|
|
||||||
for (parameter in element.valueParameters) {
|
private fun KtSecondaryConstructor.moveParametersToPrimaryConstructorAndInitializers(
|
||||||
|
primaryConstructor: KtPrimaryConstructor,
|
||||||
|
parameterToPropertyMap: MutableMap<ValueParameterDescriptor, PropertyDescriptor>,
|
||||||
|
context: BindingContext,
|
||||||
|
factory: KtPsiFactory
|
||||||
|
) {
|
||||||
|
val parameterList = primaryConstructor.valueParameterList!!
|
||||||
|
for (parameter in valueParameters) {
|
||||||
val newParameter = factory.createParameter(parameter.text)
|
val newParameter = factory.createParameter(parameter.text)
|
||||||
val parameterDescriptor = context[BindingContext.VALUE_PARAMETER, parameter]
|
val parameterDescriptor = context[BindingContext.VALUE_PARAMETER, parameter]
|
||||||
val propertyDescriptor = parameterToPropertyMap[parameterDescriptor]
|
val propertyDescriptor = parameterToPropertyMap[parameterDescriptor]
|
||||||
@@ -138,10 +143,24 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingRangeIntentio
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
with (parameterList.addParameter(newParameter)) {
|
with(parameterList.addParameter(newParameter)) {
|
||||||
propertyCommentSaver?.restore(this)
|
propertyCommentSaver?.restore(this@with)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun applyTo(element: KtSecondaryConstructor, editor: Editor?) {
|
||||||
|
val klass = element.containingClassOrObject as? KtClass ?: return
|
||||||
|
val context = klass.analyzeFully()
|
||||||
|
val factory = KtPsiFactory(klass)
|
||||||
|
val constructorCommentSaver = CommentSaver(element)
|
||||||
|
val constructorInClass = klass.createPrimaryConstructorIfAbsent()
|
||||||
|
val constructor = factory.createPrimaryConstructor(element.modifierList?.text?.replace("\n", " "))
|
||||||
|
|
||||||
|
val parameterToPropertyMap = mutableMapOf<ValueParameterDescriptor, PropertyDescriptor>()
|
||||||
|
val initializer = element.extractInitializer(parameterToPropertyMap, context, factory) ?: return
|
||||||
|
|
||||||
|
element.moveParametersToPrimaryConstructorAndInitializers(constructor, parameterToPropertyMap, context, factory)
|
||||||
|
|
||||||
val delegationCall = element.getDelegationCall()
|
val delegationCall = element.getDelegationCall()
|
||||||
val argumentList = delegationCall.valueArgumentList
|
val argumentList = delegationCall.valueArgumentList
|
||||||
@@ -161,8 +180,9 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingRangeIntentio
|
|||||||
constructorCommentSaver.restore(this)
|
constructorCommentSaver.restore(this)
|
||||||
}
|
}
|
||||||
element.delete()
|
element.delete()
|
||||||
if ((initializer?.body as? KtBlockExpression)?.statements?.isNotEmpty() ?: false) {
|
|
||||||
initializer?.let { klass.addDeclaration(it) }
|
if ((initializer.body as? KtBlockExpression)?.statements?.isNotEmpty() ?: false) {
|
||||||
|
klass.addDeclaration(initializer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user