[kotlin] Fix bind to element when binding type to shorter fqn

GitOrigin-RevId: c839c17d9ed0dc33fb25e86e81d0199e77e1073b
This commit is contained in:
Bart van Helvert
2023-12-10 00:32:55 +00:00
committed by intellij-monorepo-bot
parent 2e5cc82959
commit c0162cd8fd
6 changed files with 74 additions and 1 deletions
@@ -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.<caret>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 { }
@@ -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()
}
@@ -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.<caret>A = B()
}
// FILE: test/foo/A.kt
package test.foo
interface A { }
// FILE: test/B.kt
package test
class B : A { }
@@ -0,0 +1,10 @@
// FILE: test/bar/Foo.kt
// BIND_TO test.B
package test.bar
import test.B
fun foo() {
val x: <caret>B = B()
}
@@ -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
}
}
@@ -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");