Initial tests for type hints; don't show hint when type is obvious
This commit is contained in:
@@ -19,9 +19,11 @@ package org.jetbrains.kotlin.idea.parameterInfo
|
||||
import com.intellij.codeInsight.hints.InlayInfo
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager
|
||||
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
|
||||
import org.jetbrains.kotlin.idea.intentions.SpecifyTypeExplicitlyIntention
|
||||
import org.jetbrains.kotlin.psi.KtCallableDeclaration
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer
|
||||
import org.jetbrains.kotlin.renderer.RenderingFormat
|
||||
@@ -46,7 +48,7 @@ fun providePropertyTypeHint(elem: PsiElement): List<InlayInfo> {
|
||||
|
||||
fun provideTypeHint(element: KtCallableDeclaration, offset: Int): List<InlayInfo> {
|
||||
val type = SpecifyTypeExplicitlyIntention.getTypeForDeclaration(element)
|
||||
return if (!type.isError) {
|
||||
return if (!type.isError && isUnclearType(type, element)) {
|
||||
val settings = CodeStyleSettingsManager.getInstance(element.project).currentSettings
|
||||
.getCustomSettings(KotlinCodeStyleSettings::class.java)
|
||||
|
||||
@@ -66,3 +68,18 @@ fun provideTypeHint(element: KtCallableDeclaration, offset: Int): List<InlayInfo
|
||||
}
|
||||
}
|
||||
|
||||
private fun isUnclearType(type: KotlinType, element: KtCallableDeclaration): Boolean {
|
||||
if (element is KtProperty) {
|
||||
val initializer = element.initializer ?: return true
|
||||
if (initializer is KtConstantExpression || initializer is KtStringTemplateExpression) 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()) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.parameterInfo
|
||||
|
||||
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
|
||||
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
|
||||
|
||||
class InlayTypeHintsTest : KotlinLightCodeInsightFixtureTestCase() {
|
||||
override fun getProjectDescriptor() = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
|
||||
|
||||
fun check(text: String) {
|
||||
myFixture.configureByText("A.kt", text)
|
||||
myFixture.testInlays()
|
||||
}
|
||||
|
||||
fun testLocalVariableType() {
|
||||
HintType.LOCAL_VARIABLE_HINT.option.set(true)
|
||||
check("""fun foo() { val a<hint text=": List<String>" /> = listOf("a") }""")
|
||||
}
|
||||
|
||||
fun testPropertyType() {
|
||||
HintType.PROPERTY_HINT.option.set(true)
|
||||
check("""val a<hint text=": List<String>" /> = listOf("a")""")
|
||||
}
|
||||
|
||||
fun testConstInitializerType() {
|
||||
HintType.PROPERTY_HINT.option.set(true)
|
||||
check("""val a = 1""")
|
||||
}
|
||||
|
||||
fun testConstructorWithoutTypeParametersType() {
|
||||
HintType.PROPERTY_HINT.option.set(true)
|
||||
check("""val a = Any()""")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user