From c8c0ab16473e898b5d69cb7abfe4ccc3309b1337 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Tue, 19 Sep 2017 16:37:09 +0300 Subject: [PATCH] Copy: Fix import insertion on declaration copying #KT-19909 Fixed --- .../copy/CopyKotlinDeclarationsHandler.kt | 3 ++- .../copy/copyWithImportInsertion/after/bar/Bar.kt | 3 +++ .../copy/copyWithImportInsertion/after/foo/Foo.kt | 12 ++++++++++++ .../copy/copyWithImportInsertion/after/foo/Foo2.kt | 9 +++++++++ .../copy/copyWithImportInsertion/before/bar/Bar.kt | 3 +++ .../copy/copyWithImportInsertion/before/foo/Foo.kt | 12 ++++++++++++ .../copyWithImportInsertion.test | 5 +++++ .../idea/refactoring/copy/CopyTestGenerated.java | 6 ++++++ 8 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 idea/testData/refactoring/copy/copyWithImportInsertion/after/bar/Bar.kt create mode 100644 idea/testData/refactoring/copy/copyWithImportInsertion/after/foo/Foo.kt create mode 100644 idea/testData/refactoring/copy/copyWithImportInsertion/after/foo/Foo2.kt create mode 100644 idea/testData/refactoring/copy/copyWithImportInsertion/before/bar/Bar.kt create mode 100644 idea/testData/refactoring/copy/copyWithImportInsertion/before/foo/Foo.kt create mode 100644 idea/testData/refactoring/copy/copyWithImportInsertion/copyWithImportInsertion.test diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt index 1fe0ac262bc..c249282514f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/copy/CopyKotlinDeclarationsHandler.kt @@ -265,6 +265,7 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() { runWriteAction { val newElements = elementsToCopy.map { targetFile.add(it.copy()) as KtNamedDeclaration } elementsToCopy.zip(newElements).toMap(oldToNewElementsMapping) + oldToNewElementsMapping[originalFile] = targetFile for (newElement in oldToNewElementsMapping.values) { restoredInternalUsages += restoreInternalUsages(newElement as KtElement, oldToNewElementsMapping, true) @@ -275,7 +276,7 @@ class CopyKotlinDeclarationsHandler : CopyHandlerDelegateBase() { } } - (oldToNewElementsMapping.values.singleOrNull() as? KtNamedDeclaration)?.let { newDeclaration -> + (oldToNewElementsMapping.values.filterIsInstance().singleOrNull())?.let { newDeclaration -> if (newName == newDeclaration.name) return@let val selfReferences = ReferencesSearch.search(newDeclaration, LocalSearchScope(newDeclaration)).findAll() runWriteAction { diff --git a/idea/testData/refactoring/copy/copyWithImportInsertion/after/bar/Bar.kt b/idea/testData/refactoring/copy/copyWithImportInsertion/after/bar/Bar.kt new file mode 100644 index 00000000000..213ed0b96c7 --- /dev/null +++ b/idea/testData/refactoring/copy/copyWithImportInsertion/after/bar/Bar.kt @@ -0,0 +1,3 @@ +package bar + +fun Any.bar() {} \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copyWithImportInsertion/after/foo/Foo.kt b/idea/testData/refactoring/copy/copyWithImportInsertion/after/foo/Foo.kt new file mode 100644 index 00000000000..bcd2249246a --- /dev/null +++ b/idea/testData/refactoring/copy/copyWithImportInsertion/after/foo/Foo.kt @@ -0,0 +1,12 @@ +package foo + +import bar.bar + +class Foo { + init { + 42.bar() + } +} + +class Baz { +} diff --git a/idea/testData/refactoring/copy/copyWithImportInsertion/after/foo/Foo2.kt b/idea/testData/refactoring/copy/copyWithImportInsertion/after/foo/Foo2.kt new file mode 100644 index 00000000000..ec7061f4031 --- /dev/null +++ b/idea/testData/refactoring/copy/copyWithImportInsertion/after/foo/Foo2.kt @@ -0,0 +1,9 @@ +package foo + +import bar.bar + +class Foo2 { + init { + 42.bar() + } +} \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copyWithImportInsertion/before/bar/Bar.kt b/idea/testData/refactoring/copy/copyWithImportInsertion/before/bar/Bar.kt new file mode 100644 index 00000000000..213ed0b96c7 --- /dev/null +++ b/idea/testData/refactoring/copy/copyWithImportInsertion/before/bar/Bar.kt @@ -0,0 +1,3 @@ +package bar + +fun Any.bar() {} \ No newline at end of file diff --git a/idea/testData/refactoring/copy/copyWithImportInsertion/before/foo/Foo.kt b/idea/testData/refactoring/copy/copyWithImportInsertion/before/foo/Foo.kt new file mode 100644 index 00000000000..dc9dac17444 --- /dev/null +++ b/idea/testData/refactoring/copy/copyWithImportInsertion/before/foo/Foo.kt @@ -0,0 +1,12 @@ +package foo + +import bar.bar + +class Foo { + init { + 42.bar() + } +} + +class Baz { +} diff --git a/idea/testData/refactoring/copy/copyWithImportInsertion/copyWithImportInsertion.test b/idea/testData/refactoring/copy/copyWithImportInsertion/copyWithImportInsertion.test new file mode 100644 index 00000000000..f1eb9731791 --- /dev/null +++ b/idea/testData/refactoring/copy/copyWithImportInsertion/copyWithImportInsertion.test @@ -0,0 +1,5 @@ +{ + "mainFile": "foo/Foo.kt", + "targetPackage": "foo", + "newName": "Foo2" +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java index 2c18659e721..778095c8a00 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/copy/CopyTestGenerated.java @@ -198,6 +198,12 @@ public class CopyTestGenerated extends AbstractCopyTest { doTest(fileName); } + @TestMetadata("copyWithImportInsertion/copyWithImportInsertion.test") + public void testCopyWithImportInsertion_CopyWithImportInsertion() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/copyWithImportInsertion/copyWithImportInsertion.test"); + doTest(fileName); + } + @TestMetadata("kt18149/kt18149.test") public void testKt18149_Kt18149() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/copy/kt18149/kt18149.test");