From 032d50bbbfd950c3507956d390491c1bcc15bea4 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 25 Aug 2016 20:58:07 +0200 Subject: [PATCH] handle multi-declarations in "show expression type" --- .../KotlinExpressionTypeProvider.kt | 4 +-- .../expressionType/MultiDeclaration.kt | 7 ++++ .../MultiDeclarationInLambda.kt | 10 ++++++ .../expressionType/MultiDeclarationInLoop.kt | 8 +++++ .../codeInsight/AbstractExpressionTypeTest.kt | 6 ++-- .../ExpressionTypeTestGenerated.java | 36 +++++++++++++++++++ 6 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 idea/testData/codeInsight/expressionType/MultiDeclaration.kt create mode 100644 idea/testData/codeInsight/expressionType/MultiDeclarationInLambda.kt create mode 100644 idea/testData/codeInsight/expressionType/MultiDeclarationInLoop.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt index 7151f5ecd1f..d30321e40f6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt @@ -33,8 +33,8 @@ class KotlinExpressionTypeProvider : ExpressionTypeProvider() { private fun KtExpression.shouldShowType() = when(this) { is KtFunction -> !hasBlockBody() && !hasDeclaredReturnType() - is KtProperty -> true - is KtStatementExpression -> false + is KtProperty, is KtDestructuringDeclarationEntry -> true + is KtStatementExpression, is KtDestructuringDeclaration -> false else -> true } diff --git a/idea/testData/codeInsight/expressionType/MultiDeclaration.kt b/idea/testData/codeInsight/expressionType/MultiDeclaration.kt new file mode 100644 index 00000000000..d6d61cfdac7 --- /dev/null +++ b/idea/testData/codeInsight/expressionType/MultiDeclaration.kt @@ -0,0 +1,7 @@ +data class IntStringPair(val x: Int, val s: String) + +fun f(x: IntStringPair) { + val (first, second) = x +} + +// TYPE: first -> kotlin.Int diff --git a/idea/testData/codeInsight/expressionType/MultiDeclarationInLambda.kt b/idea/testData/codeInsight/expressionType/MultiDeclarationInLambda.kt new file mode 100644 index 00000000000..029875c8120 --- /dev/null +++ b/idea/testData/codeInsight/expressionType/MultiDeclarationInLambda.kt @@ -0,0 +1,10 @@ +data class IntStringPair(val x: Int, val s: String) + +fun f(x: List) { + x.forEach { (first, second) -> + } +} + +// TYPE: first -> Int +// TYPE: { (first, second) -> } -> (IntStringPair) → Unit +// TYPE: x.forEach { (first, second) -> } -> Unit diff --git a/idea/testData/codeInsight/expressionType/MultiDeclarationInLoop.kt b/idea/testData/codeInsight/expressionType/MultiDeclarationInLoop.kt new file mode 100644 index 00000000000..96aab4d842d --- /dev/null +++ b/idea/testData/codeInsight/expressionType/MultiDeclarationInLoop.kt @@ -0,0 +1,8 @@ +data class IntStringPair(val x: Int, val s: String) + +fun f(x: List) { + for ((first, second) in x) { + } +} + +// TYPE: first -> Int diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractExpressionTypeTest.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractExpressionTypeTest.kt index e55f66c98aa..bdafaeb50e4 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractExpressionTypeTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractExpressionTypeTest.kt @@ -24,12 +24,14 @@ import org.jetbrains.kotlin.test.InTextDirectivesUtils abstract class AbstractExpressionTypeTest : KotlinLightCodeInsightFixtureTestCase() { override fun getBasePath() = PluginTestCaseBase.TEST_DATA_PROJECT_RELATIVE + "/codeInsight/expressionType" + override fun getProjectDescriptor() = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE + protected fun doTest(path: String) { myFixture.configureByFile(path) val expressionTypeProvider = KotlinExpressionTypeProvider() val expressions = expressionTypeProvider.getExpressionsAt(myFixture.elementAtCaret) val types = expressions.map { "${it.text.replace('\n', ' ')} -> ${expressionTypeProvider.getInformationHint(it)}" } - val expectedTypes = InTextDirectivesUtils.findListWithPrefixes(myFixture.file.text, "// TYPE: ") - UsefulTestCase.assertSameElements(types, expectedTypes) + val expectedTypes = InTextDirectivesUtils.findLinesWithPrefixesRemoved(myFixture.file.text, "// TYPE: ") + UsefulTestCase.assertOrderedEquals(types, expectedTypes) } } diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java index d460199814e..9b47b967f73 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java @@ -41,6 +41,42 @@ public class ExpressionTypeTestGenerated extends AbstractExpressionTypeTest { doTest(fileName); } + @TestMetadata("MultiDeclaration.kt") + public void testMultiDeclaration() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/expressionType/MultiDeclaration.kt"); + doTest(fileName); + } + + @TestMetadata("MultiDeclarationInLambda.kt") + public void testMultiDeclarationInLambda() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/expressionType/MultiDeclarationInLambda.kt"); + doTest(fileName); + } + + @TestMetadata("MultiDeclarationInLoop.kt") + public void testMultiDeclarationInLoop() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/expressionType/MultiDeclarationInLoop.kt"); + doTest(fileName); + } + + @TestMetadata("PropertyAccessor.kt") + public void testPropertyAccessor() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/expressionType/PropertyAccessor.kt"); + doTest(fileName); + } + + @TestMetadata("SmartCast.kt") + public void testSmartCast() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/expressionType/SmartCast.kt"); + doTest(fileName); + } + + @TestMetadata("SoftSmartCast.kt") + public void testSoftSmartCast() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/expressionType/SoftSmartCast.kt"); + doTest(fileName); + } + @TestMetadata("VariableDeclaration.kt") public void testVariableDeclaration() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/expressionType/VariableDeclaration.kt");