Create expect / actual refactoring: get element description at one place
This commit is contained in:
@@ -60,7 +60,7 @@ sealed class CreateActualFix<out D : KtNamedDeclaration>(
|
|||||||
|
|
||||||
override fun getFamilyName() = text
|
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})"
|
override fun getText() = "Create actual $elementType for module ${actualModule.name} (${actualPlatform.platform})"
|
||||||
|
|
||||||
@@ -137,10 +137,7 @@ class CreateActualClassFix(
|
|||||||
actualPlatform: MultiTargetPlatform.Specific
|
actualPlatform: MultiTargetPlatform.Specific
|
||||||
) : CreateActualFix<KtClassOrObject>(klass, actualModule, actualPlatform, { project, element ->
|
) : CreateActualFix<KtClassOrObject>(klass, actualModule, actualPlatform, { project, element ->
|
||||||
generateClassOrObjectByExpectedClass(project, element, actualNeeded = true)
|
generateClassOrObjectByExpectedClass(project, element, actualNeeded = true)
|
||||||
}) {
|
})
|
||||||
|
|
||||||
override val elementType = element.getTypeDescription()
|
|
||||||
}
|
|
||||||
|
|
||||||
class CreateActualPropertyFix(
|
class CreateActualPropertyFix(
|
||||||
property: KtProperty,
|
property: KtProperty,
|
||||||
@@ -149,10 +146,7 @@ class CreateActualPropertyFix(
|
|||||||
) : CreateActualFix<KtProperty>(property, actualModule, actualPlatform, { project, element ->
|
) : CreateActualFix<KtProperty>(property, actualModule, actualPlatform, { project, element ->
|
||||||
val descriptor = element.toDescriptor() as? PropertyDescriptor
|
val descriptor = element.toDescriptor() as? PropertyDescriptor
|
||||||
descriptor?.let { generateProperty(project, element, descriptor) }
|
descriptor?.let { generateProperty(project, element, descriptor) }
|
||||||
}) {
|
})
|
||||||
|
|
||||||
override val elementType = "property"
|
|
||||||
}
|
|
||||||
|
|
||||||
class CreateActualFunctionFix(
|
class CreateActualFunctionFix(
|
||||||
function: KtFunction,
|
function: KtFunction,
|
||||||
@@ -161,10 +155,7 @@ class CreateActualFunctionFix(
|
|||||||
) : CreateActualFix<KtFunction>(function, actualModule, actualPlatform, { project, element ->
|
) : CreateActualFix<KtFunction>(function, actualModule, actualPlatform, { project, element ->
|
||||||
val descriptor = element.toDescriptor() as? FunctionDescriptor
|
val descriptor = element.toDescriptor() as? FunctionDescriptor
|
||||||
descriptor?.let { generateFunction(project, element, descriptor) }
|
descriptor?.let { generateFunction(project, element, descriptor) }
|
||||||
}) {
|
})
|
||||||
|
|
||||||
override val elementType = "function"
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun KtModifierListOwner.replaceExpectModifier(actualNeeded: Boolean) {
|
private fun KtModifierListOwner.replaceExpectModifier(actualNeeded: Boolean) {
|
||||||
if (actualNeeded) {
|
if (actualNeeded) {
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ sealed class CreateExpectedFix<out D : KtNamedDeclaration>(
|
|||||||
|
|
||||||
override fun getFamilyName() = text
|
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}"
|
override fun getText() = "Create expected $elementType in common module ${commonModule.name}"
|
||||||
|
|
||||||
@@ -152,10 +152,7 @@ class CreateExpectedClassFix(
|
|||||||
commonModule: Module
|
commonModule: Module
|
||||||
) : CreateExpectedFix<KtClassOrObject>(klass, outerExpectedClass, commonModule, { project, element ->
|
) : CreateExpectedFix<KtClassOrObject>(klass, outerExpectedClass, commonModule, { project, element ->
|
||||||
generateClassOrObjectByActualClass(project, element, listOfNotNull(outerExpectedClass))
|
generateClassOrObjectByActualClass(project, element, listOfNotNull(outerExpectedClass))
|
||||||
}) {
|
})
|
||||||
|
|
||||||
override val elementType = element.getTypeDescription()
|
|
||||||
}
|
|
||||||
|
|
||||||
class CreateExpectedPropertyFix(
|
class CreateExpectedPropertyFix(
|
||||||
property: KtNamedDeclaration,
|
property: KtNamedDeclaration,
|
||||||
@@ -164,10 +161,7 @@ class CreateExpectedPropertyFix(
|
|||||||
) : CreateExpectedFix<KtNamedDeclaration>(property, targetExpectedClass, commonModule, { project, element ->
|
) : CreateExpectedFix<KtNamedDeclaration>(property, targetExpectedClass, commonModule, { project, element ->
|
||||||
val descriptor = element.toDescriptor() as? PropertyDescriptor
|
val descriptor = element.toDescriptor() as? PropertyDescriptor
|
||||||
descriptor?.let { generateProperty(project, element, descriptor, targetExpectedClass, emptyList()) }
|
descriptor?.let { generateProperty(project, element, descriptor, targetExpectedClass, emptyList()) }
|
||||||
}) {
|
})
|
||||||
|
|
||||||
override val elementType = "property"
|
|
||||||
}
|
|
||||||
|
|
||||||
class CreateExpectedFunctionFix(
|
class CreateExpectedFunctionFix(
|
||||||
function: KtFunction,
|
function: KtFunction,
|
||||||
@@ -176,10 +170,7 @@ class CreateExpectedFunctionFix(
|
|||||||
) : CreateExpectedFix<KtFunction>(function, targetExpectedClass, commonModule, { project, element ->
|
) : CreateExpectedFix<KtFunction>(function, targetExpectedClass, commonModule, { project, element ->
|
||||||
val descriptor = element.toDescriptor() as? FunctionDescriptor
|
val descriptor = element.toDescriptor() as? FunctionDescriptor
|
||||||
descriptor?.let { generateFunction(project, element, descriptor, targetExpectedClass, emptyList()) }
|
descriptor?.let { generateFunction(project, element, descriptor, targetExpectedClass, emptyList()) }
|
||||||
}) {
|
})
|
||||||
|
|
||||||
override val elementType = "function"
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun KtPsiFactory.generateClassOrObjectByActualClass(
|
private fun KtPsiFactory.generateClassOrObjectByActualClass(
|
||||||
project: Project,
|
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 KtObjectDeclaration -> "object"
|
||||||
is KtClass -> when {
|
is KtClass -> when {
|
||||||
isInterface() -> "interface"
|
isInterface() -> "interface"
|
||||||
@@ -71,5 +71,7 @@ fun KtClassOrObject?.getTypeDescription(): String = when (this) {
|
|||||||
isAnnotation() -> "annotation class"
|
isAnnotation() -> "annotation class"
|
||||||
else -> "class"
|
else -> "class"
|
||||||
}
|
}
|
||||||
else -> "class"
|
is KtProperty, is KtParameter -> "property"
|
||||||
|
is KtFunction -> "function"
|
||||||
|
else -> "declaration"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user