Extract Function: Fix replacement of references with class receivers (objects, class objects, enum classes)
This commit is contained in:
+14
-7
@@ -331,14 +331,21 @@ private fun ExtractionData.inferParametersInfo(
|
||||
val hasThisReceiver = thisDescriptor != null
|
||||
val thisExpr = ref.getParent() as? JetThisExpression
|
||||
|
||||
val hasClassObjectReceiver = (thisDescriptor as? ClassDescriptor)?.getKind() == ClassKind.CLASS_OBJECT
|
||||
val classObjectClassDescriptor =
|
||||
if (hasClassObjectReceiver) thisDescriptor!!.getContainingDeclaration() as? ClassDescriptor else null
|
||||
val typeDescriptor = ((thisDescriptor ?: originalDescriptor) as? ClassDescriptor)?.let {
|
||||
when(it.getKind()) {
|
||||
ClassKind.OBJECT, ClassKind.ENUM_CLASS -> it
|
||||
ClassKind.CLASS_OBJECT, ClassKind.ENUM_ENTRY -> it.getContainingDeclaration() as? ClassDescriptor
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
if (classObjectClassDescriptor != null) {
|
||||
assert (classObjectClassDescriptor.canBeReferencedViaImport(), "Class object should be allowed only for importable classes: className = ${classObjectClassDescriptor.getName().asString()}")
|
||||
|
||||
replacementMap[refInfo.offsetInBody] = FqNameReplacement(DescriptorUtils.getFqNameSafe(originalDescriptor))
|
||||
if (typeDescriptor != null) {
|
||||
if (typeDescriptor.canBeReferencedViaImport()) {
|
||||
replacementMap[refInfo.offsetInBody] = FqNameReplacement(DescriptorUtils.getFqNameSafe(originalDescriptor))
|
||||
}
|
||||
else {
|
||||
nonDenotableTypes.add(typeDescriptor.getDefaultType())
|
||||
}
|
||||
}
|
||||
else {
|
||||
val extractThis = hasThisReceiver || thisExpr != null
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// SIBLING:
|
||||
class MyClass {
|
||||
fun test() {
|
||||
<selection>P.A.foo()
|
||||
P.A.a</selection>
|
||||
}
|
||||
|
||||
enum class P {
|
||||
A
|
||||
|
||||
val a = 1
|
||||
fun foo() = 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// SIBLING:
|
||||
class MyClass {
|
||||
fun test() {
|
||||
unit()
|
||||
}
|
||||
|
||||
enum class P {
|
||||
A
|
||||
|
||||
val a = 1
|
||||
fun foo() = 1
|
||||
}
|
||||
}
|
||||
|
||||
fun unit() {
|
||||
MyClass.P.A.foo()
|
||||
MyClass.P.A.a
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// SIBLING:
|
||||
class MyClass {
|
||||
fun test() {
|
||||
<selection>P.foo()
|
||||
P.a</selection>
|
||||
}
|
||||
|
||||
object P {
|
||||
val a = 1
|
||||
fun foo() = 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// SIBLING:
|
||||
class MyClass {
|
||||
fun test() {
|
||||
unit()
|
||||
}
|
||||
|
||||
object P {
|
||||
val a = 1
|
||||
fun foo() = 1
|
||||
}
|
||||
}
|
||||
|
||||
fun unit() {
|
||||
MyClass.P.foo()
|
||||
MyClass.P.a
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package p.q
|
||||
|
||||
val a = 1
|
||||
fun foo() = 1
|
||||
|
||||
// SIBLING:
|
||||
class MyClass {
|
||||
fun test() {
|
||||
<selection>p.q.foo()
|
||||
p.q.a</selection>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package p.q
|
||||
|
||||
val a = 1
|
||||
fun foo() = 1
|
||||
|
||||
// SIBLING:
|
||||
class MyClass {
|
||||
fun test() {
|
||||
unit()
|
||||
}
|
||||
}
|
||||
|
||||
fun unit() {
|
||||
foo()
|
||||
a
|
||||
}
|
||||
+15
@@ -744,6 +744,21 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
||||
doExtractFunctionTest("idea/testData/refactoring/extractFunction/parameters/misc/qualifiedClassObject.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("qualifiedEnum.kt")
|
||||
public void testQualifiedEnum() throws Exception {
|
||||
doExtractFunctionTest("idea/testData/refactoring/extractFunction/parameters/misc/qualifiedEnum.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("qualifiedObject.kt")
|
||||
public void testQualifiedObject() throws Exception {
|
||||
doExtractFunctionTest("idea/testData/refactoring/extractFunction/parameters/misc/qualifiedObject.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("qualifiedPackage.kt")
|
||||
public void testQualifiedPackage() throws Exception {
|
||||
doExtractFunctionTest("idea/testData/refactoring/extractFunction/parameters/misc/qualifiedPackage.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("usagesInCallArgs.kt")
|
||||
public void testUsagesInCallArgs() throws Exception {
|
||||
doExtractFunctionTest("idea/testData/refactoring/extractFunction/parameters/misc/usagesInCallArgs.kt");
|
||||
|
||||
Reference in New Issue
Block a user