From f73a41aa157a96d3a68d77e19d13cf052aca6994 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 7 Apr 2015 14:55:44 +0300 Subject: [PATCH] Completion should not generate deprecated lambda syntax --- .../kotlin/idea/completion/handlers/GenerateLambda.kt | 11 +---------- .../kotlin/idea/completion/smart/LambdaItems.kt | 6 +++--- .../completion/basic/common/HigherOrderFunction1.kt | 2 +- .../completion/basic/common/HigherOrderFunction2.kt | 2 +- .../handlers/HigherOrderFunctionWithArgs1.kt.after | 2 +- .../handlers/HigherOrderFunctionWithArgs3.kt.after | 2 +- idea/testData/completion/handlers/smart/Lambda4.kt | 2 +- .../completion/handlers/smart/Lambda4.kt.after | 4 ++-- idea/testData/completion/handlers/smart/Lambda5.kt | 2 +- .../completion/handlers/smart/Lambda5.kt.after | 4 ++-- .../completion/handlers/smart/Lambda6.kt.todo | 2 +- .../handlers/smart/LambdaInsertImport.kt.after | 2 +- idea/testData/completion/smart/functionLiterals/3.kt | 2 +- .../completion/handlers/BasicCompletionHandlerTest.kt | 4 ++-- 14 files changed, 19 insertions(+), 28 deletions(-) diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/GenerateLambda.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/GenerateLambda.kt index aea5b76fdba..d5b9a257fe1 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/GenerateLambda.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/handlers/GenerateLambda.kt @@ -69,8 +69,7 @@ fun insertLambdaTemplate(context: InsertionContext, placeholderRange: TextRange, fun buildLambdaPresentation(lambdaType: JetType): String { val parameterTypes = functionParameterTypes(lambdaType) val parametersPresentation = parameterTypes.map { IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(it) }.joinToString(", ") - fun wrap(s: String) = if (parameterTypes.size() != 1) "($s)" else s - return "{ ${wrap(parametersPresentation)} -> ... }" + return "{ $parametersPresentation -> ... }" } private fun needExplicitParameterTypes(context: InsertionContext, placeholderRange: TextRange, lambdaType: JetType): Boolean { @@ -92,17 +91,12 @@ private fun needExplicitParameterTypes(context: InsertionContext, placeholderRan private fun buildTemplate(lambdaType: JetType, explicitParameterTypes: Boolean, project: Project): Template { val parameterTypes = functionParameterTypes(lambdaType) - val useParenthesis = explicitParameterTypes || parameterTypes.size() != 1 - val manager = TemplateManager.getInstance(project) val template = manager.createTemplate("", "") template.setToShortenLongNames(true) //template.setToReformat(true) //TODO template.addTextSegment("{ ") - if (useParenthesis) { - template.addTextSegment("(") - } for ((i, parameterType) in parameterTypes.withIndex()) { if (i > 0) { @@ -115,9 +109,6 @@ private fun buildTemplate(lambdaType: JetType, explicitParameterTypes: Boolean, } } - if (useParenthesis) { - template.addTextSegment(")") - } template.addTextSegment(" -> ") template.addEndVariable() template.addTextSegment(" }") diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/LambdaItems.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/LambdaItems.kt index a0857b8eea4..9c829f5981c 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/LambdaItems.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/smart/LambdaItems.kt @@ -29,8 +29,8 @@ object LambdaItems { public fun addToCollection(collection: MutableCollection, functionExpectedInfos: Collection) { val distinctTypes = functionExpectedInfos.map { it.type }.toSet() - val singleType = if (distinctTypes.size == 1) distinctTypes.single() else null - val singleSignatureLength = singleType?.let { KotlinBuiltIns.getParameterTypeProjectionsFromFunctionType(it).size } + val singleType = if (distinctTypes.size() == 1) distinctTypes.single() else null + val singleSignatureLength = singleType?.let { KotlinBuiltIns.getParameterTypeProjectionsFromFunctionType(it).size() } val offerNoParametersLambda = singleSignatureLength == 0 || singleSignatureLength == 1 if (offerNoParametersLambda) { val lookupElement = LookupElementBuilder.create("{...}") @@ -49,7 +49,7 @@ object LambdaItems { val offset = context.getStartOffset() val placeholder = "{}" context.getDocument().replaceString(offset, context.getTailOffset(), placeholder) - insertLambdaTemplate(context, TextRange(offset, offset + placeholder.length), functionType) + insertLambdaTemplate(context, TextRange(offset, offset + placeholder.length()), functionType) }) .suppressAutoInsertion() .assignSmartCompletionPriority(SmartCompletionItemPriority.LAMBDA) diff --git a/idea/testData/completion/basic/common/HigherOrderFunction1.kt b/idea/testData/completion/basic/common/HigherOrderFunction1.kt index 9333f5c921b..0f040ffe8ce 100644 --- a/idea/testData/completion/basic/common/HigherOrderFunction1.kt +++ b/idea/testData/completion/basic/common/HigherOrderFunction1.kt @@ -5,4 +5,4 @@ fun test() { } // EXIST: { lookupString:"foo", itemText: "foo", tailText: "(p: (String, Char) -> Unit) ()", typeText:"Unit" } -// EXIST: { lookupString:"foo", itemText: "foo", tailText: " { (String, Char) -> ... } ()", typeText:"Unit" } +// EXIST: { lookupString:"foo", itemText: "foo", tailText: " { String, Char -> ... } ()", typeText:"Unit" } diff --git a/idea/testData/completion/basic/common/HigherOrderFunction2.kt b/idea/testData/completion/basic/common/HigherOrderFunction2.kt index 3037f76ad78..ee9b2aad939 100644 --- a/idea/testData/completion/basic/common/HigherOrderFunction2.kt +++ b/idea/testData/completion/basic/common/HigherOrderFunction2.kt @@ -5,4 +5,4 @@ fun test() { } // EXIST: { lookupString:"foo", itemText: "foo", tailText: "(p: (String, Char) -> Unit) for String in ", typeText:"Unit" } -// EXIST: { lookupString:"foo", itemText: "foo", tailText: " { (String, Char) -> ... } for String in ", typeText:"Unit" } +// EXIST: { lookupString:"foo", itemText: "foo", tailText: " { String, Char -> ... } for String in ", typeText:"Unit" } diff --git a/idea/testData/completion/handlers/HigherOrderFunctionWithArgs1.kt.after b/idea/testData/completion/handlers/HigherOrderFunctionWithArgs1.kt.after index 97539c9cad4..dea6b2f41a6 100644 --- a/idea/testData/completion/handlers/HigherOrderFunctionWithArgs1.kt.after +++ b/idea/testData/completion/handlers/HigherOrderFunctionWithArgs1.kt.after @@ -1,5 +1,5 @@ fun foo(p : (String, Char) -> Boolean){} fun main(args: Array) { - foo { (s, c) -> } + foo { s, c -> } } diff --git a/idea/testData/completion/handlers/HigherOrderFunctionWithArgs3.kt.after b/idea/testData/completion/handlers/HigherOrderFunctionWithArgs3.kt.after index e41a63fb28a..b556b37ff54 100644 --- a/idea/testData/completion/handlers/HigherOrderFunctionWithArgs3.kt.after +++ b/idea/testData/completion/handlers/HigherOrderFunctionWithArgs3.kt.after @@ -2,5 +2,5 @@ fun foo(p : (String, Char) -> Boolean){} fun foo(p : (String, Boolean) -> Boolean){} fun main(args: Array) { - foo { (s: String, c: Char) -> } + foo { s: String, c: Char -> } } diff --git a/idea/testData/completion/handlers/smart/Lambda4.kt b/idea/testData/completion/handlers/smart/Lambda4.kt index 1223938ecb3..9b9adce74b6 100644 --- a/idea/testData/completion/handlers/smart/Lambda4.kt +++ b/idea/testData/completion/handlers/smart/Lambda4.kt @@ -5,4 +5,4 @@ fun bar() { foo() } -// ELEMENT: "{ (String, StringBuilder) -> ... }" +// ELEMENT: "{ String, StringBuilder -> ... }" diff --git a/idea/testData/completion/handlers/smart/Lambda4.kt.after b/idea/testData/completion/handlers/smart/Lambda4.kt.after index b5a4eee3f62..1013fdbd882 100644 --- a/idea/testData/completion/handlers/smart/Lambda4.kt.after +++ b/idea/testData/completion/handlers/smart/Lambda4.kt.after @@ -2,7 +2,7 @@ fun foo(p: (String, StringBuilder) -> Unit){} fun foo(p: (String) -> Unit){} fun bar() { - foo({ (s, stringBuilder) -> }) + foo({ s, stringBuilder -> }) } -// ELEMENT: "{ (String, StringBuilder) -> ... }" +// ELEMENT: "{ String, StringBuilder -> ... }" diff --git a/idea/testData/completion/handlers/smart/Lambda5.kt b/idea/testData/completion/handlers/smart/Lambda5.kt index 0426f7129ed..47b345ef0ac 100644 --- a/idea/testData/completion/handlers/smart/Lambda5.kt +++ b/idea/testData/completion/handlers/smart/Lambda5.kt @@ -5,4 +5,4 @@ fun bar() { foo() } -// ELEMENT: "{ (String, StringBuilder) -> ... }" +// ELEMENT: "{ String, StringBuilder -> ... }" diff --git a/idea/testData/completion/handlers/smart/Lambda5.kt.after b/idea/testData/completion/handlers/smart/Lambda5.kt.after index c86353933e7..bacc83ab256 100644 --- a/idea/testData/completion/handlers/smart/Lambda5.kt.after +++ b/idea/testData/completion/handlers/smart/Lambda5.kt.after @@ -2,7 +2,7 @@ fun foo(p: (String, StringBuilder) -> Unit){} fun foo(p: (Int, Char) -> Unit){} fun bar() { - foo({ (s: String, stringBuilder: StringBuilder) -> }) + foo({ s: String, stringBuilder: StringBuilder -> }) } -// ELEMENT: "{ (String, StringBuilder) -> ... }" +// ELEMENT: "{ String, StringBuilder -> ... }" diff --git a/idea/testData/completion/handlers/smart/Lambda6.kt.todo b/idea/testData/completion/handlers/smart/Lambda6.kt.todo index 06e16747f50..9f528073dc0 100644 --- a/idea/testData/completion/handlers/smart/Lambda6.kt.todo +++ b/idea/testData/completion/handlers/smart/Lambda6.kt.todo @@ -4,4 +4,4 @@ fun bar() { foo() } -// ELEMENT: "{ (String, String, String) -> ... }" +// ELEMENT: "{ String, String, String -> ... }" diff --git a/idea/testData/completion/handlers/smart/LambdaInsertImport.kt.after b/idea/testData/completion/handlers/smart/LambdaInsertImport.kt.after index 66b5161f40a..863ff0b7279 100644 --- a/idea/testData/completion/handlers/smart/LambdaInsertImport.kt.after +++ b/idea/testData/completion/handlers/smart/LambdaInsertImport.kt.after @@ -4,7 +4,7 @@ fun foo(p: (java.util.Date) -> Unit){} fun foo(p: (String) -> Unit){} fun bar() { - foo({ (date: Date) -> }) + foo({ date: Date -> }) } // ELEMENT: "{ Date -> ... }" diff --git a/idea/testData/completion/smart/functionLiterals/3.kt b/idea/testData/completion/smart/functionLiterals/3.kt index aa8d6cddea0..2daa8dcf538 100644 --- a/idea/testData/completion/smart/functionLiterals/3.kt +++ b/idea/testData/completion/smart/functionLiterals/3.kt @@ -5,4 +5,4 @@ fun bar() { } // ABSENT: "{...}" -// EXIST: "{ (String, Int) -> ... }" +// EXIST: "{ String, Int -> ... }" diff --git a/idea/tests/org/jetbrains/kotlin/completion/handlers/BasicCompletionHandlerTest.kt b/idea/tests/org/jetbrains/kotlin/completion/handlers/BasicCompletionHandlerTest.kt index b88ba82bc2d..afe804fa566 100644 --- a/idea/tests/org/jetbrains/kotlin/completion/handlers/BasicCompletionHandlerTest.kt +++ b/idea/tests/org/jetbrains/kotlin/completion/handlers/BasicCompletionHandlerTest.kt @@ -91,11 +91,11 @@ public class BasicCompletionHandlerTest : CompletionHandlerTestBase(){ fun testHigherOrderFunctionWithArg() = doTest(2, "filterNot", null, '\n') - fun testHigherOrderFunctionWithArgs1() = doTest(1, "foo", "foo", " { (String, Char) -> ... }", '\n') + fun testHigherOrderFunctionWithArgs1() = doTest(1, "foo", "foo", " { String, Char -> ... }", '\n') fun testHigherOrderFunctionWithArgs2() = doTest(1, "foo", "foo", "(p: (String, Char) -> Boolean)", '\n') - fun testHigherOrderFunctionWithArgs3() = doTest(1, "foo", "foo", " { (String, Char) -> ... }", '\n') + fun testHigherOrderFunctionWithArgs3() = doTest(1, "foo", "foo", " { String, Char -> ... }", '\n') fun testForceParenthesisForTabChar() = doTest(0, "some", null, '\t')