Extract Function: Do not extract extension function reference as parameter
This commit is contained in:
+19
-4
@@ -598,6 +598,11 @@ private fun ExtractionData.inferParametersInfo(
|
||||
val hasThisReceiver = thisDescriptor != null
|
||||
val thisExpr = ref.getParent() as? JetThisExpression
|
||||
|
||||
if (hasThisReceiver
|
||||
&& DescriptorToSourceUtilsIde.getAllDeclarations(project, thisDescriptor!!).all { it.isInsideOf(originalElements) }) {
|
||||
continue
|
||||
}
|
||||
|
||||
val referencedClassDescriptor: ClassDescriptor? = (thisDescriptor ?: originalDescriptor).let {
|
||||
when (it) {
|
||||
is ClassDescriptor ->
|
||||
@@ -638,9 +643,6 @@ private fun ExtractionData.inferParametersInfo(
|
||||
?: DEFAULT_PARAMETER_TYPE
|
||||
}
|
||||
|
||||
val parameterTypePredicate =
|
||||
and(pseudocode.getElementValuesRecursively(originalRef).map { getExpectedTypePredicate(it, bindingContext) })
|
||||
|
||||
val parameter = extractedDescriptorToParameter.getOrPut(descriptorToExtract) {
|
||||
val argumentText =
|
||||
if (hasThisReceiver && extractThis) {
|
||||
@@ -661,7 +663,20 @@ private fun ExtractionData.inferParametersInfo(
|
||||
info.originalRefToParameter[originalRef] = parameter
|
||||
|
||||
parameter.addDefaultType(parameterType)
|
||||
parameter.addTypePredicate(parameterTypePredicate)
|
||||
|
||||
if (extractThis && thisExpr == null) {
|
||||
val callElement = resolvedCall!!.getCall().getCallElement()
|
||||
val instruction = pseudocode.getElementValue(callElement)?.createdAt as? InstructionWithReceivers
|
||||
val receiverValue = instruction?.receiverValues?.entrySet()?.singleOrNull { it.getValue() == receiverToExtract }?.getKey()
|
||||
if (receiverValue != null) {
|
||||
parameter.addTypePredicate(getExpectedTypePredicate(receiverValue, bindingContext))
|
||||
}
|
||||
}
|
||||
else {
|
||||
pseudocode.getElementValuesRecursively(originalRef).forEach {
|
||||
parameter.addTypePredicate(getExpectedTypePredicate(it, bindingContext))
|
||||
}
|
||||
}
|
||||
|
||||
info.replacementMap[refInfo.offsetInBody] =
|
||||
if (hasThisReceiver && extractThis) AddPrefixReplacement(parameter) else RenameReplacement(parameter)
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// PARAM_DESCRIPTOR: local final fun Builder.<anonymous>(): kotlin.Unit defined in test.<anonymous>
|
||||
// PARAM_TYPES: Builder
|
||||
|
||||
class Builder()
|
||||
|
||||
fun Builder.build(b: Builder.() -> Unit) {
|
||||
}
|
||||
|
||||
fun Builder.test(b: Builder, param: Int) {
|
||||
build {
|
||||
build {
|
||||
<selection>build {
|
||||
build {
|
||||
}
|
||||
}</selection>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// PARAM_DESCRIPTOR: local final fun Builder.<anonymous>(): kotlin.Unit defined in test.<anonymous>
|
||||
// PARAM_TYPES: Builder
|
||||
|
||||
class Builder()
|
||||
|
||||
fun Builder.build(b: Builder.() -> Unit) {
|
||||
}
|
||||
|
||||
fun Builder.test(b: Builder, param: Int) {
|
||||
build {
|
||||
build {
|
||||
__dummyTestFun__()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun Builder.__dummyTestFun__() {
|
||||
build {
|
||||
build {
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
@@ -1726,6 +1726,12 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
||||
doExtractFunctionTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("extensionLambda.kt")
|
||||
public void testExtensionLambda() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/extensionLambda.kt");
|
||||
doExtractFunctionTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("extensionLambdaAsParameter.kt")
|
||||
public void testExtensionLambdaAsParameter() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/extractFunction/parameters/misc/extensionLambdaAsParameter.kt");
|
||||
|
||||
Reference in New Issue
Block a user