diff --git a/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/after.kt.template b/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/after.kt.template deleted file mode 100644 index 1496c30d93e..00000000000 --- a/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/after.kt.template +++ /dev/null @@ -1,5 +0,0 @@ -when (a) { - 1 -> { - b() - } -} diff --git a/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/before.kt.template b/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/before.kt.template deleted file mode 100644 index f1469731c74..00000000000 --- a/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/before.kt.template +++ /dev/null @@ -1,3 +0,0 @@ -when (a) { - 1 -> b() -} diff --git a/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/description.html b/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/description.html deleted file mode 100644 index 854d26a6b5c..00000000000 --- a/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/description.html +++ /dev/null @@ -1,5 +0,0 @@ - - -This intention adds braces to 'when' entries without braces. - - \ No newline at end of file diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 7af8f40266d..091ccb07953 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -1320,11 +1320,6 @@ Kotlin - - org.jetbrains.kotlin.idea.intentions.AddBracesToWhenEntryIntention - Kotlin - - org.jetbrains.kotlin.idea.intentions.ReplaceRangeToWithUntilIntention Kotlin diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddBracesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddBracesIntention.kt index 059656a322c..f53a7499f86 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddBracesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddBracesIntention.kt @@ -22,17 +22,29 @@ import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.startOffset import java.lang.IllegalArgumentException -class AddBracesIntention : SelfTargetingIntention(KtExpression::class.java, "Add braces") { - override fun isApplicableTo(element: KtExpression, caretOffset: Int): Boolean { +class AddBracesIntention : SelfTargetingIntention(KtElement::class.java, "Add braces") { + override fun isApplicableTo(element: KtElement, caretOffset: Int): Boolean { val expression = element.getTargetExpression(caretOffset) ?: return false if (expression is KtBlockExpression) return false - val description = (expression.parent as KtContainerNode).description()!! - text = "Add braces to '$description' statement" - return true + val parent = expression.parent + when (parent) { + is KtContainerNode -> { + val description = parent.description()!! + text = "Add braces to '$description' statement" + return true + } + is KtWhenEntry -> { + text = "Add braces to 'when' entry" + return true + } + else -> { + return false + } + } } - override fun applyTo(element: KtExpression, editor: Editor?) { + override fun applyTo(element: KtElement, editor: Editor?) { if (editor == null) throw IllegalArgumentException("This intention requires an editor") val expression = element.getTargetExpression(editor.caretModel.offset)!! @@ -48,24 +60,22 @@ class AddBracesIntention : SelfTargetingIntention(KtExpression::cl } } - private fun KtExpression.getTargetExpression(caretLocation: Int): KtExpression? { - when (this) { + private fun KtElement.getTargetExpression(caretLocation: Int): KtExpression? { + return when (this) { is KtIfExpression -> { val thenExpr = then ?: return null val elseExpr = `else` if (elseExpr != null && caretLocation >= elseKeyword!!.startOffset) { - return elseExpr + elseExpr + } + else { + thenExpr } - return thenExpr } - is KtWhileExpression -> return body - - is KtDoWhileExpression -> return body - - is KtForExpression -> return body - - else -> return null + is KtLoopExpression -> body + is KtWhenEntry -> expression + else -> null } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddBracesToWhenEntryIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddBracesToWhenEntryIntention.kt deleted file mode 100644 index 65e85497a73..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddBracesToWhenEntryIntention.kt +++ /dev/null @@ -1,34 +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 org.jetbrains.kotlin.psi.KtBlockExpression -import org.jetbrains.kotlin.psi.KtPsiFactory -import org.jetbrains.kotlin.psi.KtWhenEntry - -class AddBracesToWhenEntryIntention : SelfTargetingIntention(KtWhenEntry::class.java, "Add braces to 'when' entry") { - - override fun isApplicableTo(element: KtWhenEntry, caretOffset: Int): Boolean { - return element.expression !is KtBlockExpression - } - - override fun applyTo(element: KtWhenEntry, editor: Editor?) { - val factory = KtPsiFactory(element) - element.expression?.let { it.replace(factory.createBlock(it.text)) } - } -} diff --git a/idea/testData/intentions/addBracesToWhenEntry/hasBraces.kt b/idea/testData/intentions/addBraces/whenHasBraces.kt similarity index 100% rename from idea/testData/intentions/addBracesToWhenEntry/hasBraces.kt rename to idea/testData/intentions/addBraces/whenHasBraces.kt diff --git a/idea/testData/intentions/addBracesToWhenEntry/simple.kt b/idea/testData/intentions/addBraces/whenSimple.kt similarity index 100% rename from idea/testData/intentions/addBracesToWhenEntry/simple.kt rename to idea/testData/intentions/addBraces/whenSimple.kt diff --git a/idea/testData/intentions/addBracesToWhenEntry/simple.kt.after b/idea/testData/intentions/addBraces/whenSimple.kt.after similarity index 100% rename from idea/testData/intentions/addBracesToWhenEntry/simple.kt.after rename to idea/testData/intentions/addBraces/whenSimple.kt.after diff --git a/idea/testData/intentions/addBracesToWhenEntry/.intention b/idea/testData/intentions/addBracesToWhenEntry/.intention deleted file mode 100644 index 33d8bf024f8..00000000000 --- a/idea/testData/intentions/addBracesToWhenEntry/.intention +++ /dev/null @@ -1 +0,0 @@ -org.jetbrains.kotlin.idea.intentions.AddBracesToWhenEntryIntention \ 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 f172297394b..d3d37eedc6a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -96,25 +96,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addBraces/notInsideElseIfBlock.kt"); doTest(fileName); } - } - @TestMetadata("idea/testData/intentions/addBracesToWhenEntry") - @TestDataPath("$PROJECT_ROOT") - @RunWith(JUnit3RunnerWithInners.class) - public static class AddBracesToWhenEntry extends AbstractIntentionTest { - public void testAllFilesPresentInAddBracesToWhenEntry() throws Exception { - KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addBracesToWhenEntry"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); - } - - @TestMetadata("hasBraces.kt") - public void testHasBraces() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addBracesToWhenEntry/hasBraces.kt"); + @TestMetadata("whenHasBraces.kt") + public void testWhenHasBraces() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addBraces/whenHasBraces.kt"); doTest(fileName); } - @TestMetadata("simple.kt") - public void testSimple() throws Exception { - String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addBracesToWhenEntry/simple.kt"); + @TestMetadata("whenSimple.kt") + public void testWhenSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addBraces/whenSimple.kt"); doTest(fileName); } }