From 62389bad0bee1dac8992a9a4d09b7267140458cd Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Fri, 19 Feb 2021 18:51:10 +0300 Subject: [PATCH] FIR IDE: Add more useful Lookup Objects Main purpose of those lookup objects for now is to allow same lookup elements to merge --- .../KotlinFirLookupElementFactory.kt | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/idea/idea-fir/src/org/jetbrains/kotlin/idea/completion/KotlinFirLookupElementFactory.kt b/idea/idea-fir/src/org/jetbrains/kotlin/idea/completion/KotlinFirLookupElementFactory.kt index e609c13aa6e..78a2c71d95d 100644 --- a/idea/idea-fir/src/org/jetbrains/kotlin/idea/completion/KotlinFirLookupElementFactory.kt +++ b/idea/idea-fir/src/org/jetbrains/kotlin/idea/completion/KotlinFirLookupElementFactory.kt @@ -61,6 +61,16 @@ private class UniqueLookupObject private data class ClassifierLookupObject(val shortName: Name, val classId: ClassId?) +/** + * Simplest lookup object so two lookup elements for the same function will clash. + */ +private data class FunctionLookupObject(val name: Name, val callableIdIfNonLocal: FqName?, val renderedFunctionParameters: String) + +/** + * Simplest lookup object so two lookup elements for the same property will clash. + */ +private data class VariableLookupObject(val name: Name, val callableIdIfNonLocal: FqName?) + private class ClassLookupElementFactory { fun createLookup(symbol: KtClassLikeSymbol): LookupElementBuilder { return LookupElementBuilder.create(ClassifierLookupObject(symbol.name, symbol.classIdIfNonLocal), symbol.name.asString()) @@ -83,7 +93,9 @@ private class TypeParameterLookupElementFactory { private class VariableLookupElementFactory { fun KtAnalysisSession.createLookup(symbol: KtVariableLikeSymbol): LookupElementBuilder { - return LookupElementBuilder.create(UniqueLookupObject(), symbol.name.asString()) + val lookupObject = VariableLookupObject(symbol.name, symbol.callableIdIfExists) + + return LookupElementBuilder.create(lookupObject, symbol.name.asString()) .withTypeText(symbol.annotatedType.type.render()) .markIfSyntheticJavaProperty(symbol) .withInsertHandler(createInsertHandler(symbol)) @@ -105,12 +117,32 @@ private class VariableLookupElementFactory { private fun createInsertHandler(symbol: KtVariableLikeSymbol): InsertHandler { return QuotedNamesAwareInsertionHandler(symbol.name) } + + private val KtVariableLikeSymbol.callableIdIfExists: FqName? + get() = when (this) { + is KtJavaFieldSymbol -> callableIdIfNonLocal + is KtKotlinPropertySymbol -> callableIdIfNonLocal + is KtSyntheticJavaPropertySymbol -> callableIdIfNonLocal + + // Compiler will complain if there would be a new type in the hierarchy + is KtEnumEntrySymbol, + is KtLocalVariableSymbol, + is KtFunctionParameterSymbol, + is KtConstructorParameterSymbol, + is KtSetterParameterSymbol -> null + } } private class FunctionLookupElementFactory { fun KtAnalysisSession.createLookup(symbol: KtFunctionSymbol): LookupElementBuilder? { + val lookupObject = FunctionLookupObject( + symbol.name, + symbol.callableIdIfNonLocal, + with(ShortNamesRenderer) { renderFunctionParameters(symbol) } + ) + return try { - LookupElementBuilder.create(UniqueLookupObject(), symbol.name.asString()) + LookupElementBuilder.create(lookupObject, symbol.name.asString()) .withTailText(getTailText(symbol), true) .withTypeText(symbol.annotatedType.type.render()) .withInsertHandler(createInsertHandler(symbol))