From b5e89713cdaf26c7285722e55ef0cdd568cb94f8 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 7 Apr 2016 20:38:40 +0300 Subject: [PATCH] KT-11275 Inconsistent behaviour when using lambda calls with function arguments without parens #KT-11275 Fixed --- .../org/jetbrains/kotlin/idea/core/psiModificationUtils.kt | 2 +- .../moveLambdaOutsideParentheses/noTwoConsequentLambdas.kt | 7 +++++++ .../noTwoConsequentLambdas.kt.after | 7 +++++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 6 ++++++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/moveLambdaOutsideParentheses/noTwoConsequentLambdas.kt create mode 100644 idea/testData/intentions/moveLambdaOutsideParentheses/noTwoConsequentLambdas.kt.after diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/psiModificationUtils.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/psiModificationUtils.kt index 7c8985e62eb..d2da4e91e72 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/psiModificationUtils.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/psiModificationUtils.kt @@ -94,7 +94,7 @@ fun KtCallExpression.moveFunctionLiteralOutsideParentheses() { val dummyCall = KtPsiFactory(this).createExpressionByPattern("foo()$0:'{}'", expression) as KtCallExpression val functionLiteralArgument = dummyCall.lambdaArguments.single() this.add(functionLiteralArgument) - if (argumentList.arguments.size > 1) { + if (argumentList.arguments.size > 1 || calleeExpression is KtCallExpression /* we should not remove empty parenthesis when callee is a call too - it won't parse */) { argumentList.removeArgument(argument) } else { diff --git a/idea/testData/intentions/moveLambdaOutsideParentheses/noTwoConsequentLambdas.kt b/idea/testData/intentions/moveLambdaOutsideParentheses/noTwoConsequentLambdas.kt new file mode 100644 index 00000000000..dcfa677738e --- /dev/null +++ b/idea/testData/intentions/moveLambdaOutsideParentheses/noTwoConsequentLambdas.kt @@ -0,0 +1,7 @@ +fun bar() { + foo { "one" } ({ "two" }) +} + +fun foo(a: () -> String): (() -> String) -> Unit { + return { } +} diff --git a/idea/testData/intentions/moveLambdaOutsideParentheses/noTwoConsequentLambdas.kt.after b/idea/testData/intentions/moveLambdaOutsideParentheses/noTwoConsequentLambdas.kt.after new file mode 100644 index 00000000000..9c4f7a66db9 --- /dev/null +++ b/idea/testData/intentions/moveLambdaOutsideParentheses/noTwoConsequentLambdas.kt.after @@ -0,0 +1,7 @@ +fun bar() { + foo { "one" } () { "two" } +} + +fun foo(a: () -> String): (() -> String) -> Unit { + return { } +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index b465c89d3fd..336f12c92ac 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -6777,6 +6777,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/moveLambdaOutsideParentheses/moveLambda6.kt"); doTest(fileName); } + + @TestMetadata("noTwoConsequentLambdas.kt") + public void testNoTwoConsequentLambdas() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/moveLambdaOutsideParentheses/noTwoConsequentLambdas.kt"); + doTest(fileName); + } } @TestMetadata("idea/testData/intentions/moveOutOfCompanion")