From 951dc6a43866f131815e5fc0f99258f5ab78a4ab Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Mon, 19 Sep 2016 15:00:28 +0300 Subject: [PATCH] Move: Remove companion object which becomes empty after the move #KT-13903 Fixed --- ChangeLog.md | 1 + .../MoveKotlinDeclarationsProcessor.kt | 18 ++++++++++++------ .../dropEmptyCompanion/after/Nested.kt | 3 +++ .../dropEmptyCompanion/after/test.kt | 4 ++++ .../dropEmptyCompanion/before/test.kt | 7 +++++++ .../dropEmptyCompanion/dropEmptyCompanion.test | 5 +++++ .../refactoring/move/MoveTestGenerated.java | 6 ++++++ 7 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/after/Nested.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/after/test.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/before/test.kt create mode 100644 idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/dropEmptyCompanion.test diff --git a/ChangeLog.md b/ChangeLog.md index 1b69d964471..8e2ba21d2e7 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -129,6 +129,7 @@ These artifacts include extensions for the types available in the latter JDKs, s - Pull Up: Drop 'override' modifier if moved member doesn't override anything - [`KT-13660`](https://youtrack.jetbrains.com/issue/KT-13660) Move: Do not drop object receivers when calling variable of extension functional type - [`KT-13759`](https://youtrack.jetbrains.com/issue/KT-13759) Rename: Process object-wrapping alias references +- [`KT-13903`](https://youtrack.jetbrains.com/issue/KT-13903) Move: Remove companion object which becomes empty after the move ##### New features diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsProcessor.kt index b841062d1f7..655f14ffcf7 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/moveDeclarations/MoveKotlinDeclarationsProcessor.kt @@ -54,10 +54,8 @@ import org.jetbrains.kotlin.idea.refactoring.move.moveFilesOrDirectories.MoveKot import org.jetbrains.kotlin.idea.refactoring.move.postProcessMoveUsages import org.jetbrains.kotlin.idea.references.KtSimpleNameReference.ShorteningMode import org.jetbrains.kotlin.idea.search.projectScope -import org.jetbrains.kotlin.psi.KtClassOrObject -import org.jetbrains.kotlin.psi.KtElement -import org.jetbrains.kotlin.psi.KtFile -import org.jetbrains.kotlin.psi.KtNamedDeclaration +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext import org.jetbrains.kotlin.psi.psiUtil.isAncestor import org.jetbrains.kotlin.utils.keysToMap @@ -69,9 +67,17 @@ interface Mover : (KtNamedDeclaration, KtElement) -> KtNamedDeclaration { override fun invoke(originalElement: KtNamedDeclaration, targetContainer: KtElement): KtNamedDeclaration { return when (targetContainer) { is KtFile -> targetContainer.add(originalElement) as KtNamedDeclaration - is KtClassOrObject -> targetContainer.addDeclaration(originalElement) as KtNamedDeclaration + is KtClassOrObject -> targetContainer.addDeclaration(originalElement) else -> error("Unexpected element: ${targetContainer.getElementTextWithContext()}") - }.apply { originalElement.deleteSingle() } + }.apply { + val container = originalElement.containingClassOrObject + if (container is KtObjectDeclaration && container.isCompanion() && container.declarations.singleOrNull() == originalElement) { + container.deleteSingle() + } + else { + originalElement.deleteSingle() + } + } } } diff --git a/idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/after/Nested.kt b/idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/after/Nested.kt new file mode 100644 index 00000000000..4409642d505 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/after/Nested.kt @@ -0,0 +1,3 @@ +package test + +class Nested{} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/after/test.kt b/idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/after/test.kt new file mode 100644 index 00000000000..5def2bddd51 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/after/test.kt @@ -0,0 +1,4 @@ +package test + +class Test1 { +} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/before/test.kt b/idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/before/test.kt new file mode 100644 index 00000000000..a4a6388088e --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/before/test.kt @@ -0,0 +1,7 @@ +package test + +class Test1 { + companion object { + class Nested{} + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/dropEmptyCompanion.test b/idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/dropEmptyCompanion.test new file mode 100644 index 00000000000..7d61ef53852 --- /dev/null +++ b/idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/dropEmptyCompanion.test @@ -0,0 +1,5 @@ +{ + "mainFile": "test.kt", + "type": "MOVE_KOTLIN_NESTED_CLASS", + "withRuntime": "true" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java index 4d347e2ae96..4f122aa33b0 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/move/MoveTestGenerated.java @@ -311,6 +311,12 @@ public class MoveTestGenerated extends AbstractMoveTest { doTest(fileName); } + @TestMetadata("kotlin/moveNestedClass/dropEmptyCompanion/dropEmptyCompanion.test") + public void testKotlin_moveNestedClass_dropEmptyCompanion_DropEmptyCompanion() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/move/kotlin/moveNestedClass/dropEmptyCompanion/dropEmptyCompanion.test"); + doTest(fileName); + } + @TestMetadata("kotlin/moveNestedClass/innerToTopLevelNoThis/innerToTopLevelNoThis.test") public void testKotlin_moveNestedClass_innerToTopLevelNoThis_InnerToTopLevelNoThis() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/move/kotlin/moveNestedClass/innerToTopLevelNoThis/innerToTopLevelNoThis.test");