Extract Function: Fix type candidates for parameters with flexibly nullable types
#KT-6546 Fixed
This commit is contained in:
+22
-2
@@ -69,6 +69,8 @@ import org.jetbrains.jet.plugin.caches.resolve.findModuleDescriptor
|
|||||||
import org.jetbrains.jet.plugin.caches.resolve.analyze
|
import org.jetbrains.jet.plugin.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
import org.jetbrains.jet.plugin.refactoring.comparePossiblyOverridingDescriptors
|
import org.jetbrains.jet.plugin.refactoring.comparePossiblyOverridingDescriptors
|
||||||
|
import org.jetbrains.kotlin.plugin.util.makeNullable
|
||||||
|
import org.jetbrains.kotlin.plugin.util.makeNotNullable
|
||||||
|
|
||||||
private val DEFAULT_FUNCTION_NAME = "myFun"
|
private val DEFAULT_FUNCTION_NAME = "myFun"
|
||||||
private val DEFAULT_RETURN_TYPE = KotlinBuiltIns.getInstance().getUnitType()
|
private val DEFAULT_RETURN_TYPE = KotlinBuiltIns.getInstance().getUnitType()
|
||||||
@@ -473,8 +475,26 @@ private class MutableParameter(
|
|||||||
|
|
||||||
override val parameterTypeCandidates: List<JetType> by Delegates.lazy {
|
override val parameterTypeCandidates: List<JetType> by Delegates.lazy {
|
||||||
writable = false
|
writable = false
|
||||||
val superTypes = TypeUtils.getAllSupertypes(defaultType).filter(and(typePredicates))
|
|
||||||
(Collections.singletonList(defaultType) + superTypes).filter { it.isExtractable() }
|
val typePredicate = and(typePredicates)
|
||||||
|
|
||||||
|
val typeList = if (defaultType.isNullabilityFlexible()) {
|
||||||
|
val bounds = defaultType.getCapability(javaClass<Flexibility>())
|
||||||
|
if (typePredicate(bounds.upperBound)) arrayListOf(bounds.upperBound, bounds.lowerBound) else arrayListOf(bounds.lowerBound)
|
||||||
|
}
|
||||||
|
else arrayListOf(defaultType)
|
||||||
|
|
||||||
|
val addNullableTypes = typeList.size() > 1
|
||||||
|
val superTypes = TypeUtils.getAllSupertypes(defaultType).filter(typePredicate)
|
||||||
|
|
||||||
|
for (superType in superTypes) {
|
||||||
|
if (addNullableTypes) {
|
||||||
|
typeList.add(superType.makeNullable())
|
||||||
|
}
|
||||||
|
typeList.add(superType)
|
||||||
|
}
|
||||||
|
|
||||||
|
typeList.filter { it.isExtractable() }
|
||||||
}
|
}
|
||||||
|
|
||||||
override val parameterType: JetType by Delegates.lazy { parameterTypeCandidates.firstOrNull() ?: defaultType }
|
override val parameterType: JetType by Delegates.lazy { parameterTypeCandidates.firstOrNull() ?: defaultType }
|
||||||
|
|||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// PARAM_TYPES: String?, String, kotlin.CharSequence?, CharSequence
|
||||||
|
// PARAM_DESCRIPTOR: val property: (String..String?) defined in test
|
||||||
|
fun test() {
|
||||||
|
val property = System.getProperty("some")
|
||||||
|
val n = <selection>property?.length()</selection>
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// PARAM_TYPES: String?, String, kotlin.CharSequence?, CharSequence
|
||||||
|
// PARAM_DESCRIPTOR: val property: (String..String?) defined in test
|
||||||
|
fun test() {
|
||||||
|
val property = System.getProperty("some")
|
||||||
|
val n = i(property)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun i(property: String?): Int? {
|
||||||
|
return property?.length()
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// PARAM_TYPES: String, CharSequence
|
||||||
|
// PARAM_DESCRIPTOR: val property: (String..String?) defined in test
|
||||||
|
fun test() {
|
||||||
|
val property = System.getProperty("some")
|
||||||
|
val n = <selection>property.length()</selection>
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// PARAM_TYPES: String, CharSequence
|
||||||
|
// PARAM_DESCRIPTOR: val property: (String..String?) defined in test
|
||||||
|
fun test() {
|
||||||
|
val property = System.getProperty("some")
|
||||||
|
val n = i(property)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun i(property: String): Int {
|
||||||
|
return property.length()
|
||||||
|
}
|
||||||
+12
@@ -1421,6 +1421,18 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
|||||||
doExtractFunctionTest(fileName);
|
doExtractFunctionTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("flexibleTypesWithNull.kt")
|
||||||
|
public void testFlexibleTypesWithNull() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/candidateTypes/flexibleTypesWithNull.kt");
|
||||||
|
doExtractFunctionTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("flexibleTypesWithoutNull.kt")
|
||||||
|
public void testFlexibleTypesWithoutNull() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/candidateTypes/flexibleTypesWithoutNull.kt");
|
||||||
|
doExtractFunctionTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("liftAnonymousToSupertype1.kt")
|
@TestMetadata("liftAnonymousToSupertype1.kt")
|
||||||
public void testLiftAnonymousToSupertype1() throws Exception {
|
public void testLiftAnonymousToSupertype1() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/candidateTypes/liftAnonymousToSupertype1.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/candidateTypes/liftAnonymousToSupertype1.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user