Rename: Fix rename of import alias referring to object member
#KT-18096 Fixed
This commit is contained in:
+5
-2
@@ -45,6 +45,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElementSelector
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.DataClassDescriptorResolver
|
||||
import org.jetbrains.kotlin.resolve.ImportedFromObjectCallableDescriptor
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.getImportableDescriptor
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.expressions.OperatorConventions
|
||||
@@ -241,13 +242,15 @@ class KtSimpleNameReference(expression: KtSimpleNameExpression) : KtSimpleRefere
|
||||
}
|
||||
|
||||
fun getImportAlias(): KtImportAlias? {
|
||||
fun DeclarationDescriptor.unwrap() = if (this is ImportedFromObjectCallableDescriptor<*>) callableFromObject else this
|
||||
|
||||
val element = element
|
||||
val name = element.getReferencedName()
|
||||
val file = element.containingKtFile
|
||||
val importDirective = file.findImportByAlias(name) ?: return null
|
||||
val fqName = importDirective.importedFqName ?: return null
|
||||
val importedDescriptors = file.resolveImportReference(fqName)
|
||||
if (getTargetDescriptors(element.analyze(BodyResolveMode.PARTIAL)).any { it.getImportableDescriptor() in importedDescriptors }) {
|
||||
val importedDescriptors = file.resolveImportReference(fqName).map { it.unwrap() }
|
||||
if (getTargetDescriptors(element.analyze(BodyResolveMode.PARTIAL)).any { it.unwrap().getImportableDescriptor() in importedDescriptors }) {
|
||||
return importDirective.alias
|
||||
}
|
||||
return null
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import Foo.foo as baz
|
||||
|
||||
object Foo {
|
||||
fun foo() {}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
baz()
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
import Foo.foo as bar
|
||||
|
||||
object Foo {
|
||||
fun foo() {}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
/*rename*/bar()
|
||||
}
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"type": "AUTO_DETECT",
|
||||
"mainFile": "test.kt",
|
||||
"newName": "baz"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import Foo.foo as baz
|
||||
|
||||
object Foo {
|
||||
val foo = 1
|
||||
}
|
||||
|
||||
fun test() {
|
||||
val x = baz
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
import Foo.foo as bar
|
||||
|
||||
object Foo {
|
||||
val foo = 1
|
||||
}
|
||||
|
||||
fun test() {
|
||||
val x = /*rename*/bar
|
||||
}
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"type": "AUTO_DETECT",
|
||||
"mainFile": "test.kt",
|
||||
"newName": "baz"
|
||||
}
|
||||
@@ -396,6 +396,18 @@ public class RenameTestGenerated extends AbstractRenameTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectFunctionImportAliasByRef/objectFunctionImportAliasByRef.test")
|
||||
public void testObjectFunctionImportAliasByRef_ObjectFunctionImportAliasByRef() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/objectFunctionImportAliasByRef/objectFunctionImportAliasByRef.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("objectPropertyImportAliasByRef/objectPropertyImportAliasByRef.test")
|
||||
public void testObjectPropertyImportAliasByRef_ObjectPropertyImportAliasByRef() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/objectPropertyImportAliasByRef/objectPropertyImportAliasByRef.test");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("paramererAsReceiverWithNewNameAsSelector/parameterAsReceiverWithNewNameAsSelector.test")
|
||||
public void testParamererAsReceiverWithNewNameAsSelector_ParameterAsReceiverWithNewNameAsSelector() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/rename/paramererAsReceiverWithNewNameAsSelector/parameterAsReceiverWithNewNameAsSelector.test");
|
||||
|
||||
Reference in New Issue
Block a user