KT-12138 Do not show "::error" in smart completion when any function type accepting one argument is expected

(actually made them lowest priority)

 #KT-12138 Fixed
This commit is contained in:
Valentin Kipyatkov
2016-04-29 19:09:26 +03:00
parent 81e4114c5a
commit 16c38a2415
10 changed files with 71 additions and 7 deletions
@@ -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)
@@ -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
}
}
}
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
}
}
@@ -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)
}
@@ -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 {
@@ -268,7 +268,7 @@ enum class SmartCompletionItemPriority {
ANONYMOUS_OBJECT,
LAMBDA_NO_PARAMS,
LAMBDA,
FUNCTION_REFERENCE,
CALLABLE_REFERENCE,
NULL,
INHERITOR_INSTANTIATION
}
@@ -0,0 +1,11 @@
fun foo(p: (Int) -> Unit) { }
fun fff1(p: Int): Nothing{}
fun fff2(p: Int): Unit{}
fun f() {
foo(::fff<caret>)
}
// ORDER: fff2
// ORDER: fff1
@@ -0,0 +1,12 @@
fun foo(p: (Int) -> Unit) { }
fun fff1(p: Int): Nothing{}
fun fff2(p: Int): Unit{}
fun f() {
foo(<caret>)
}
// ORDER: {...}
// ORDER: { Int -> ... }
// ORDER: ::fff2
@@ -0,0 +1,11 @@
fun foo(p: (Int) -> Unit) { }
fun fff1(p: Int): Nothing{}
fun fff2(p: Int): Unit{}
fun f() {
foo(::fff<caret>)
}
// ORDER: fff2
// ORDER: fff1
@@ -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");
@@ -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");