Extract Function: Add support of property-as-function calls

#KT-5179 Fixed
This commit is contained in:
Alexey Sedunov
2014-06-19 16:17:48 +04:00
parent 6bc3772d29
commit c7d8ab8219
5 changed files with 35 additions and 10 deletions
@@ -45,6 +45,7 @@ import org.jetbrains.jet.lang.psi.psiUtil.getParentByType
import org.jetbrains.jet.lang.psi.JetDeclaration
import org.jetbrains.jet.lang.psi.JetDeclarationWithBody
import org.jetbrains.jet.lang.psi.JetUserType
import org.jetbrains.jet.lang.resolve.calls.model.VariableAsFunctionResolvedCall
data class ExtractionOptions(val inferUnitTypeForUnusedValues: Boolean) {
class object {
@@ -102,7 +103,9 @@ class ExtractionData(
if (ref !is JetSimpleNameExpression) continue
val resolvedCallKey = (ref.getParent() as? JetThisExpression) ?: ref
val resolvedCall = context[BindingContext.RESOLVED_CALL, resolvedCallKey]
val resolvedCall = context[BindingContext.RESOLVED_CALL, resolvedCallKey]?.let {
(it as? VariableAsFunctionResolvedCall)?.functionCall ?: it
}
val descriptor = context[BindingContext.REFERENCE_TARGET, ref]
if (descriptor == null) continue
@@ -379,15 +379,10 @@ private fun ExtractionData.inferParametersInfo(
val extractParameter = extractThis || extractLocalVar
if (extractParameter) {
val parameterType =
if (hasThisReceiver) {
when (descriptorToExtract) {
is ClassDescriptor -> descriptorToExtract.getDefaultType()
is CallableDescriptor -> descriptorToExtract.getReceiverParameter()?.getType()
else -> null
} ?: DEFAULT_PARAMETER_TYPE
}
else bindingContext[BindingContext.EXPRESSION_TYPE, originalRef] ?: DEFAULT_PARAMETER_TYPE
val parameterType = when {
receiver.exists() -> receiver.getType()
else -> bindingContext[BindingContext.EXPRESSION_TYPE, originalRef] ?: DEFAULT_PARAMETER_TYPE
}
if (!parameterType.processTypeIfExtractable(bindingContext, typeParameters, nonDenotableTypes)) continue
@@ -0,0 +1,9 @@
// PARAM_TYPES: A
class A {
fun invoke() = 20
}
// SIBLING:
fun testProp() {
val foo = A()
<selection>foo()</selection>
}
@@ -0,0 +1,13 @@
// PARAM_TYPES: A
class A {
fun invoke() = 20
}
// SIBLING:
fun testProp() {
val foo = A()
unit(foo)
}
fun unit(foo: A) {
foo()
}
@@ -794,6 +794,11 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/parameters/misc/usagesInCallArgs.kt");
}
@TestMetadata("variableAsFunction.kt")
public void testVariableAsFunction() throws Exception {
doExtractFunctionTest("idea/testData/refactoring/extractFunction/parameters/misc/variableAsFunction.kt");
}
}
@TestMetadata("idea/testData/refactoring/extractFunction/parameters/nonDenotableTypes")