diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt index c9771223421..8b8cf46f38b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt @@ -32,16 +32,20 @@ import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.resolve.frontendService import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector +import org.jetbrains.kotlin.psi.psiUtil.parents import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf import org.jetbrains.kotlin.renderer.ClassifierNamePolicy import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.renderer.RenderingFormat import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.resolve.calls.callUtil.getParameterForArgument +import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.callUtil.getType import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode +import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.expressions.typeInfoFactory.noTypeInfo class KotlinExpressionTypeProvider : ExpressionTypeProvider() { @@ -112,7 +116,8 @@ class KotlinExpressionTypeProvider : ExpressionTypeProvider() { } val expressionTypeInfo = bindingContext[BindingContext.EXPRESSION_TYPE_INFO, element] ?: noTypeInfo(DataFlowInfo.EMPTY) - val expressionType = element.getType(bindingContext) + val expressionType = element.getType(bindingContext) ?: getTypeForArgumentName(element, bindingContext) + val result = expressionType?.let { typeRenderer.renderType(it) } ?: return "Type is unknown" val dataFlowValueFactory = element.getResolutionFacade().frontendService() @@ -132,5 +137,14 @@ class KotlinExpressionTypeProvider : ExpressionTypeProvider() { return result } + private fun getTypeForArgumentName(element: KtExpression, bindingContext: BindingContext): KotlinType? { + val valueArgumentName = (element.parent as? KtValueArgumentName) ?: return null + val argument = valueArgumentName.parent as? KtValueArgument ?: return null + val ktCallExpression = argument.parents.filterIsInstance().firstOrNull() ?: return null + val resolvedCall = ktCallExpression.getResolvedCall(bindingContext) ?: return null + val parameter = resolvedCall.getParameterForArgument(argument) ?: return null + return parameter.type + } + override fun getErrorHint(): String = "No expression found" } diff --git a/idea/testData/codeInsight/expressionType/ArgumentName.kt b/idea/testData/codeInsight/expressionType/ArgumentName.kt new file mode 100644 index 00000000000..561614959bc --- /dev/null +++ b/idea/testData/codeInsight/expressionType/ArgumentName.kt @@ -0,0 +1,7 @@ +fun foo(a: Int) {} + +fun main() { + foo(a = 42) +} + +// TYPE: a -> Int \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java index a5ccdce4cb0..6af9afd3d34 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java @@ -34,6 +34,11 @@ public class ExpressionTypeTestGenerated extends AbstractExpressionTypeTest { runTest("idea/testData/codeInsight/expressionType/AnonymousObject.kt"); } + @TestMetadata("ArgumentName.kt") + public void testArgumentName() throws Exception { + runTest("idea/testData/codeInsight/expressionType/ArgumentName.kt"); + } + @TestMetadata("BlockBodyFunction.kt") public void testBlockBodyFunction() throws Exception { runTest("idea/testData/codeInsight/expressionType/BlockBodyFunction.kt");