From cd6d8ffa988e2eefef50debb738281aa449114bc Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Fri, 7 Aug 2015 17:25:06 +0300 Subject: [PATCH] Basic completion prefers right type in lambda value --- .../idea/completion/smart/SmartCompletion.kt | 13 ++++++------- .../weighers/basic/expectedInfo/LambdaValue.kt | 15 +++++++++++++++ .../BasicCompletionWeigherTestGenerated.java | 6 ++++++ 3 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 idea/idea-completion/testData/weighers/basic/expectedInfo/LambdaValue.kt 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 22361e1c964..75ec10fba44 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 @@ -238,12 +238,6 @@ class SmartCompletion( } private fun calcExpectedInfos(expression: JetExpression): Collection { - if (forBasicCompletion) { - return ExpectedInfos(bindingContext, resolutionFacade, moduleDescriptor, useOuterCallsExpectedTypeCount = 0) - .calculate(expression) - .map { it.copy(tail = null) } - } - // if our expression is initializer of implicitly typed variable - take type of variable from original file (+ the same for function) val declaration = implicitlyTypedDeclarationFromInitializer(expression) if (declaration != null) { @@ -262,7 +256,12 @@ class SmartCompletion( while (true) { val infos = ExpectedInfos(bindingContext, resolutionFacade, moduleDescriptor, useOuterCallsExpectedTypeCount = count) .calculate(expression) - if (count == 2 /* use two outer calls maximum */ || infos.none { it.fuzzyType?.isAlmostEverything() ?: false }) return infos + if (count == 2 /* use two outer calls maximum */ || infos.none { it.fuzzyType?.isAlmostEverything() ?: false }) { + return if (forBasicCompletion) + infos.map { it.copy(tail = null) } + else + infos + } count++ } //TODO: we could always give higher priority to results with outer call expected type used diff --git a/idea/idea-completion/testData/weighers/basic/expectedInfo/LambdaValue.kt b/idea/idea-completion/testData/weighers/basic/expectedInfo/LambdaValue.kt new file mode 100644 index 00000000000..1df6052669a --- /dev/null +++ b/idea/idea-completion/testData/weighers/basic/expectedInfo/LambdaValue.kt @@ -0,0 +1,15 @@ +fun List.map(transform: (T) -> R): List {} + +class X { + val x1: String + val x2: Any + val x3: Int +} + +fun foo(list: List): Collection { + return list.map { it.x } +} + +// ORDER: x3 +// ORDER: x1 +// ORDER: x2 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 e9a56a1f505..f2191526b91 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 @@ -181,6 +181,12 @@ public class BasicCompletionWeigherTestGenerated extends AbstractBasicCompletion doTest(fileName); } + @TestMetadata("LambdaValue.kt") + public void testLambdaValue() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/weighers/basic/expectedInfo/LambdaValue.kt"); + doTest(fileName); + } + @TestMetadata("MultiArgsItem.kt") public void testMultiArgsItem() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/weighers/basic/expectedInfo/MultiArgsItem.kt");