Skip to content

Commit ecdba7d

Browse files
committed
Show more correct type hints
- Don't show hints containing error type as type parameter - Do not expand type aliases - Show base type for types of anonymous objects #KT-18369 Fixed #KT-18341 Fixed #KT-18343 Fixed (cherry picked from commit 8077a71)
1 parent 4d21631 commit ecdba7d

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

idea/src/org/jetbrains/kotlin/idea/parameterInfo/TypeHints.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,21 @@ import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
2323
import org.jetbrains.kotlin.idea.caches.resolve.analyze
2424
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
2525
import org.jetbrains.kotlin.idea.intentions.SpecifyTypeExplicitlyIntention
26+
import org.jetbrains.kotlin.name.SpecialNames
2627
import org.jetbrains.kotlin.psi.*
2728
import org.jetbrains.kotlin.psi.psiUtil.endOffset
2829
import org.jetbrains.kotlin.renderer.DescriptorRenderer
2930
import org.jetbrains.kotlin.renderer.RenderingFormat
3031
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
3132
import org.jetbrains.kotlin.types.KotlinType
32-
import org.jetbrains.kotlin.types.isError
33+
import org.jetbrains.kotlin.types.typeUtil.containsError
34+
import org.jetbrains.kotlin.types.typeUtil.immediateSupertypes
3335

3436
//hack to separate type presentation from param info presentation
3537
const val TYPE_INFO_PREFIX = "@TYPE@"
3638
private val typeRenderer = DescriptorRenderer.COMPACT_WITH_SHORT_TYPES.withOptions {
3739
textFormat = RenderingFormat.PLAIN
40+
renderUnabbreviatedType = false
3841
}
3942

4043
fun providePropertyTypeHint(elem: PsiElement): List<InlayInfo> {
@@ -47,8 +50,17 @@ fun providePropertyTypeHint(elem: PsiElement): List<InlayInfo> {
4750
}
4851

4952
fun provideTypeHint(element: KtCallableDeclaration, offset: Int): List<InlayInfo> {
50-
val type = SpecifyTypeExplicitlyIntention.getTypeForDeclaration(element)
51-
return if (!type.isError && isUnclearType(type, element)) {
53+
var type: KotlinType = SpecifyTypeExplicitlyIntention.getTypeForDeclaration(element).unwrap()
54+
if (type.containsError()) return emptyList()
55+
val name = type.constructor.declarationDescriptor?.name
56+
if (name == SpecialNames.NO_NAME_PROVIDED) {
57+
type = type.immediateSupertypes().singleOrNull() ?: return emptyList()
58+
}
59+
else if (name?.isSpecial == true) {
60+
return emptyList()
61+
}
62+
63+
return if (isUnclearType(type, element)) {
5264
val settings = CodeStyleSettingsManager.getInstance(element.project).currentSettings
5365
.getCustomSettings(KotlinCodeStyleSettings::class.java)
5466

idea/tests/org/jetbrains/kotlin/idea/parameterInfo/InlayTypeHintsTest.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,24 @@ class InlayTypeHintsTest : KotlinLightCodeInsightFixtureTestCase() {
5656
HintType.LOCAL_VARIABLE_HINT.option.set(true)
5757
check("""fun foo() { for (x: String in listOf("a")) { } }""")
5858
}
59-
}
59+
60+
fun testErrorType() {
61+
HintType.PROPERTY_HINT.option.set(true)
62+
check("""val x = arrayListOf<>()""")
63+
}
64+
65+
fun testExpandedTypeAlias() {
66+
HintType.PROPERTY_HINT.option.set(true)
67+
check("""val x<hint text=": ArrayList<Int>" /> = arrayListOf(1)""")
68+
}
69+
70+
fun testAnonymousObject() {
71+
HintType.FUNCTION_HINT.option.set(true)
72+
check("""val o = object : Iterable<Int> {
73+
override fun iterator()<hint text=": Iterator<Int>" /> = object : Iterator<Int> {
74+
override fun next()<hint text=": Int" /> = 1
75+
override fun hasNext()<hint text=": Boolean" /> = true
76+
}
77+
}""")
78+
}
79+
}

0 commit comments

Comments
 (0)