KT-29926: Support completion of suspend lambda body and parameters

- ^KT-29926 Fixed
This commit is contained in:
Roman Golyshev
2019-10-17 14:41:07 +03:00
committed by Roman Golyshev
parent da4097f488
commit e89a87b2e3
8 changed files with 48 additions and 4 deletions
@@ -27,7 +27,7 @@ import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.builtins.extractParameterNameFromFunctionTypeArgument import org.jetbrains.kotlin.builtins.extractParameterNameFromFunctionTypeArgument
import org.jetbrains.kotlin.builtins.getValueParameterTypesFromFunctionType import org.jetbrains.kotlin.builtins.getValueParameterTypesFromFunctionType
import org.jetbrains.kotlin.builtins.isFunctionType import org.jetbrains.kotlin.builtins.isFunctionOrSuspendFunctionType
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.completion.handlers.isCharAt import org.jetbrains.kotlin.idea.completion.handlers.isCharAt
import org.jetbrains.kotlin.idea.core.ExpectedInfos import org.jetbrains.kotlin.idea.core.ExpectedInfos
@@ -122,7 +122,7 @@ object LambdaSignatureTemplates {
val expectedInfos = ExpectedInfos(bindingContext, resolutionFacade, indicesHelper = null, useHeuristicSignatures = false).calculate(expression) val expectedInfos = ExpectedInfos(bindingContext, resolutionFacade, indicesHelper = null, useHeuristicSignatures = false).calculate(expression)
val functionTypes = expectedInfos val functionTypes = expectedInfos
.mapNotNull { it.fuzzyType?.type } .mapNotNull { it.fuzzyType?.type }
.filter(KotlinType::isFunctionType) .filter(KotlinType::isFunctionOrSuspendFunctionType)
.toSet() .toSet()
return explicitParameterTypesRequired(functionTypes, lambdaType) return explicitParameterTypesRequired(functionTypes, lambdaType)
} }
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.idea.completion.smart
import com.intellij.codeInsight.lookup.LookupElement import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.codeInsight.lookup.LookupElementBuilder import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.openapi.util.TextRange import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.builtins.isFunctionType import org.jetbrains.kotlin.builtins.isFunctionOrSuspendFunctionType
import org.jetbrains.kotlin.idea.completion.LambdaSignatureTemplates import org.jetbrains.kotlin.idea.completion.LambdaSignatureTemplates
import org.jetbrains.kotlin.idea.completion.suppressAutoInsertion import org.jetbrains.kotlin.idea.completion.suppressAutoInsertion
import org.jetbrains.kotlin.idea.core.ExpectedInfo import org.jetbrains.kotlin.idea.core.ExpectedInfo
@@ -36,7 +36,7 @@ object LambdaItems {
} }
fun addToCollection(collection: MutableCollection<LookupElement>, expectedInfos: Collection<ExpectedInfo>) { fun addToCollection(collection: MutableCollection<LookupElement>, expectedInfos: Collection<ExpectedInfo>) {
val functionExpectedInfos = expectedInfos.filter { it.fuzzyType?.type?.isFunctionType == true } val functionExpectedInfos = expectedInfos.filter { it.fuzzyType?.type?.isFunctionOrSuspendFunctionType == true }
if (functionExpectedInfos.isEmpty()) return if (functionExpectedInfos.isEmpty()) return
val functionTypes = functionExpectedInfos val functionTypes = functionExpectedInfos
@@ -0,0 +1,9 @@
fun foo(p : suspend (String, Char) -> Boolean){}
fun foo(p : suspend (String, Boolean) -> Boolean){}
fun main(args: Array<String>) {
fo<caret>
}
// ELEMENT: foo
// TAIL_TEXT: " { String, Char -> ... } (p: suspend (String, Char) -> Boolean) (<root>)"
@@ -0,0 +1,9 @@
fun foo(p : suspend (String, Char) -> Boolean){}
fun foo(p : suspend (String, Boolean) -> Boolean){}
fun main(args: Array<String>) {
foo { s: String, c: Char -> <caret> }
}
// ELEMENT: foo
// TAIL_TEXT: " { String, Char -> ... } (p: suspend (String, Char) -> Boolean) (<root>)"
@@ -0,0 +1,11 @@
fun foo(p: suspend (String, Int) -> Unit){}
fun foo(p: suspend (Char, xx: Any) -> Unit){}
fun bar() {
foo(<caret>)
}
// ABSENT: "{ s, i -> ... }"
// ABSENT: "{ c, xx -> ... }"
// EXIST: "{ String, Int -> ... }"
// EXIST: "{ Char, xx -> ... }"
@@ -959,6 +959,11 @@ public class JvmSmartCompletionTestGenerated extends AbstractJvmSmartCompletionT
public void testOutsideCallParenthesisAndVararg2() throws Exception { public void testOutsideCallParenthesisAndVararg2() throws Exception {
runTest("idea/idea-completion/testData/smart/functionLiterals/OutsideCallParenthesisAndVararg2.kt"); runTest("idea/idea-completion/testData/smart/functionLiterals/OutsideCallParenthesisAndVararg2.kt");
} }
@TestMetadata("SuspendExplicitParameterTypesRequired.kt")
public void testSuspendExplicitParameterTypesRequired() throws Exception {
runTest("idea/idea-completion/testData/smart/functionLiterals/SuspendExplicitParameterTypesRequired.kt");
}
} }
@TestMetadata("idea/idea-completion/testData/smart/generics") @TestMetadata("idea/idea-completion/testData/smart/generics")
@@ -462,6 +462,11 @@ public class BasicCompletionHandlerTestGenerated extends AbstractBasicCompletion
runTest("idea/idea-completion/testData/handlers/basic/highOrderFunctions/HigherOrderFunctionWithArgs3.kt"); runTest("idea/idea-completion/testData/handlers/basic/highOrderFunctions/HigherOrderFunctionWithArgs3.kt");
} }
@TestMetadata("HigherOrderSuspendFunctionWithArgs.kt")
public void testHigherOrderSuspendFunctionWithArgs() throws Exception {
runTest("idea/idea-completion/testData/handlers/basic/highOrderFunctions/HigherOrderSuspendFunctionWithArgs.kt");
}
@TestMetadata("InsertFunctionWithSingleParameterWithBrace.kt") @TestMetadata("InsertFunctionWithSingleParameterWithBrace.kt")
public void testInsertFunctionWithSingleParameterWithBrace() throws Exception { public void testInsertFunctionWithSingleParameterWithBrace() throws Exception {
runTest("idea/idea-completion/testData/handlers/basic/highOrderFunctions/InsertFunctionWithSingleParameterWithBrace.kt"); runTest("idea/idea-completion/testData/handlers/basic/highOrderFunctions/InsertFunctionWithSingleParameterWithBrace.kt");
@@ -462,6 +462,11 @@ public class PerformanceBasicCompletionHandlerTestGenerated extends AbstractPerf
runTest("idea/idea-completion/testData/handlers/basic/highOrderFunctions/HigherOrderFunctionWithArgs3.kt"); runTest("idea/idea-completion/testData/handlers/basic/highOrderFunctions/HigherOrderFunctionWithArgs3.kt");
} }
@TestMetadata("HigherOrderSuspendFunctionWithArgs.kt")
public void testHigherOrderSuspendFunctionWithArgs() throws Exception {
runTest("idea/idea-completion/testData/handlers/basic/highOrderFunctions/HigherOrderSuspendFunctionWithArgs.kt");
}
@TestMetadata("InsertFunctionWithSingleParameterWithBrace.kt") @TestMetadata("InsertFunctionWithSingleParameterWithBrace.kt")
public void testInsertFunctionWithSingleParameterWithBrace() throws Exception { public void testInsertFunctionWithSingleParameterWithBrace() throws Exception {
runTest("idea/idea-completion/testData/handlers/basic/highOrderFunctions/InsertFunctionWithSingleParameterWithBrace.kt"); runTest("idea/idea-completion/testData/handlers/basic/highOrderFunctions/InsertFunctionWithSingleParameterWithBrace.kt");