From 7b644e2c52deae60f97efa8d76461983be13c5ac Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 25 Aug 2016 20:40:14 +0200 Subject: [PATCH] support "show expression type" in variable declaration position #KT-10095 Fixed --- .../kotlin/generators/tests/GenerateTests.kt | 4 ++ .../KotlinExpressionTypeProvider.kt | 7 +-- .../expressionType/VariableDeclaration.kt | 3 ++ .../codeInsight/AbstractExpressionTypeTest.kt | 35 +++++++++++++++ .../ExpressionTypeTestGenerated.java | 43 +++++++++++++++++++ 5 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 idea/testData/codeInsight/expressionType/VariableDeclaration.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractExpressionTypeTest.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 79c37f5d784..f24d1950ea1 100755 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -581,6 +581,10 @@ fun main(args: Array) { model("codeInsight/unwrapAndRemove/unwrapLambda", testMethod = "doTestLambdaUnwrapper") } + testClass() { + model("codeInsight/expressionType") + } + testClass() { model("editor/backspaceHandler") } diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt index 19ac19c991b..109acc0bc8a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/KotlinExpressionTypeProvider.kt @@ -20,10 +20,7 @@ import com.intellij.lang.ExpressionTypeProvider import com.intellij.psi.PsiElement import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze -import org.jetbrains.kotlin.psi.KtCallableDeclaration -import org.jetbrains.kotlin.psi.KtExpression -import org.jetbrains.kotlin.psi.KtFunction -import org.jetbrains.kotlin.psi.KtStatementExpression +import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.resolve.BindingContext @@ -35,7 +32,7 @@ class KotlinExpressionTypeProvider : ExpressionTypeProvider() { elementAt.parentsWithSelf.filterIsInstance().filterNot { it.shouldSkip() }.toList() private fun KtExpression.shouldSkip(): Boolean { - return this is KtStatementExpression && this !is KtFunction + return this is KtStatementExpression && this !is KtFunction && this !is KtProperty } override fun getInformationHint(element: KtExpression): String { diff --git a/idea/testData/codeInsight/expressionType/VariableDeclaration.kt b/idea/testData/codeInsight/expressionType/VariableDeclaration.kt new file mode 100644 index 00000000000..5172daedd64 --- /dev/null +++ b/idea/testData/codeInsight/expressionType/VariableDeclaration.kt @@ -0,0 +1,3 @@ +val x = 1 + +// TYPE: val x = 1 -> kotlin.Int diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractExpressionTypeTest.kt b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractExpressionTypeTest.kt new file mode 100644 index 00000000000..e3ae661aa06 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/AbstractExpressionTypeTest.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.codeInsight + +import com.intellij.testFramework.UsefulTestCase +import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase +import org.jetbrains.kotlin.idea.test.PluginTestCaseBase +import org.jetbrains.kotlin.test.InTextDirectivesUtils + +abstract class AbstractExpressionTypeTest : KotlinLightCodeInsightFixtureTestCase() { + override fun getBasePath() = PluginTestCaseBase.TEST_DATA_PROJECT_RELATIVE + "/codeInsight/expressionType" + + protected fun doTest(path: String) { + myFixture.configureByFile(path) + val expressionTypeProvider = KotlinExpressionTypeProvider() + val expressions = expressionTypeProvider.getExpressionsAt(myFixture.elementAtCaret) + val types = expressions.map { "${it.text} -> ${expressionTypeProvider.getInformationHint(it)}" } + val expectedTypes = InTextDirectivesUtils.findListWithPrefixes(myFixture.file.text, "// TYPE: ") + UsefulTestCase.assertSameElements(types, expectedTypes) + } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java new file mode 100644 index 00000000000..432b1a1867c --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/ExpressionTypeTestGenerated.java @@ -0,0 +1,43 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.codeInsight; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.KotlinTestUtils; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("idea/testData/codeInsight/expressionType") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class ExpressionTypeTestGenerated extends AbstractExpressionTypeTest { + public void testAllFilesPresentInExpressionType() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/expressionType"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("VariableDeclaration.kt") + public void testVariableDeclaration() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/expressionType/VariableDeclaration.kt"); + doTest(fileName); + } +}