KT-6780 Completion inserts incorrect comma in end of parameter list when lambda parameter exists

#KT-6780 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-05-06 20:06:36 +03:00
parent a036a6435b
commit 023c5eabf4
6 changed files with 51 additions and 1 deletions
@@ -208,11 +208,21 @@ class ExpectedInfos(
}
}
else {
val lastNonOptionalParam = parameters.lastOrNull { !it.hasDefaultValue() }
fun needCommaForParameter(parameter: ValueParameterDescriptor): Boolean {
if (parameter.hasDefaultValue()) return false // parameter is optional
if (parameter.getVarargElementType() != null) return false // vararg arguments list can be empty
// last non-optional parameter of functional type can be placed outside parenthesis:
if (parameter == lastNonOptionalParam && KotlinBuiltIns.isFunctionOrExtensionFunctionType(parameter.getType())) return false
return true
}
val tail = if (isFunctionLiteralArgument)
null
else if (parameterIndex == parameters.lastIndex)
Tail.RPARENTH //TODO: support square brackets
else if (parameters.drop(parameterIndex + 1).all { it.hasDefaultValue() || it.getVarargElementType() != null })
else if (parameters.drop(parameterIndex + 1).none(::needCommaForParameter))
null
else
Tail.COMMA
@@ -0,0 +1,7 @@
fun foo(p: Int, handler: () -> Unit){}
fun bar(p: Int) {
foo(<caret>) { doIt() }
}
// ELEMENT: p
@@ -0,0 +1,7 @@
fun foo(p: Int, handler: () -> Unit){}
fun bar(p: Int) {
foo(p<caret>) { doIt() }
}
// ELEMENT: p
@@ -0,0 +1,7 @@
fun foo(p: Int, handler: () -> Unit, optional: String = ""){}
fun bar(p: Int) {
foo(<caret>)
}
// ELEMENT: p
@@ -0,0 +1,7 @@
fun foo(p: Int, handler: () -> Unit, optional: String = ""){}
fun bar(p: Int) {
foo(p<caret>)
}
// ELEMENT: p
@@ -329,6 +329,12 @@ public class SmartCompletionHandlerTestGenerated extends AbstractSmartCompletion
doTest(fileName);
}
@TestMetadata("FunctionLiteralParamAlreadyExist.kt")
public void testFunctionLiteralParamAlreadyExist() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/smart/FunctionLiteralParamAlreadyExist.kt");
doTest(fileName);
}
@TestMetadata("FunctionReference1.kt")
public void testFunctionReference1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/smart/FunctionReference1.kt");
@@ -461,6 +467,12 @@ public class SmartCompletionHandlerTestGenerated extends AbstractSmartCompletion
doTest(fileName);
}
@TestMetadata("LastNonOptionalParamIsFunction.kt")
public void testLastNonOptionalParamIsFunction() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/smart/LastNonOptionalParamIsFunction.kt");
doTest(fileName);
}
@TestMetadata("MergeTail1.kt")
public void testMergeTail1() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/idea-completion/testData/handlers/smart/MergeTail1.kt");