From cf2839eb9a8ba9b7f0b0f65599e70f41e3511fab Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 8 Sep 2016 18:20:31 +0300 Subject: [PATCH] Refactoring: remove braces from when entry intention is integrated into general remove braces intention --- .../after.kt.template | 3 - .../before.kt.template | 5 -- .../description.html | 5 -- idea/src/META-INF/plugin.xml | 5 -- .../RemoveBracesFromWhenEntryIntention.kt | 41 ----------- .../idea/intentions/RemoveBracesIntention.kt | 30 +++++--- .../lambda.kt => removeBraces/whenLambda.kt} | 0 .../whenMultiple.kt} | 0 .../whenNoBraces.kt} | 0 .../simple.kt => removeBraces/whenSimple.kt} | 0 .../whenSimple.kt.after} | 0 .../whenStatement.kt} | 0 .../removeBracesFromWhenEntry/.intention | 1 - .../intentions/IntentionTestGenerated.java | 69 ++++++++----------- 14 files changed, 49 insertions(+), 110 deletions(-) delete mode 100644 idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/after.kt.template delete mode 100644 idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/before.kt.template delete mode 100644 idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/description.html delete mode 100644 idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesFromWhenEntryIntention.kt rename idea/testData/intentions/{removeBracesFromWhenEntry/lambda.kt => removeBraces/whenLambda.kt} (100%) rename idea/testData/intentions/{removeBracesFromWhenEntry/multiple.kt => removeBraces/whenMultiple.kt} (100%) rename idea/testData/intentions/{removeBracesFromWhenEntry/noBraces.kt => removeBraces/whenNoBraces.kt} (100%) rename idea/testData/intentions/{removeBracesFromWhenEntry/simple.kt => removeBraces/whenSimple.kt} (100%) rename idea/testData/intentions/{removeBracesFromWhenEntry/simple.kt.after => removeBraces/whenSimple.kt.after} (100%) rename idea/testData/intentions/{removeBracesFromWhenEntry/statement.kt => removeBraces/whenStatement.kt} (100%) delete mode 100644 idea/testData/intentions/removeBracesFromWhenEntry/.intention diff --git a/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/after.kt.template b/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/after.kt.template deleted file mode 100644 index 99b515f4498..00000000000 --- a/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/after.kt.template +++ /dev/null @@ -1,3 +0,0 @@ -when (a) { - 1 -> b() -} diff --git a/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/before.kt.template b/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/before.kt.template deleted file mode 100644 index 57cbc88c577..00000000000 --- a/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/before.kt.template +++ /dev/null @@ -1,5 +0,0 @@ -when (a) { - 1 -> { - b() - } -} diff --git a/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/description.html b/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/description.html deleted file mode 100644 index b498f4c3b5f..00000000000 --- a/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/description.html +++ /dev/null @@ -1,5 +0,0 @@ - - -This intention removes braces from 'when' entries with only a single expression. - - \ No newline at end of file diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 0f2ec24ef64..7af8f40266d 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -1325,11 +1325,6 @@ Kotlin - - org.jetbrains.kotlin.idea.intentions.RemoveBracesFromWhenEntryIntention - Kotlin - - org.jetbrains.kotlin.idea.intentions.ReplaceRangeToWithUntilIntention Kotlin diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesFromWhenEntryIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesFromWhenEntryIntention.kt deleted file mode 100644 index 25a39d03709..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesFromWhenEntryIntention.kt +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2010-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.idea.intentions - -import com.intellij.openapi.editor.Editor -import com.intellij.psi.PsiElement -import org.jetbrains.kotlin.psi.KtBlockExpression -import org.jetbrains.kotlin.psi.KtNamedDeclaration -import org.jetbrains.kotlin.psi.KtWhenEntry - -class RemoveBracesFromWhenEntryIntention : SelfTargetingIntention(KtWhenEntry::class.java, "Remove braces from 'when' entry") { - - override fun isApplicableTo(element: KtWhenEntry, caretOffset: Int): Boolean { - val block = element.expression as? KtBlockExpression ?: return false - val singleExpression = block.statements.singleOrNull() ?: return false - return singleExpression !is KtNamedDeclaration - } - - override fun applyTo(element: KtWhenEntry, editor: Editor?) { - val block = element.expression as KtBlockExpression - block.replace(block.statements.single()) - } - - override fun allowCaretInsideElement(element: PsiElement): Boolean { - return element !is KtBlockExpression || element.parent is KtWhenEntry - } -} diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt index 0bde8c363d8..a11cf5e2883 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesIntention.kt @@ -24,24 +24,32 @@ 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 + when (container) { + is KtContainerNode -> { + if (singleStatement is KtIfExpression && container.parent is KtIfExpression) return false - val containerNode = element.parent as? KtContainerNode ?: return false - if (singleStatement is KtIfExpression && containerNode.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 lBrace = element.lBrace ?: return false - val rBrace = element.rBrace ?: return false - if (!lBrace.textRange.containsOffset(caretOffset) && !rBrace.textRange.containsOffset(caretOffset)) return false - - val description = containerNode.description() ?: return false - text = "Remove braces from '$description' statement" - return true + val description = container.description() ?: return false + text = "Remove braces from '$description' statement" + return true + } + is KtWhenEntry -> { + text = "Remove braces from 'when' entry" + return singleStatement !is KtNamedDeclaration + } + else -> return false + } } override fun applyTo(element: KtBlockExpression, editor: Editor?) { val statement = element.statements.single() - val containerNode = element.parent as KtContainerNode - val construct = containerNode.parent as KtExpression + val container = element.parent!! + val construct = container.parent as KtExpression handleComments(construct, element) val newElement = element.replace(statement.copy()) diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/lambda.kt b/idea/testData/intentions/removeBraces/whenLambda.kt similarity index 100% rename from idea/testData/intentions/removeBracesFromWhenEntry/lambda.kt rename to idea/testData/intentions/removeBraces/whenLambda.kt diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/multiple.kt b/idea/testData/intentions/removeBraces/whenMultiple.kt similarity index 100% rename from idea/testData/intentions/removeBracesFromWhenEntry/multiple.kt rename to idea/testData/intentions/removeBraces/whenMultiple.kt diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/noBraces.kt b/idea/testData/intentions/removeBraces/whenNoBraces.kt similarity index 100% rename from idea/testData/intentions/removeBracesFromWhenEntry/noBraces.kt rename to idea/testData/intentions/removeBraces/whenNoBraces.kt diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/simple.kt b/idea/testData/intentions/removeBraces/whenSimple.kt similarity index 100% rename from idea/testData/intentions/removeBracesFromWhenEntry/simple.kt rename to idea/testData/intentions/removeBraces/whenSimple.kt diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/simple.kt.after b/idea/testData/intentions/removeBraces/whenSimple.kt.after similarity index 100% rename from idea/testData/intentions/removeBracesFromWhenEntry/simple.kt.after rename to idea/testData/intentions/removeBraces/whenSimple.kt.after diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/statement.kt b/idea/testData/intentions/removeBraces/whenStatement.kt similarity index 100% rename from idea/testData/intentions/removeBracesFromWhenEntry/statement.kt rename to idea/testData/intentions/removeBraces/whenStatement.kt diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/.intention b/idea/testData/intentions/removeBracesFromWhenEntry/.intention deleted file mode 100644 index 7ec357c6ca6..00000000000 --- a/idea/testData/intentions/removeBracesFromWhenEntry/.intention +++ /dev/null @@ -1 +0,0 @@ -org.jetbrains.kotlin.idea.intentions.RemoveBracesFromWhenEntryIntention \ 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 c856f02746f..a9db44a18f4 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -9641,6 +9641,36 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("whenLambda.kt") + public void testWhenLambda() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/whenLambda.kt"); + doTest(fileName); + } + + @TestMetadata("whenMultiple.kt") + public void testWhenMultiple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/whenMultiple.kt"); + doTest(fileName); + } + + @TestMetadata("whenNoBraces.kt") + public void testWhenNoBraces() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/whenNoBraces.kt"); + doTest(fileName); + } + + @TestMetadata("whenSimple.kt") + public void testWhenSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/whenSimple.kt"); + doTest(fileName); + } + + @TestMetadata("whenStatement.kt") + public void testWhenStatement() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/whenStatement.kt"); + doTest(fileName); + } + @TestMetadata("while.kt") public void testWhile() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/while.kt"); @@ -9654,45 +9684,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest { } } - @TestMetadata("idea/testData/intentions/removeBracesFromWhenEntry") - @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class RemoveBracesFromWhenEntry extends AbstractIntentionTest { - public void testAllFilesPresentInRemoveBracesFromWhenEntry() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/removeBracesFromWhenEntry"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); - } - - @TestMetadata("lambda.kt") - public void testLambda() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBracesFromWhenEntry/lambda.kt"); - doTest(fileName); - } - - @TestMetadata("multiple.kt") - public void testMultiple() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBracesFromWhenEntry/multiple.kt"); - doTest(fileName); - } - - @TestMetadata("noBraces.kt") - public void testNoBraces() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBracesFromWhenEntry/noBraces.kt"); - doTest(fileName); - } - - @TestMetadata("simple.kt") - public void testSimple() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBracesFromWhenEntry/simple.kt"); - doTest(fileName); - } - - @TestMetadata("statement.kt") - public void testStatement() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBracesFromWhenEntry/statement.kt"); - doTest(fileName); - } - } - @TestMetadata("idea/testData/intentions/removeCurlyBracesFromTemplate") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)