From a7084ceb9b6e25c71d1d688df4636154ac6c8464 Mon Sep 17 00:00:00 2001 From: Kirill Rakhman Date: Fri, 21 Jul 2017 16:31:46 +0200 Subject: [PATCH] Fix some type hints related issues (#1204) * Type hints shouldn't appear for negative literals Fixes #KT-18974 * Make type hints work for destructuring declarations Fixes #KT-18444 * Hide type hints for generic constructor calls if type arguments are explicitly specified Fixes #KT-19167 --- .../KotlinInlayParameterHintsProvider.kt | 8 +++----- .../kotlin/idea/parameterInfo/TypeHints.kt | 4 +++- .../idea/parameterInfo/InlayTypeHintsTest.kt | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinInlayParameterHintsProvider.kt b/idea/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinInlayParameterHintsProvider.kt index 1637f6751c8..7c912332565 100644 --- a/idea/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinInlayParameterHintsProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinInlayParameterHintsProvider.kt @@ -28,10 +28,7 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.getReturnTypeReference import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.psi.KtCallExpression -import org.jetbrains.kotlin.psi.KtNamedFunction -import org.jetbrains.kotlin.psi.KtParameter -import org.jetbrains.kotlin.psi.KtProperty +import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument @@ -55,7 +52,8 @@ enum class HintType(desc: String, enabled: Boolean) { } override fun isApplicable(elem: PsiElement): Boolean = (elem is KtProperty && elem.getReturnTypeReference() == null && elem.isLocal) || - (elem is KtParameter && elem.isLoopParameter && elem.typeReference == null) + (elem is KtParameter && elem.isLoopParameter && elem.typeReference == null) || + (elem is KtDestructuringDeclarationEntry) }, FUNCTION_HINT("Show function return type hints", false) { diff --git a/idea/src/org/jetbrains/kotlin/idea/parameterInfo/TypeHints.kt b/idea/src/org/jetbrains/kotlin/idea/parameterInfo/TypeHints.kt index ee8678f506c..91b08239dae 100644 --- a/idea/src/org/jetbrains/kotlin/idea/parameterInfo/TypeHints.kt +++ b/idea/src/org/jetbrains/kotlin/idea/parameterInfo/TypeHints.kt @@ -84,11 +84,13 @@ private fun isUnclearType(type: KotlinType, element: KtCallableDeclaration): Boo if (element is KtProperty) { val initializer = element.initializer ?: return true if (initializer is KtConstantExpression || initializer is KtStringTemplateExpression) return false + if (initializer is KtUnaryExpression && initializer.baseExpression is KtConstantExpression) return false if (initializer is KtCallExpression) { val bindingContext = element.analyze() val resolvedCall = initializer.getResolvedCall(bindingContext) val constructorDescriptor = resolvedCall?.candidateDescriptor as? ConstructorDescriptor - if (constructorDescriptor != null && constructorDescriptor.constructedClass.declaredTypeParameters.isEmpty()) { + if (constructorDescriptor != null && + (constructorDescriptor.constructedClass.declaredTypeParameters.isEmpty() || initializer.typeArgumentList != null)) { return false } } diff --git a/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/InlayTypeHintsTest.kt b/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/InlayTypeHintsTest.kt index 307244549c0..473c2ac92bf 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/InlayTypeHintsTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/parameterInfo/InlayTypeHintsTest.kt @@ -32,6 +32,11 @@ class InlayTypeHintsTest : KotlinLightCodeInsightFixtureTestCase() { check("""fun foo() { val a = listOf("a") }""") } + fun testDestructuringType() { + HintType.LOCAL_VARIABLE_HINT.option.set(true) + check("""fun foo() { val (i, s) = 1 to "" }""") + } + fun testPropertyType() { HintType.PROPERTY_HINT.option.set(true) check("""val a = listOf("a")""") @@ -42,11 +47,26 @@ class InlayTypeHintsTest : KotlinLightCodeInsightFixtureTestCase() { check("""val a = 1""") } + fun testUnaryConstInitializerType() { + HintType.PROPERTY_HINT.option.set(true) + check("""val a = -1; val b = +1""") + } + fun testConstructorWithoutTypeParametersType() { HintType.PROPERTY_HINT.option.set(true) check("""val a = Any()""") } + fun testConstructorWithExplicitTypeParametersType() { + HintType.PROPERTY_HINT.option.set(true) + check("""class Bar; val a = Bar()""") + } + + fun testConstructorWithoutExplicitTypeParametersType() { + HintType.PROPERTY_HINT.option.set(true) + check("""class Bar(val t: T); val a = Bar("")""") + } + fun testLoopParameter() { HintType.LOCAL_VARIABLE_HINT.option.set(true) check("""fun foo() { for (x in listOf("a")) { } }""")