diff --git a/idea/src/org/jetbrains/kotlin/idea/parameterInfo/LambdaImpicitHints.kt b/idea/src/org/jetbrains/kotlin/idea/parameterInfo/LambdaImpicitHints.kt index 8e68329c6fc..5686b9be2ff 100644 --- a/idea/src/org/jetbrains/kotlin/idea/parameterInfo/LambdaImpicitHints.kt +++ b/idea/src/org/jetbrains/kotlin/idea/parameterInfo/LambdaImpicitHints.kt @@ -29,7 +29,7 @@ fun provideLambdaImplicitHints(lambda: KtLambdaExpression): List { val text = buildString { append(TYPE_INFO_PREFIX) append("this: ") - append(inlayHintsTypeRenderer.renderType(implicitReceiver.type)) + append(getInlayHintsTypeRenderer(bindingContext, lambda).renderType(implicitReceiver.type)) } return listOf(InlayInfo(text, lbrace.textRange.endOffset)) } @@ -41,7 +41,7 @@ fun provideLambdaImplicitHints(lambda: KtLambdaExpression): List { val text = buildString { append(TYPE_INFO_PREFIX) append("it: ") - append(inlayHintsTypeRenderer.renderType(type)) + append(getInlayHintsTypeRenderer(bindingContext, lambda).renderType(type)) } return listOf(InlayInfo(text, lbrace.textRange.endOffset)) } diff --git a/idea/src/org/jetbrains/kotlin/idea/parameterInfo/TypeHints.kt b/idea/src/org/jetbrains/kotlin/idea/parameterInfo/TypeHints.kt index 420eab9221d..3f5d514a07b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/parameterInfo/TypeHints.kt +++ b/idea/src/org/jetbrains/kotlin/idea/parameterInfo/TypeHints.kt @@ -8,28 +8,56 @@ 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.ClassDescriptor +import org.jetbrains.kotlin.descriptors.ClassifierDescriptor import org.jetbrains.kotlin.descriptors.ConstructorDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings import org.jetbrains.kotlin.idea.intentions.SpecifyTypeExplicitlyIntention +import org.jetbrains.kotlin.idea.util.getResolutionScope +import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.load.java.sam.SamConstructorDescriptor import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.renderer.ClassifierNamePolicy import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.renderer.RenderingFormat +import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall +import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.typeUtil.containsError import org.jetbrains.kotlin.types.typeUtil.immediateSupertypes //hack to separate type presentation from param info presentation const val TYPE_INFO_PREFIX = "@TYPE@" -val inlayHintsTypeRenderer = DescriptorRenderer.COMPACT_WITH_SHORT_TYPES.withOptions { - textFormat = RenderingFormat.PLAIN - renderUnabbreviatedType = false + +class ImportAwareClassifierNamePolicy( + val bindingContext: BindingContext, + val context: KtElement +) : ClassifierNamePolicy { + override fun renderClassifier(classifier: ClassifierDescriptor, renderer: DescriptorRenderer): String { + if (classifier.containingDeclaration is ClassDescriptor) { + val resolutionFacade = context.getResolutionFacade() + val scope = context.getResolutionScope(bindingContext, resolutionFacade) + if (scope.findClassifier(classifier.name, NoLookupLocation.FROM_IDE) == classifier) { + return classifier.name.asString() + } + } + + return ClassifierNamePolicy.SHORT.renderClassifier(classifier, renderer) + } } +fun getInlayHintsTypeRenderer(bindingContext: BindingContext, context: KtElement) = + DescriptorRenderer.COMPACT_WITH_SHORT_TYPES.withOptions { + textFormat = RenderingFormat.PLAIN + renderUnabbreviatedType = false + classifierNamePolicy = ImportAwareClassifierNamePolicy(bindingContext, context) + } + fun providePropertyTypeHint(elem: PsiElement): List { (elem as? KtCallableDeclaration)?.let { property -> property.nameIdentifier?.let { ident -> @@ -50,14 +78,13 @@ fun provideTypeHint(element: KtCallableDeclaration, offset: Int): List = mapOf(1 to "1").entries""" + ) + } + + fun testNestedClassWithoutImport() { + HintType.PROPERTY_HINT.option.set(true) + check( + """val entries = mapOf(1 to "1").entries""" + ) + } }