From c0162cd8fd2c6ce0eeb3645fdd7052c574054146 Mon Sep 17 00:00:00 2001 From: Bart van Helvert Date: Sat, 9 Dec 2023 17:17:04 +0100 Subject: [PATCH] [kotlin] Fix bind to element when binding type to shorter fqn GitOrigin-RevId: c839c17d9ed0dc33fb25e86e81d0199e77e1073b --- .../FullyQualifiedWithLongerFqn.kt | 19 +++++++++++++++++++ .../FullyQualifiedWithLongerFqn.kt.after | 10 ++++++++++ .../FullyQualifiedWithShorterFqn.kt | 19 +++++++++++++++++++ .../FullyQualifiedWithShorterFqn.kt.after | 10 ++++++++++ .../refactoring/K2ReferenceMutateService.kt | 7 ++++++- .../K2BindToElementTestGenerated.java | 10 ++++++++++ 6 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithLongerFqn.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithLongerFqn.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithShorterFqn.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithShorterFqn.kt.after diff --git a/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithLongerFqn.kt b/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithLongerFqn.kt new file mode 100644 index 000000000000..1ada182e1e0a --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithLongerFqn.kt @@ -0,0 +1,19 @@ +// FILE: test/bar/Foo.kt +// BIND_TO test.foo.bar.B +package test.bar + +import test.foo.bar.B + +fun foo() { + val x: test.foo.A = B() +} + +// FILE: test/foo/A.kt +package test.foo + +interface A { } + +// FILE: test/foo/bar/B.kt +package test.foo.bar + +class B : A { } \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithLongerFqn.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithLongerFqn.kt.after new file mode 100644 index 000000000000..d8d639269514 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithLongerFqn.kt.after @@ -0,0 +1,10 @@ +// FILE: test/bar/Foo.kt +// BIND_TO test.foo.bar.B +package test.bar + +import test.foo.bar.B + +fun foo() { + val x: B = B() +} + diff --git a/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithShorterFqn.kt b/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithShorterFqn.kt new file mode 100644 index 000000000000..06753e32f3d9 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithShorterFqn.kt @@ -0,0 +1,19 @@ +// FILE: test/bar/Foo.kt +// BIND_TO test.B +package test.bar + +import test.B + +fun foo() { + val x: test.foo.A = B() +} + +// FILE: test/foo/A.kt +package test.foo + +interface A { } + +// FILE: test/B.kt +package test + +class B : A { } \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithShorterFqn.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithShorterFqn.kt.after new file mode 100644 index 000000000000..9bb3c2066eb4 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithShorterFqn.kt.after @@ -0,0 +1,10 @@ +// FILE: test/bar/Foo.kt +// BIND_TO test.B +package test.bar + +import test.B + +fun foo() { + val x: B = B() +} + diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/K2ReferenceMutateService.kt b/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/K2ReferenceMutateService.kt index 31b8452407dc..fa54267da4c9 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/K2ReferenceMutateService.kt +++ b/plugins/kotlin/refactorings/kotlin.refactorings.k2/src/org/jetbrains/kotlin/idea/k2/refactoring/K2ReferenceMutateService.kt @@ -117,7 +117,12 @@ internal class K2ReferenceMutateService : KtReferenceMutateServiceBase() { ?: error("Could not create type from $fqName") replaced(newReference) } else { - if (!fqName.isRoot) qualifier?.replaceWith(fqName.parent()) // do recursive short name replacement to preserve type arguments + val parentFqn = fqName.parent() + if (parentFqn.isRoot) { + deleteQualifier() + } else { + qualifier?.replaceWith(parentFqn) // do recursive short name replacement to preserve type arguments + } referenceExpression?.replaceShortName(fqName)?.parent as KtUserType } } diff --git a/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/bindToElement/K2BindToElementTestGenerated.java b/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/bindToElement/K2BindToElementTestGenerated.java index 1df5ec53405e..7eed0cc9483e 100644 --- a/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/bindToElement/K2BindToElementTestGenerated.java +++ b/plugins/kotlin/refactorings/kotlin.refactorings.tests.k2/test/org/jetbrains/kotlin/idea/k2/refactoring/bindToElement/K2BindToElementTestGenerated.java @@ -279,6 +279,16 @@ public abstract class K2BindToElementTestGenerated extends AbstractK2BindToEleme runTest("../../idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualified.kt"); } + @TestMetadata("FullyQualifiedWithLongerFqn.kt") + public void testFullyQualifiedWithLongerFqn() throws Exception { + runTest("../../idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithLongerFqn.kt"); + } + + @TestMetadata("FullyQualifiedWithShorterFqn.kt") + public void testFullyQualifiedWithShorterFqn() throws Exception { + runTest("../../idea/tests/testData/refactoring/bindToElement/propertyTypeReference/FullyQualifiedWithShorterFqn.kt"); + } + @TestMetadata("RootPkg.kt") public void testRootPkg() throws Exception { runTest("../../idea/tests/testData/refactoring/bindToElement/propertyTypeReference/RootPkg.kt");