From e4dfa5897fa26665f0b2e8743ec65827ae43a8a3 Mon Sep 17 00:00:00 2001 From: Alexander Podkhalyuzin Date: Thu, 5 Apr 2018 19:22:05 +0300 Subject: [PATCH] Do not show duplicated names in variables completion list #KT-23546 Fixed --- .../VariableOrParameterNameWithTypeCompletion.kt | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/VariableOrParameterNameWithTypeCompletion.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/VariableOrParameterNameWithTypeCompletion.kt index 4f6bae8dbf0..30c174ed019 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/VariableOrParameterNameWithTypeCompletion.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/VariableOrParameterNameWithTypeCompletion.kt @@ -72,6 +72,7 @@ class VariableOrParameterNameWithTypeCompletion( } private val suggestionsByTypesAdded = HashSet() + private val lookupNamesAdded = HashSet() fun addFromImportedClasses(position: PsiElement, bindingContext: BindingContext, visibilityFilter: (DeclarationDescriptor) -> Boolean) { for ((classNameMatcher, userPrefix) in classNamePrefixMatchers.zip(userPrefixes)) { @@ -103,7 +104,7 @@ class VariableOrParameterNameWithTypeCompletion( } fun addFromParametersInFile(position: PsiElement, resolutionFacade: ResolutionFacade, visibilityFilter: (DeclarationDescriptor) -> Boolean) { - val lookupElementToCount = LinkedHashMap() + val lookupElementToCount = LinkedHashMap>() position.containingFile.forEachDescendantOfType( canGoInside = { it !is KtExpression || it is KtDeclaration } // we analyze parameters inside bodies to not resolve too much ) { parameter -> @@ -116,16 +117,18 @@ class VariableOrParameterNameWithTypeCompletion( val parameterType = descriptor.type if (parameterType.isVisible(visibilityFilter)) { val lookupElement = MyLookupElement.create(name, ArbitraryType(parameterType), withType, lookupElementFactory)!! - val count = lookupElementToCount[lookupElement] ?: 0 - lookupElementToCount[lookupElement] = count + 1 + val (count, name) = lookupElementToCount[lookupElement] ?: Pair(0, name) + lookupElementToCount[lookupElement] = Pair(count + 1, name) } } } } - for ((lookupElement, count) in lookupElementToCount) { + for ((lookupElement, countAndName) in lookupElementToCount) { + val (count, name) = countAndName lookupElement.putUserData(PRIORITY_KEY, -count) - collector.addElement(lookupElement) + if (withType || !lookupNamesAdded.contains(name)) collector.addElement(lookupElement) + lookupNamesAdded.add(name) } } @@ -148,8 +151,9 @@ class VariableOrParameterNameWithTypeCompletion( val lookupElement = MyLookupElement.create(parameterName, type, withType, lookupElementFactory) if (lookupElement != null) { lookupElement.putUserData(PRIORITY_KEY, userPrefix.length) // suggestions with longer user prefix get lower priority - collector.addElement(lookupElement, notImported) + if (withType || !lookupNamesAdded.contains(parameterName)) collector.addElement(lookupElement, notImported) suggestionsByTypesAdded.add(type) + lookupNamesAdded.add(parameterName) } } }