From cbc7f72f150e75c07ac66876e9d31e64dc5d536a Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 8 Sep 2016 18:53:37 +0300 Subject: [PATCH] Minor: remove braces is now applicable for the whole loop or when entry --- .../idea/intentions/RemoveBracesIntention.kt | 37 ++++++++++++------- .../removeBraces/whenSimpleOutsideBlock.kt | 7 ++++ .../whenSimpleOutsideBlock.kt.after | 5 +++ .../removeBraces/whileOutsideBlock.kt | 7 ++++ .../removeBraces/whileOutsideBlock.kt.after | 5 +++ .../intentions/IntentionTestGenerated.java | 12 ++++++ 6 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 idea/testData/intentions/removeBraces/whenSimpleOutsideBlock.kt create mode 100644 idea/testData/intentions/removeBraces/whenSimpleOutsideBlock.kt.after create mode 100644 idea/testData/intentions/removeBraces/whileOutsideBlock.kt create mode 100644 idea/testData/intentions/removeBraces/whileOutsideBlock.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt index a11cf5e2883..6409a7f1691 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt @@ -18,21 +18,27 @@ package org.jetbrains.kotlin.idea.intentions import com.intellij.openapi.editor.Editor import com.intellij.psi.PsiComment +import com.intellij.psi.PsiElement import com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.psi.* -class RemoveBracesIntention : SelfTargetingIntention(KtBlockExpression::class.java, "Remove braces") { - override fun isApplicableTo(element: KtBlockExpression, caretOffset: Int): Boolean { - val singleStatement = element.statements.singleOrNull() ?: return false - val container = element.parent +class RemoveBracesIntention : SelfTargetingIntention(KtElement::class.java, "Remove braces") { + + private fun KtElement.findChildBlock() = when (this) { + is KtBlockExpression -> this + is KtLoopExpression -> body as? KtBlockExpression + is KtWhenEntry -> expression as? KtBlockExpression + else -> null + } + + override fun isApplicableTo(element: KtElement, caretOffset: Int): Boolean { + val block = element.findChildBlock() ?: return false + val singleStatement = block.statements.singleOrNull() ?: return false + val container = block.parent when (container) { is KtContainerNode -> { if (singleStatement is KtIfExpression && container.parent is KtIfExpression) return false - val lBrace = element.lBrace ?: return false - val rBrace = element.rBrace ?: return false - if (!lBrace.textRange.containsOffset(caretOffset) && !rBrace.textRange.containsOffset(caretOffset)) return false - val description = container.description() ?: return false text = "Remove braces from '$description' statement" return true @@ -45,17 +51,18 @@ class RemoveBracesIntention : SelfTargetingIntention(KtBlockE } } - override fun applyTo(element: KtBlockExpression, editor: Editor?) { - val statement = element.statements.single() + override fun applyTo(element: KtElement, editor: Editor?) { + val block = element.findChildBlock() ?: return + val statement = block.statements.single() - val container = element.parent!! + val container = block.parent!! val construct = container.parent as KtExpression - handleComments(construct, element) + handleComments(construct, block) - val newElement = element.replace(statement.copy()) + val newElement = block.replace(statement.copy()) if (construct is KtDoWhileExpression) { - newElement.parent!!.addAfter(KtPsiFactory(element).createNewLine(), newElement) + newElement.parent!!.addAfter(KtPsiFactory(block).createNewLine(), newElement) } } @@ -75,4 +82,6 @@ class RemoveBracesIntention : SelfTargetingIntention(KtBlockE sibling = sibling.nextSibling } } + + override fun allowCaretInsideElement(element: PsiElement) = element !is KtBlockExpression || element.parent is KtWhenEntry } diff --git a/idea/testData/intentions/removeBraces/whenSimpleOutsideBlock.kt b/idea/testData/intentions/removeBraces/whenSimpleOutsideBlock.kt new file mode 100644 index 00000000000..5d48a1a1097 --- /dev/null +++ b/idea/testData/intentions/removeBraces/whenSimpleOutsideBlock.kt @@ -0,0 +1,7 @@ +fun foo() { + when (1) { + else -> { + foo() + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBraces/whenSimpleOutsideBlock.kt.after b/idea/testData/intentions/removeBraces/whenSimpleOutsideBlock.kt.after new file mode 100644 index 00000000000..159b5bcf5f0 --- /dev/null +++ b/idea/testData/intentions/removeBraces/whenSimpleOutsideBlock.kt.after @@ -0,0 +1,5 @@ +fun foo() { + when (1) { + else -> foo() + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBraces/whileOutsideBlock.kt b/idea/testData/intentions/removeBraces/whileOutsideBlock.kt new file mode 100644 index 00000000000..0435ce2213b --- /dev/null +++ b/idea/testData/intentions/removeBraces/whileOutsideBlock.kt @@ -0,0 +1,7 @@ +fun doSomething(a: T) {} + +fun foo() { + while (true) { + doSomething("test") + } +} diff --git a/idea/testData/intentions/removeBraces/whileOutsideBlock.kt.after b/idea/testData/intentions/removeBraces/whileOutsideBlock.kt.after new file mode 100644 index 00000000000..77e6064ba7e --- /dev/null +++ b/idea/testData/intentions/removeBraces/whileOutsideBlock.kt.after @@ -0,0 +1,5 @@ +fun doSomething(a: T) {} + +fun foo() { + while (true) doSomething("test") +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index a9db44a18f4..f172297394b 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -9665,6 +9665,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("whenSimpleOutsideBlock.kt") + public void testWhenSimpleOutsideBlock() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/whenSimpleOutsideBlock.kt"); + doTest(fileName); + } + @TestMetadata("whenStatement.kt") public void testWhenStatement() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/whenStatement.kt"); @@ -9677,6 +9683,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("whileOutsideBlock.kt") + public void testWhileOutsideBlock() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/whileOutsideBlock.kt"); + doTest(fileName); + } + @TestMetadata("whileWithTwoStatements.kt") public void testWhileWithTwoStatements() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/whileWithTwoStatements.kt");