From 25c7dccd3b76d13fa94299f3b937ef0cb70de96f Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 27 Oct 2016 14:03:01 +0200 Subject: [PATCH] Don't consider function names as candidates for "Show expression type" (KT-14384) --- .../idea/codeInsight/KotlinExpressionTypeProvider.kt | 10 +++++++++- idea/testData/codeInsight/expressionType/MethodName.kt | 5 +++++ .../idea/codeInsight/ExpressionTypeTestGenerated.java | 6 ++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 idea/testData/codeInsight/expressionType/MethodName.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt index dae47806fd9..b04712051d4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt @@ -21,12 +21,14 @@ import com.intellij.psi.PsiElement import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.findModuleDescriptor +import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.renderer.RenderingFormat import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.callUtil.getType import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode @@ -51,7 +53,7 @@ class KotlinExpressionTypeProvider : ExpressionTypeProvider() { is KtIfExpression, is KtWhenExpression, is KtTryExpression -> shouldShowStatementType() is KtLoopExpression -> false is KtConstantExpression -> false - else -> getQualifiedExpressionForSelector() == null && parent !is KtCallableReferenceExpression + else -> getQualifiedExpressionForSelector() == null && parent !is KtCallableReferenceExpression && !isFunctionCallee() } private fun KtExpression.shouldShowStatementType(): Boolean { @@ -62,6 +64,12 @@ class KotlinExpressionTypeProvider : ExpressionTypeProvider() { return false } + private fun KtExpression.isFunctionCallee(): Boolean { + val callExpression = parent as? KtCallExpression ?: return false + if (callExpression.calleeExpression != this) return false + return mainReference?.resolve() is KtFunction + } + override fun getInformationHint(element: KtExpression): String { val bindingContext = element.analyze(BodyResolveMode.PARTIAL) diff --git a/idea/testData/codeInsight/expressionType/MethodName.kt b/idea/testData/codeInsight/expressionType/MethodName.kt new file mode 100644 index 00000000000..e6d654462f8 --- /dev/null +++ b/idea/testData/codeInsight/expressionType/MethodName.kt @@ -0,0 +1,5 @@ +fun foo(value: String) { + println(value) +} + +// TYPE: println(value) -> Unit diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java index bebb33fb31a..6c515e37c05 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java @@ -65,6 +65,12 @@ public class ExpressionTypeTestGenerated extends AbstractExpressionTypeTest { doTest(fileName); } + @TestMetadata("MethodName.kt") + public void testMethodName() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/expressionType/MethodName.kt"); + doTest(fileName); + } + @TestMetadata("MethodReference.kt") public void testMethodReference() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/expressionType/MethodReference.kt");