Create expect / actual refactoring: get element description at one place

This commit is contained in:
Mikhail Glukhikh
2018-11-21 16:06:23 +03:00
parent 933d10e75d
commit d54b5b68cc
3 changed files with 12 additions and 28 deletions
@@ -60,7 +60,7 @@ sealed class CreateActualFix<out D : KtNamedDeclaration>(
override fun getFamilyName() = text
protected abstract val elementType: String
protected val elementType: String = element.getTypeDescription()
override fun getText() = "Create actual $elementType for module ${actualModule.name} (${actualPlatform.platform})"
@@ -137,10 +137,7 @@ class CreateActualClassFix(
actualPlatform: MultiTargetPlatform.Specific
) : CreateActualFix<KtClassOrObject>(klass, actualModule, actualPlatform, { project, element ->
generateClassOrObjectByExpectedClass(project, element, actualNeeded = true)
}) {
override val elementType = element.getTypeDescription()
}
})
class CreateActualPropertyFix(
property: KtProperty,
@@ -149,10 +146,7 @@ class CreateActualPropertyFix(
) : CreateActualFix<KtProperty>(property, actualModule, actualPlatform, { project, element ->
val descriptor = element.toDescriptor() as? PropertyDescriptor
descriptor?.let { generateProperty(project, element, descriptor) }
}) {
override val elementType = "property"
}
})
class CreateActualFunctionFix(
function: KtFunction,
@@ -161,10 +155,7 @@ class CreateActualFunctionFix(
) : CreateActualFix<KtFunction>(function, actualModule, actualPlatform, { project, element ->
val descriptor = element.toDescriptor() as? FunctionDescriptor
descriptor?.let { generateFunction(project, element, descriptor) }
}) {
override val elementType = "function"
}
})
private fun KtModifierListOwner.replaceExpectModifier(actualNeeded: Boolean) {
if (actualNeeded) {
@@ -55,7 +55,7 @@ sealed class CreateExpectedFix<out D : KtNamedDeclaration>(
override fun getFamilyName() = text
protected abstract val elementType: String
protected val elementType: String = element.getTypeDescription()
override fun getText() = "Create expected $elementType in common module ${commonModule.name}"
@@ -152,10 +152,7 @@ class CreateExpectedClassFix(
commonModule: Module
) : CreateExpectedFix<KtClassOrObject>(klass, outerExpectedClass, commonModule, { project, element ->
generateClassOrObjectByActualClass(project, element, listOfNotNull(outerExpectedClass))
}) {
override val elementType = element.getTypeDescription()
}
})
class CreateExpectedPropertyFix(
property: KtNamedDeclaration,
@@ -164,10 +161,7 @@ class CreateExpectedPropertyFix(
) : CreateExpectedFix<KtNamedDeclaration>(property, targetExpectedClass, commonModule, { project, element ->
val descriptor = element.toDescriptor() as? PropertyDescriptor
descriptor?.let { generateProperty(project, element, descriptor, targetExpectedClass, emptyList()) }
}) {
override val elementType = "property"
}
})
class CreateExpectedFunctionFix(
function: KtFunction,
@@ -176,10 +170,7 @@ class CreateExpectedFunctionFix(
) : CreateExpectedFix<KtFunction>(function, targetExpectedClass, commonModule, { project, element ->
val descriptor = element.toDescriptor() as? FunctionDescriptor
descriptor?.let { generateFunction(project, element, descriptor, targetExpectedClass, emptyList()) }
}) {
override val elementType = "function"
}
})
private fun KtPsiFactory.generateClassOrObjectByActualClass(
project: Project,
@@ -63,7 +63,7 @@ fun KtPsiFactory.createClassHeaderCopyByText(originalClass: KtClassOrObject): Kt
}
}
fun KtClassOrObject?.getTypeDescription(): String = when (this) {
fun KtNamedDeclaration?.getTypeDescription(): String = when (this) {
is KtObjectDeclaration -> "object"
is KtClass -> when {
isInterface() -> "interface"
@@ -71,5 +71,7 @@ fun KtClassOrObject?.getTypeDescription(): String = when (this) {
isAnnotation() -> "annotation class"
else -> "class"
}
else -> "class"
is KtProperty, is KtParameter -> "property"
is KtFunction -> "function"
else -> "declaration"
}