diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt index 35024fde68b..6052564469e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt @@ -62,6 +62,13 @@ class RemoveBracesIntention : SelfTargetingIntention(KtElement::class if (construct is KtDoWhileExpression) { newElement.parent!!.addAfter(factory.createNewLine(), newElement) + } else if (editor != null) { + val document = editor.document + val line = document.getLineNumber(newElement.startOffset) + val rightMargin = editor.settings.getRightMargin(editor.project) + if (document.getLineEndOffset(line) - document.getLineStartOffset(line) >= rightMargin) { + newElement.parent.addBefore(factory.createNewLine(), newElement) + } } if (construct is KtIfExpression && diff --git a/idea/testData/intentions/removeBraces/ifLong.kt b/idea/testData/intentions/removeBraces/ifLong.kt new file mode 100644 index 00000000000..6b4a6e94cef --- /dev/null +++ b/idea/testData/intentions/removeBraces/ifLong.kt @@ -0,0 +1,8 @@ +fun foo( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: Boolean +): Int { + if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) { + return 1 + } + return 2 +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBraces/ifLong.kt.after b/idea/testData/intentions/removeBraces/ifLong.kt.after new file mode 100644 index 00000000000..1be6a289d81 --- /dev/null +++ b/idea/testData/intentions/removeBraces/ifLong.kt.after @@ -0,0 +1,7 @@ +fun foo( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: Boolean +): Int { + if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) + return 1 + return 2 +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBraces/whenLong.kt b/idea/testData/intentions/removeBraces/whenLong.kt new file mode 100644 index 00000000000..cccadaf2ae6 --- /dev/null +++ b/idea/testData/intentions/removeBraces/whenLong.kt @@ -0,0 +1,10 @@ +fun foo( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: Boolean +): Int { + when { + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -> { + return 1 + } + else -> return 2 + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBraces/whenLong.kt.after b/idea/testData/intentions/removeBraces/whenLong.kt.after new file mode 100644 index 00000000000..1c766484983 --- /dev/null +++ b/idea/testData/intentions/removeBraces/whenLong.kt.after @@ -0,0 +1,9 @@ +fun foo( + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: Boolean +): Int { + when { + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -> + return 1 + else -> return 2 + } +} \ 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 8b6bd38507d..36fc2095db6 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -13781,6 +13781,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/removeBraces/ifLambda2.kt"); } + @TestMetadata("ifLong.kt") + public void testIfLong() throws Exception { + runTest("idea/testData/intentions/removeBraces/ifLong.kt"); + } + @TestMetadata("ifWithComment.kt") public void testIfWithComment() throws Exception { runTest("idea/testData/intentions/removeBraces/ifWithComment.kt"); @@ -13826,6 +13831,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/removeBraces/whenLambda3.kt"); } + @TestMetadata("whenLong.kt") + public void testWhenLong() throws Exception { + runTest("idea/testData/intentions/removeBraces/whenLong.kt"); + } + @TestMetadata("whenMultiple.kt") public void testWhenMultiple() throws Exception { runTest("idea/testData/intentions/removeBraces/whenMultiple.kt");