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
This commit is contained in:
Kirill Rakhman
2017-07-21 16:31:46 +02:00
committed by Dmitry Jemerov
parent f4038f7109
commit a7084ceb9b
3 changed files with 26 additions and 6 deletions
@@ -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) {
@@ -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
}
}
@@ -32,6 +32,11 @@ class InlayTypeHintsTest : KotlinLightCodeInsightFixtureTestCase() {
check("""fun foo() { val a<hint text=": List<String>" /> = listOf("a") }""")
}
fun testDestructuringType() {
HintType.LOCAL_VARIABLE_HINT.option.set(true)
check("""fun foo() { val (i<hint text=": Int" />, s<hint text=": String" />) = 1 to "" }""")
}
fun testPropertyType() {
HintType.PROPERTY_HINT.option.set(true)
check("""val a<hint text=": List<String>" /> = 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<T>; val a = Bar<String>()""")
}
fun testConstructorWithoutExplicitTypeParametersType() {
HintType.PROPERTY_HINT.option.set(true)
check("""class Bar<T>(val t: T); val a<hint text=": Bar<String>" /> = Bar(<hint text="t:" />"")""")
}
fun testLoopParameter() {
HintType.LOCAL_VARIABLE_HINT.option.set(true)
check("""fun foo() { for (x<hint text=": String" /> in listOf("a")) { } }""")