Create actual fix: reformat

This commit is contained in:
Mikhail Glukhikh
2018-03-02 15:05:16 +03:00
parent 27d7bb595f
commit 3ec2dac10b
@@ -57,7 +57,7 @@ sealed class CreateActualFix<out D : KtNamedDeclaration>(
override fun startInWriteAction() = false override fun startInWriteAction() = false
override final fun invoke(project: Project, editor: Editor?, file: KtFile) { final override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val element = element ?: return val element = element ?: return
val factory = KtPsiFactory(project) val factory = KtPsiFactory(project)
@@ -66,15 +66,15 @@ sealed class CreateActualFix<out D : KtNamedDeclaration>(
runWriteAction { runWriteAction {
if (actualFile.packageDirective?.fqName != file.packageDirective?.fqName && if (actualFile.packageDirective?.fqName != file.packageDirective?.fqName &&
actualFile.declarations.isEmpty()) { actualFile.declarations.isEmpty()
) {
val packageDirective = file.packageDirective val packageDirective = file.packageDirective
packageDirective?.let { packageDirective?.let {
val oldPackageDirective = actualFile.packageDirective val oldPackageDirective = actualFile.packageDirective
val newPackageDirective = factory.createPackageDirective(it.fqName) val newPackageDirective = factory.createPackageDirective(it.fqName)
if (oldPackageDirective != null) { if (oldPackageDirective != null) {
oldPackageDirective.replace(newPackageDirective) oldPackageDirective.replace(newPackageDirective)
} } else {
else {
actualFile.add(newPackageDirective) actualFile.add(newPackageDirective)
} }
} }
@@ -114,17 +114,16 @@ sealed class CreateActualFix<out D : KtNamedDeclaration>(
if (existingFile is KtFile) { if (existingFile is KtFile) {
val existingPackageDirective = existingFile.packageDirective val existingPackageDirective = existingFile.packageDirective
if (existingFile.declarations.isNotEmpty() && if (existingFile.declarations.isNotEmpty() &&
existingPackageDirective?.fqName != packageDirective?.fqName) { existingPackageDirective?.fqName != packageDirective?.fqName
) {
val newName = KotlinNameSuggester.suggestNameByName(name) { val newName = KotlinNameSuggester.suggestNameByName(name) {
actualDirectory.findFile("$it.kt") == null actualDirectory.findFile("$it.kt") == null
} + ".kt" } + ".kt"
createKotlinFile(newName, actualDirectory, packageName) createKotlinFile(newName, actualDirectory, packageName)
} } else {
else {
existingFile existingFile
} }
} } else {
else {
createKotlinFile(fileName, actualDirectory, packageName) createKotlinFile(fileName, actualDirectory, packageName)
} }
} }
@@ -195,8 +194,7 @@ class CreateActualFunctionFix(
private fun KtModifierListOwner.replaceExpectModifier(actualNeeded: Boolean) { private fun KtModifierListOwner.replaceExpectModifier(actualNeeded: Boolean) {
if (actualNeeded) { if (actualNeeded) {
addModifier(KtTokens.ACTUAL_KEYWORD) addModifier(KtTokens.ACTUAL_KEYWORD)
} } else {
else {
removeModifier(KtTokens.HEADER_KEYWORD) removeModifier(KtTokens.HEADER_KEYWORD)
removeModifier(KtTokens.EXPECT_KEYWORD) removeModifier(KtTokens.EXPECT_KEYWORD)
} }
@@ -246,8 +244,7 @@ internal fun KtPsiFactory.generateClassOrObjectByExpectedClass(
is KtCallableDeclaration -> { is KtCallableDeclaration -> {
if (!isInterface && !it.hasModifier(KtTokens.ABSTRACT_KEYWORD)) { if (!isInterface && !it.hasModifier(KtTokens.ABSTRACT_KEYWORD)) {
it.delete() it.delete()
} } else {
else {
it.addModifier(KtTokens.ACTUAL_KEYWORD) it.addModifier(KtTokens.ACTUAL_KEYWORD)
} }
} }
@@ -260,8 +257,7 @@ internal fun KtPsiFactory.generateClassOrObjectByExpectedClass(
is KtClassOrObject -> is KtClassOrObject ->
if (expectedDeclaration !is KtEnumEntry) { if (expectedDeclaration !is KtEnumEntry) {
generateClassOrObjectByExpectedClass(project, expectedDeclaration, actualNeeded = true) generateClassOrObjectByExpectedClass(project, expectedDeclaration, actualNeeded = true)
} } else {
else {
continue@declLoop continue@declLoop
} }
is KtCallableDeclaration -> { is KtCallableDeclaration -> {
@@ -310,16 +306,14 @@ private fun KtPsiFactory.generateFunction(
) )
"{$delegation\n}" "{$delegation\n}"
} } else {
else {
"{}" "{}"
} }
} }
return (if (expectedFunction is KtSecondaryConstructor) { return (if (expectedFunction is KtSecondaryConstructor) {
createSecondaryConstructor(expectedFunction.text + " " + body) createSecondaryConstructor(expectedFunction.text + " " + body)
} } else {
else {
createFunction(expectedFunction.text + " " + body) createFunction(expectedFunction.text + " " + body)
} as KtFunction).apply { } as KtFunction).apply {
replaceExpectModifier(actualNeeded) replaceExpectModifier(actualNeeded)