diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/BasicCompletionSession.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/BasicCompletionSession.kt index 8ff8755724d..0c8e55eb932 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/BasicCompletionSession.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/BasicCompletionSession.kt @@ -129,7 +129,10 @@ class BasicCompletionSession( if (smartCompletion != null) { val smartCompletionInBasicWeigher = SmartCompletionInBasicWeigher(smartCompletion, callTypeAndReceiver.callType, resolutionFacade) - sorter = sorter.weighBefore(KindWeigher.toString(), smartCompletionInBasicWeigher, SmartCompletionPriorityWeigher) + sorter = sorter.weighBefore(KindWeigher.toString(), + smartCompletionInBasicWeigher, + SmartCompletionPriorityWeigher, + CallableReferenceWeigher(callTypeAndReceiver.callType)) } sorter = completionKind.addWeighers(sorter) diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/Weighers.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/Weighers.kt index a2f7c819d48..7d37f430fdf 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/Weighers.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/Weighers.kt @@ -31,10 +31,10 @@ import org.jetbrains.kotlin.idea.core.completion.DeclarationLookupObject import org.jetbrains.kotlin.idea.core.completion.PackageLookupObject import org.jetbrains.kotlin.idea.resolve.ResolutionFacade import org.jetbrains.kotlin.idea.util.CallType -import org.jetbrains.kotlin.idea.util.FuzzyType import org.jetbrains.kotlin.idea.util.toFuzzyType import org.jetbrains.kotlin.resolve.descriptorUtil.parentsWithSelf import org.jetbrains.kotlin.resolve.findOriginalTopMostOverriddenDescriptors +import org.jetbrains.kotlin.types.typeUtil.isNothing object PriorityWeigher : LookupElementWeigher("kotlin.priority") { override fun weigh(element: LookupElement, context: WeighingContext) @@ -298,4 +298,14 @@ object PreferLessParametersWeigher : LookupElementWeigher("kotlin.preferLessPara val function = lookupObject.descriptor as? FunctionDescriptor ?: return null return function.valueParameters.size } -} \ No newline at end of file +} + +class CallableReferenceWeigher(private val callType: CallType<*>) : LookupElementWeigher("kotlin.callableReference") { + override fun weigh(element: LookupElement): Int? { + if (callType == CallType.CALLABLE_REFERENCE || element.getUserData(SMART_COMPLETION_ITEM_PRIORITY_KEY) == SmartCompletionItemPriority.CALLABLE_REFERENCE) { + val descriptor = (element.`object` as? DeclarationLookupObject)?.descriptor as? CallableDescriptor + return if (descriptor?.returnType?.isNothing() == true) 1 else 0 + } + return null + } +} diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/SmartCompletion.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/SmartCompletion.kt index ca14ebe1ebc..d9c2cd49834 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/SmartCompletion.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/SmartCompletion.kt @@ -30,7 +30,6 @@ import org.jetbrains.kotlin.idea.completion.handlers.WithTailInsertHandler import org.jetbrains.kotlin.idea.core.* import org.jetbrains.kotlin.idea.resolve.ResolutionFacade import org.jetbrains.kotlin.idea.util.CallTypeAndReceiver -import org.jetbrains.kotlin.idea.util.FuzzyType import org.jetbrains.kotlin.idea.util.isAlmostEverything import org.jetbrains.kotlin.idea.util.toFuzzyType import org.jetbrains.kotlin.lexer.KtTokens @@ -351,7 +350,7 @@ class SmartCompletion( } return lookupElement - .assignSmartCompletionPriority(SmartCompletionItemPriority.FUNCTION_REFERENCE) + .assignSmartCompletionPriority(SmartCompletionItemPriority.CALLABLE_REFERENCE) .addTailAndNameSimilarity(matchedExpectedInfos) } diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/SmartCompletionSession.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/SmartCompletionSession.kt index e7d53ea81d7..b871c0ef245 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/SmartCompletionSession.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/SmartCompletionSession.kt @@ -201,7 +201,7 @@ class SmartCompletionSession( override fun createSorter(): CompletionSorter { return super.createSorter() - .weighBefore(KindWeigher.toString(), NameSimilarityWeigher, SmartCompletionPriorityWeigher) + .weighBefore(KindWeigher.toString(), NameSimilarityWeigher, SmartCompletionPriorityWeigher, CallableReferenceWeigher(callTypeAndReceiver.callType)) } override fun createLookupElementFactory(contextVariablesProvider: ContextVariablesProvider): LookupElementFactory { diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/Utils.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/Utils.kt index 9561b2d4ef4..0cf6038525a 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/Utils.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/Utils.kt @@ -268,7 +268,7 @@ enum class SmartCompletionItemPriority { ANONYMOUS_OBJECT, LAMBDA_NO_PARAMS, LAMBDA, - FUNCTION_REFERENCE, + CALLABLE_REFERENCE, NULL, INHERITOR_INSTANTIATION } diff --git a/idea/idea-completion/testData/weighers/basic/CallableReference_NothingLast.kt b/idea/idea-completion/testData/weighers/basic/CallableReference_NothingLast.kt new file mode 100644 index 00000000000..79a8df3b534 --- /dev/null +++ b/idea/idea-completion/testData/weighers/basic/CallableReference_NothingLast.kt @@ -0,0 +1,11 @@ +fun foo(p: (Int) -> Unit) { } + +fun fff1(p: Int): Nothing{} +fun fff2(p: Int): Unit{} + +fun f() { + foo(::fff) +} + +// ORDER: fff2 +// ORDER: fff1 diff --git a/idea/idea-completion/testData/weighers/smart/CallableReference_NothingLast.kt b/idea/idea-completion/testData/weighers/smart/CallableReference_NothingLast.kt new file mode 100644 index 00000000000..eafdd408ec0 --- /dev/null +++ b/idea/idea-completion/testData/weighers/smart/CallableReference_NothingLast.kt @@ -0,0 +1,12 @@ +fun foo(p: (Int) -> Unit) { } + +fun fff1(p: Int): Nothing{} +fun fff2(p: Int): Unit{} + +fun f() { + foo() +} + +// ORDER: {...} +// ORDER: { Int -> ... } +// ORDER: ::fff2 diff --git a/idea/idea-completion/testData/weighers/smart/CallableReference_NothingLast2.kt b/idea/idea-completion/testData/weighers/smart/CallableReference_NothingLast2.kt new file mode 100644 index 00000000000..79a8df3b534 --- /dev/null +++ b/idea/idea-completion/testData/weighers/smart/CallableReference_NothingLast2.kt @@ -0,0 +1,11 @@ +fun foo(p: (Int) -> Unit) { } + +fun fff1(p: Int): Nothing{} +fun fff2(p: Int): Unit{} + +fun f() { + foo(::fff) +} + +// ORDER: fff2 +// ORDER: fff1 diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/weighers/BasicCompletionWeigherTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/weighers/BasicCompletionWeigherTestGenerated.java index 499f9f10ff9..18997d17b90 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/weighers/BasicCompletionWeigherTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/weighers/BasicCompletionWeigherTestGenerated.java @@ -41,6 +41,12 @@ public class BasicCompletionWeigherTestGenerated extends AbstractBasicCompletion KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/idea-completion/testData/weighers/basic"), Pattern.compile("^([^\\.]+)\\.kt$"), true); } + @TestMetadata("CallableReference_NothingLast.kt") + public void testCallableReference_NothingLast() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/weighers/basic/CallableReference_NothingLast.kt"); + doTest(fileName); + } + @TestMetadata("Callables.kt") public void testCallables() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/weighers/basic/Callables.kt"); diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/weighers/SmartCompletionWeigherTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/weighers/SmartCompletionWeigherTestGenerated.java index 0de75715e5b..8e7dad0a769 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/weighers/SmartCompletionWeigherTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/weighers/SmartCompletionWeigherTestGenerated.java @@ -41,6 +41,18 @@ public class SmartCompletionWeigherTestGenerated extends AbstractSmartCompletion doTest(fileName); } + @TestMetadata("CallableReference_NothingLast.kt") + public void testCallableReference_NothingLast() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/weighers/smart/CallableReference_NothingLast.kt"); + doTest(fileName); + } + + @TestMetadata("CallableReference_NothingLast2.kt") + public void testCallableReference_NothingLast2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/weighers/smart/CallableReference_NothingLast2.kt"); + doTest(fileName); + } + @TestMetadata("FunctionExpected.kt") public void testFunctionExpected() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/weighers/smart/FunctionExpected.kt");