Copy: Fix import insertion on declaration copying

#KT-19909 Fixed
This commit is contained in:
Alexey Sedunov
2017-09-19 16:37:09 +03:00
parent 0beadddb7f
commit c8c0ab1647
8 changed files with 52 additions and 1 deletions
@@ -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<KtNamedDeclaration>().singleOrNull())?.let { newDeclaration ->
if (newName == newDeclaration.name) return@let
val selfReferences = ReferencesSearch.search(newDeclaration, LocalSearchScope(newDeclaration)).findAll()
runWriteAction {
@@ -0,0 +1,3 @@
package bar
fun Any.bar() {}
@@ -0,0 +1,12 @@
package foo
import bar.bar
class Foo {
init {
42.bar()
}
}
class Baz {
}
@@ -0,0 +1,9 @@
package foo
import bar.bar
class Foo2 {
init {
42.bar()
}
}
@@ -0,0 +1,3 @@
package bar
fun Any.bar() {}
@@ -0,0 +1,12 @@
package foo
import bar.bar
class <caret>Foo {
init {
42.bar()
}
}
class Baz {
}
@@ -0,0 +1,5 @@
{
"mainFile": "foo/Foo.kt",
"targetPackage": "foo",
"newName": "Foo2"
}
@@ -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");