diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt index 4125f43ff03..5f1ea102b93 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt @@ -38,6 +38,7 @@ class RemoveBracesIntention : SelfTargetingIntention(KtElement::class override fun isApplicableTo(element: KtElement, caretOffset: Int): Boolean { val block = element.findChildBlock() ?: return false val singleStatement = block.statements.singleOrNull() ?: return false + if (singleStatement is KtLambdaExpression && singleStatement.functionLiteral.arrow == null) return false when (val container = block.parent) { is KtContainerNode -> { if (singleStatement is KtIfExpression) { @@ -52,12 +53,12 @@ class RemoveBracesIntention : SelfTargetingIntention(KtElement::class is KtWhenEntry -> { text = KotlinBundle.message("remove.braces.from.when.entry") return singleStatement !is KtNamedDeclaration - && !(singleStatement is KtLambdaExpression && singleStatement.functionLiteral.arrow == null) } else -> return false } } + override fun applyTo(element: KtElement, editor: Editor?) { val block = element.findChildBlock() ?: return val factory = KtPsiFactory(element) diff --git a/idea/testData/intentions/removeBraces/elseLambda.kt b/idea/testData/intentions/removeBraces/elseLambda.kt new file mode 100644 index 00000000000..0c2b8ee016f --- /dev/null +++ b/idea/testData/intentions/removeBraces/elseLambda.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false +fun test(i: Int) { + val predicate: () -> Boolean = + if (i == 1) { + { true } + } else { + { false } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBraces/elseLambda2.kt b/idea/testData/intentions/removeBraces/elseLambda2.kt new file mode 100644 index 00000000000..78c8b741cbb --- /dev/null +++ b/idea/testData/intentions/removeBraces/elseLambda2.kt @@ -0,0 +1,8 @@ +fun test(i: Int) { + val predicate: () -> Boolean = + if (i == 1) { + { true } + } else { + { -> false } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBraces/elseLambda2.kt.after b/idea/testData/intentions/removeBraces/elseLambda2.kt.after new file mode 100644 index 00000000000..5e864229d20 --- /dev/null +++ b/idea/testData/intentions/removeBraces/elseLambda2.kt.after @@ -0,0 +1,6 @@ +fun test(i: Int) { + val predicate: () -> Boolean = + if (i == 1) { + { true } + } else { -> false } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBraces/ifLambda.kt b/idea/testData/intentions/removeBraces/ifLambda.kt new file mode 100644 index 00000000000..91375b1a171 --- /dev/null +++ b/idea/testData/intentions/removeBraces/ifLambda.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false +fun test(i: Int) { + val predicate: () -> Boolean = + if (i == 1) { + { true } + } else { + { false } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBraces/ifLambda2.kt b/idea/testData/intentions/removeBraces/ifLambda2.kt new file mode 100644 index 00000000000..23b274bb43c --- /dev/null +++ b/idea/testData/intentions/removeBraces/ifLambda2.kt @@ -0,0 +1,8 @@ +fun test(i: Int) { + val predicate: () -> Boolean = + if (i == 1) { + { -> true } + } else { + { false } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBraces/ifLambda2.kt.after b/idea/testData/intentions/removeBraces/ifLambda2.kt.after new file mode 100644 index 00000000000..b243e771a50 --- /dev/null +++ b/idea/testData/intentions/removeBraces/ifLambda2.kt.after @@ -0,0 +1,6 @@ +fun test(i: Int) { + val predicate: () -> Boolean = + if (i == 1) { -> true } else { + { false } + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 3106b2f6685..7a1069a0055 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -13024,6 +13024,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/removeBraces/elseInPlusAssignExpression.kt"); } + @TestMetadata("elseLambda.kt") + public void testElseLambda() throws Exception { + runTest("idea/testData/intentions/removeBraces/elseLambda.kt"); + } + + @TestMetadata("elseLambda2.kt") + public void testElseLambda2() throws Exception { + runTest("idea/testData/intentions/removeBraces/elseLambda2.kt"); + } + @TestMetadata("for.kt") public void testFor() throws Exception { runTest("idea/testData/intentions/removeBraces/for.kt"); @@ -13079,6 +13089,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/removeBraces/ifInsideIf7.kt"); } + @TestMetadata("ifLambda.kt") + public void testIfLambda() throws Exception { + runTest("idea/testData/intentions/removeBraces/ifLambda.kt"); + } + + @TestMetadata("ifLambda2.kt") + public void testIfLambda2() throws Exception { + runTest("idea/testData/intentions/removeBraces/ifLambda2.kt"); + } + @TestMetadata("ifWithComment.kt") public void testIfWithComment() throws Exception { runTest("idea/testData/intentions/removeBraces/ifWithComment.kt");