Completion should not generate deprecated lambda syntax

This commit is contained in:
Valentin Kipyatkov
2015-04-07 14:55:44 +03:00
parent 723bf0ecf9
commit f73a41aa15
14 changed files with 19 additions and 28 deletions
@@ -69,8 +69,7 @@ fun insertLambdaTemplate(context: InsertionContext, placeholderRange: TextRange,
fun buildLambdaPresentation(lambdaType: JetType): String { fun buildLambdaPresentation(lambdaType: JetType): String {
val parameterTypes = functionParameterTypes(lambdaType) val parameterTypes = functionParameterTypes(lambdaType)
val parametersPresentation = parameterTypes.map { IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(it) }.joinToString(", ") 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 "{ $parametersPresentation -> ... }"
return "{ ${wrap(parametersPresentation)} -> ... }"
} }
private fun needExplicitParameterTypes(context: InsertionContext, placeholderRange: TextRange, lambdaType: JetType): Boolean { 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 { private fun buildTemplate(lambdaType: JetType, explicitParameterTypes: Boolean, project: Project): Template {
val parameterTypes = functionParameterTypes(lambdaType) val parameterTypes = functionParameterTypes(lambdaType)
val useParenthesis = explicitParameterTypes || parameterTypes.size() != 1
val manager = TemplateManager.getInstance(project) val manager = TemplateManager.getInstance(project)
val template = manager.createTemplate("", "") val template = manager.createTemplate("", "")
template.setToShortenLongNames(true) template.setToShortenLongNames(true)
//template.setToReformat(true) //TODO //template.setToReformat(true) //TODO
template.addTextSegment("{ ") template.addTextSegment("{ ")
if (useParenthesis) {
template.addTextSegment("(")
}
for ((i, parameterType) in parameterTypes.withIndex()) { for ((i, parameterType) in parameterTypes.withIndex()) {
if (i > 0) { if (i > 0) {
@@ -115,9 +109,6 @@ private fun buildTemplate(lambdaType: JetType, explicitParameterTypes: Boolean,
} }
} }
if (useParenthesis) {
template.addTextSegment(")")
}
template.addTextSegment(" -> ") template.addTextSegment(" -> ")
template.addEndVariable() template.addEndVariable()
template.addTextSegment(" }") template.addTextSegment(" }")
@@ -29,8 +29,8 @@ object LambdaItems {
public fun addToCollection(collection: MutableCollection<LookupElement>, functionExpectedInfos: Collection<ExpectedInfo>) { public fun addToCollection(collection: MutableCollection<LookupElement>, functionExpectedInfos: Collection<ExpectedInfo>) {
val distinctTypes = functionExpectedInfos.map { it.type }.toSet() val distinctTypes = functionExpectedInfos.map { it.type }.toSet()
val singleType = if (distinctTypes.size == 1) distinctTypes.single() else null val singleType = if (distinctTypes.size() == 1) distinctTypes.single() else null
val singleSignatureLength = singleType?.let { KotlinBuiltIns.getParameterTypeProjectionsFromFunctionType(it).size } val singleSignatureLength = singleType?.let { KotlinBuiltIns.getParameterTypeProjectionsFromFunctionType(it).size() }
val offerNoParametersLambda = singleSignatureLength == 0 || singleSignatureLength == 1 val offerNoParametersLambda = singleSignatureLength == 0 || singleSignatureLength == 1
if (offerNoParametersLambda) { if (offerNoParametersLambda) {
val lookupElement = LookupElementBuilder.create("{...}") val lookupElement = LookupElementBuilder.create("{...}")
@@ -49,7 +49,7 @@ object LambdaItems {
val offset = context.getStartOffset() val offset = context.getStartOffset()
val placeholder = "{}" val placeholder = "{}"
context.getDocument().replaceString(offset, context.getTailOffset(), 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() .suppressAutoInsertion()
.assignSmartCompletionPriority(SmartCompletionItemPriority.LAMBDA) .assignSmartCompletionPriority(SmartCompletionItemPriority.LAMBDA)
@@ -5,4 +5,4 @@ fun test() {
} }
// EXIST: { lookupString:"foo", itemText: "foo", tailText: "(p: (String, Char) -> Unit) (<root>)", typeText:"Unit" } // EXIST: { lookupString:"foo", itemText: "foo", tailText: "(p: (String, Char) -> Unit) (<root>)", typeText:"Unit" }
// EXIST: { lookupString:"foo", itemText: "foo", tailText: " { (String, Char) -> ... } (<root>)", typeText:"Unit" } // EXIST: { lookupString:"foo", itemText: "foo", tailText: " { String, Char -> ... } (<root>)", typeText:"Unit" }
@@ -5,4 +5,4 @@ fun test() {
} }
// EXIST: { lookupString:"foo", itemText: "foo", tailText: "(p: (String, Char) -> Unit) for String in <root>", typeText:"Unit" } // EXIST: { lookupString:"foo", itemText: "foo", tailText: "(p: (String, Char) -> Unit) for String in <root>", typeText:"Unit" }
// EXIST: { lookupString:"foo", itemText: "foo", tailText: " { (String, Char) -> ... } for String in <root>", typeText:"Unit" } // EXIST: { lookupString:"foo", itemText: "foo", tailText: " { String, Char -> ... } for String in <root>", typeText:"Unit" }
@@ -1,5 +1,5 @@
fun foo(p : (String, Char) -> Boolean){} fun foo(p : (String, Char) -> Boolean){}
fun main(args: Array<String>) { fun main(args: Array<String>) {
foo { (s, c) -> <caret> } foo { s, c -> <caret> }
} }
@@ -2,5 +2,5 @@ fun foo(p : (String, Char) -> Boolean){}
fun foo(p : (String, Boolean) -> Boolean){} fun foo(p : (String, Boolean) -> Boolean){}
fun main(args: Array<String>) { fun main(args: Array<String>) {
foo { (s: String, c: Char) -> <caret> } foo { s: String, c: Char -> <caret> }
} }
@@ -5,4 +5,4 @@ fun bar() {
foo(<caret>) foo(<caret>)
} }
// ELEMENT: "{ (String, StringBuilder) -> ... }" // ELEMENT: "{ String, StringBuilder -> ... }"
@@ -2,7 +2,7 @@ fun foo(p: (String, StringBuilder) -> Unit){}
fun foo(p: (String) -> Unit){} fun foo(p: (String) -> Unit){}
fun bar() { fun bar() {
foo({ (s, stringBuilder) -> <caret> }) foo({ s, stringBuilder -> <caret> })
} }
// ELEMENT: "{ (String, StringBuilder) -> ... }" // ELEMENT: "{ String, StringBuilder -> ... }"
@@ -5,4 +5,4 @@ fun bar() {
foo(<caret>) foo(<caret>)
} }
// ELEMENT: "{ (String, StringBuilder) -> ... }" // ELEMENT: "{ String, StringBuilder -> ... }"
@@ -2,7 +2,7 @@ fun foo(p: (String, StringBuilder) -> Unit){}
fun foo(p: (Int, Char) -> Unit){} fun foo(p: (Int, Char) -> Unit){}
fun bar() { fun bar() {
foo({ (s: String, stringBuilder: StringBuilder) -> <caret> }) foo({ s: String, stringBuilder: StringBuilder -> <caret> })
} }
// ELEMENT: "{ (String, StringBuilder) -> ... }" // ELEMENT: "{ String, StringBuilder -> ... }"
@@ -4,4 +4,4 @@ fun bar() {
foo(<caret>) foo(<caret>)
} }
// ELEMENT: "{ (String, String, String) -> ... }" // ELEMENT: "{ String, String, String -> ... }"
@@ -4,7 +4,7 @@ fun foo(p: (java.util.Date) -> Unit){}
fun foo(p: (String) -> Unit){} fun foo(p: (String) -> Unit){}
fun bar() { fun bar() {
foo({ (date: Date) -> <caret> }) foo({ date: Date -> <caret> })
} }
// ELEMENT: "{ Date -> ... }" // ELEMENT: "{ Date -> ... }"
@@ -5,4 +5,4 @@ fun bar() {
} }
// ABSENT: "{...}" // ABSENT: "{...}"
// EXIST: "{ (String, Int) -> ... }" // EXIST: "{ String, Int -> ... }"
@@ -91,11 +91,11 @@ public class BasicCompletionHandlerTest : CompletionHandlerTestBase(){
fun testHigherOrderFunctionWithArg() = doTest(2, "filterNot", null, '\n') 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 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') fun testForceParenthesisForTabChar() = doTest(0, "some", null, '\t')