From e09fadae515139f3a26cb44a8e73e3e0ec974bf0 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Fri, 24 Jul 2015 21:02:53 +0300 Subject: [PATCH] KT-8623 Completion get in the way when function `as` declared #KT-8623 Fixed --- .../idea/completion/CompletionSorting.kt | 27 +++++++++++++------ .../handlers/basic/PreferMatchingKeyword.kt | 9 +++++++ .../basic/PreferMatchingKeyword.kt.after | 9 +++++++ .../BasicCompletionHandlerTestGenerated.java | 6 +++++ 4 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 idea/idea-completion/testData/handlers/basic/PreferMatchingKeyword.kt create mode 100644 idea/idea-completion/testData/handlers/basic/PreferMatchingKeyword.kt.after diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/CompletionSorting.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/CompletionSorting.kt index 4599210c11b..0342cb014d0 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/CompletionSorting.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/CompletionSorting.kt @@ -23,11 +23,7 @@ import com.intellij.codeInsight.completion.CompletionType import com.intellij.codeInsight.lookup.LookupElement import com.intellij.codeInsight.lookup.LookupElementWeigher import com.intellij.codeInsight.lookup.WeighingContext -import com.intellij.psi.PsiClass -import com.intellij.psi.PsiDocCommentOwner -import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.FunctionDescriptor -import org.jetbrains.kotlin.descriptors.PackageViewDescriptor import org.jetbrains.kotlin.descriptors.VariableDescriptor import org.jetbrains.kotlin.idea.completion.smart.NameSimilarityWeigher import org.jetbrains.kotlin.idea.completion.smart.SMART_COMPLETION_ITEM_PRIORITY_KEY @@ -35,10 +31,8 @@ import org.jetbrains.kotlin.idea.completion.smart.SmartCompletionItemPriority import org.jetbrains.kotlin.idea.core.completion.DeclarationLookupObject import org.jetbrains.kotlin.idea.util.ImportInsertHelper import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.isValidJavaFqName import org.jetbrains.kotlin.platform.JavaToKotlinClassMap import org.jetbrains.kotlin.psi.JetFile -import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.ImportPath import java.util.HashSet @@ -116,9 +110,26 @@ private object DeprecatedWeigher : LookupElementWeigher("kotlin.deprecated") { } private object PreferMatchingItemWeigher : LookupElementWeigher("kotlin.preferMatching", false, true) { - override fun weigh(element: LookupElement, context: WeighingContext): Comparable { + private enum class Weight { + keywordExactMatch, + defaultExactMatch, + functionExactMatch, + notExactMatch + } + + override fun weigh(element: LookupElement, context: WeighingContext): Weight { val prefix = context.itemPattern(element) - return if (element.getLookupString() == prefix) 0 else 1 + if (element.lookupString != prefix) { + return Weight.notExactMatch + } + else { + val o = element.`object` + return when (o) { + is KeywordLookupObject -> Weight.keywordExactMatch + is DeclarationLookupObject -> if (o.descriptor is FunctionDescriptor) Weight.functionExactMatch else Weight.defaultExactMatch + else -> Weight.defaultExactMatch + } + } } } diff --git a/idea/idea-completion/testData/handlers/basic/PreferMatchingKeyword.kt b/idea/idea-completion/testData/handlers/basic/PreferMatchingKeyword.kt new file mode 100644 index 00000000000..d15c3d156d0 --- /dev/null +++ b/idea/idea-completion/testData/handlers/basic/PreferMatchingKeyword.kt @@ -0,0 +1,9 @@ +fun Any.`as`(s: String): String{} + +fun foo(p: Any) { + p as +} + +// INVOCATION_COUNT: 0 +// ELEMENT: * +// CHAR: ' ' diff --git a/idea/idea-completion/testData/handlers/basic/PreferMatchingKeyword.kt.after b/idea/idea-completion/testData/handlers/basic/PreferMatchingKeyword.kt.after new file mode 100644 index 00000000000..5b8ce211ea9 --- /dev/null +++ b/idea/idea-completion/testData/handlers/basic/PreferMatchingKeyword.kt.after @@ -0,0 +1,9 @@ +fun Any.`as`(s: String): String{} + +fun foo(p: Any) { + p as +} + +// INVOCATION_COUNT: 0 +// ELEMENT: * +// CHAR: ' ' diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/BasicCompletionHandlerTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/BasicCompletionHandlerTestGenerated.java index 7f62698685d..dbb6b299632 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/BasicCompletionHandlerTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/handlers/BasicCompletionHandlerTestGenerated.java @@ -89,6 +89,12 @@ public class BasicCompletionHandlerTestGenerated extends AbstractBasicCompletion doTest(fileName); } + @TestMetadata("PreferMatchingKeyword.kt") + public void testPreferMatchingKeyword() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/PreferMatchingKeyword.kt"); + doTest(fileName); + } + @TestMetadata("ReplaceFunctionCallByProperty.kt") public void testReplaceFunctionCallByProperty() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/basic/ReplaceFunctionCallByProperty.kt");