From 9497143b26eb04a8cfa4540cb837bcee62aae948 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 5 Aug 2015 18:56:16 +0300 Subject: [PATCH] Smart completion works in initializer of variable with unknown type (the same as ordinary completion) #KT-8691 Fixed --- .../kotlin/idea/completion/smart/SmartCompletion.kt | 11 ++++------- .../ImplicitlyTypedValInitializerUnknownType.kt | 9 +++++++++ .../testData/weighers/smart/NoExpectedType.kt | 13 +++++++++++++ .../test/JvmSmartCompletionTestGenerated.java | 6 ++++++ .../SmartCompletionWeigherTestGenerated.java | 6 ++++++ 5 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 idea/idea-completion/testData/smart/ImplicitlyTypedValInitializerUnknownType.kt create mode 100644 idea/idea-completion/testData/weighers/smart/NoExpectedType.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 29e6d6b7d0c..ee2cd373343 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 @@ -110,12 +110,7 @@ class SmartCompletion( val asTypePositionResult = buildForAsTypePosition() if (asTypePositionResult != null) return asTypePositionResult - val expressionWithType = if (receiver != null) { - expression.getParent() as? JetExpression ?: return null - } - else { - expression - } + val expressionWithType = expression.toExpressionWithType() var originalExpectedInfos = calcExpectedInfos(expressionWithType) ?: return null originalExpectedInfos = originalExpectedInfos.filterNot { it.fuzzyType?.type?.isError ?: false } @@ -207,7 +202,9 @@ class SmartCompletion( if (originalDeclaration != null) { val originalDescriptor = originalDeclaration.resolveToDescriptor() as? CallableDescriptor val returnType = originalDescriptor?.getReturnType() - return if (returnType != null) listOf(ExpectedInfo(returnType, declaration.getName(), null)) else null + if (returnType != null && !returnType.isError) { + return listOf(ExpectedInfo(returnType, declaration.getName(), null)) + } } } diff --git a/idea/idea-completion/testData/smart/ImplicitlyTypedValInitializerUnknownType.kt b/idea/idea-completion/testData/smart/ImplicitlyTypedValInitializerUnknownType.kt new file mode 100644 index 00000000000..621e9e2b4a1 --- /dev/null +++ b/idea/idea-completion/testData/smart/ImplicitlyTypedValInitializerUnknownType.kt @@ -0,0 +1,9 @@ +class C + +fun foo(p1: String, p2: Int) { + val v = +} + +// EXIST: p1 +// EXIST: p2 +// ABSENT: C diff --git a/idea/idea-completion/testData/weighers/smart/NoExpectedType.kt b/idea/idea-completion/testData/weighers/smart/NoExpectedType.kt new file mode 100644 index 00000000000..d60bbeaae13 --- /dev/null +++ b/idea/idea-completion/testData/weighers/smart/NoExpectedType.kt @@ -0,0 +1,13 @@ +interface I { + fun takeXxx(): Int = 0 + fun takeYyy(): Int = 0 + fun takeZzz(): Int = 0 +} + +fun foo(i: I) { + val yyy = i.take +} + +// ORDER: takeYyy +// ORDER: takeXxx +// ORDER: takeZzz diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JvmSmartCompletionTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JvmSmartCompletionTestGenerated.java index 2097f2779a0..ff3b5290054 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JvmSmartCompletionTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/JvmSmartCompletionTestGenerated.java @@ -179,6 +179,12 @@ public class JvmSmartCompletionTestGenerated extends AbstractJvmSmartCompletionT doTest(fileName); } + @TestMetadata("ImplicitlyTypedValInitializerUnknownType.kt") + public void testImplicitlyTypedValInitializerUnknownType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/smart/ImplicitlyTypedValInitializerUnknownType.kt"); + doTest(fileName); + } + @TestMetadata("InClassObject.kt") public void testInClassObject() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/smart/InClassObject.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 0e6f09cddda..6b1ae61e9f8 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 @@ -185,6 +185,12 @@ public class SmartCompletionWeigherTestGenerated extends AbstractSmartCompletion doTest(fileName); } + @TestMetadata("NoExpectedType.kt") + public void testNoExpectedType() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/weighers/smart/NoExpectedType.kt"); + doTest(fileName); + } + @TestMetadata("NoNameSimilarityForQualifier.kt") public void testNoNameSimilarityForQualifier() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/weighers/smart/NoNameSimilarityForQualifier.kt");