diff --git a/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/after.kt.template b/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/after.kt.template new file mode 100644 index 00000000000..1496c30d93e --- /dev/null +++ b/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/after.kt.template @@ -0,0 +1,5 @@ +when (a) { + 1 -> { + b() + } +} diff --git a/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/before.kt.template b/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/before.kt.template new file mode 100644 index 00000000000..f1469731c74 --- /dev/null +++ b/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/before.kt.template @@ -0,0 +1,3 @@ +when (a) { + 1 -> b() +} diff --git a/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/description.html b/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/description.html new file mode 100644 index 00000000000..854d26a6b5c --- /dev/null +++ b/idea/resources/intentionDescriptions/AddBracesToWhenEntryIntention/description.html @@ -0,0 +1,5 @@ + + +This intention adds braces to 'when' entries without braces. + + \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/after.kt.template b/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/after.kt.template new file mode 100644 index 00000000000..99b515f4498 --- /dev/null +++ b/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/after.kt.template @@ -0,0 +1,3 @@ +when (a) { + 1 -> b() +} diff --git a/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/before.kt.template b/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/before.kt.template new file mode 100644 index 00000000000..57cbc88c577 --- /dev/null +++ b/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/before.kt.template @@ -0,0 +1,5 @@ +when (a) { + 1 -> { + b() + } +} diff --git a/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/description.html b/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/description.html new file mode 100644 index 00000000000..b498f4c3b5f --- /dev/null +++ b/idea/resources/intentionDescriptions/RemoveBracesFromWhenEntryIntention/description.html @@ -0,0 +1,5 @@ + + +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 58d29165166..cbb88e978e9 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -1283,6 +1283,16 @@ Kotlin + + org.jetbrains.kotlin.idea.intentions.AddBracesToWhenEntryIntention + Kotlin + + + + org.jetbrains.kotlin.idea.intentions.RemoveBracesFromWhenEntryIntention + Kotlin + + (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/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesFromWhenEntryIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesFromWhenEntryIntention.kt new file mode 100644 index 00000000000..9c8f67093d2 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveBracesFromWhenEntryIntention.kt @@ -0,0 +1,36 @@ +/* + * 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.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()) + } +} diff --git a/idea/testData/intentions/addBracesToWhenEntry/.intention b/idea/testData/intentions/addBracesToWhenEntry/.intention new file mode 100644 index 00000000000..33d8bf024f8 --- /dev/null +++ b/idea/testData/intentions/addBracesToWhenEntry/.intention @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.intentions.AddBracesToWhenEntryIntention \ No newline at end of file diff --git a/idea/testData/intentions/addBracesToWhenEntry/hasBraces.kt b/idea/testData/intentions/addBracesToWhenEntry/hasBraces.kt new file mode 100644 index 00000000000..7364e01ac1f --- /dev/null +++ b/idea/testData/intentions/addBracesToWhenEntry/hasBraces.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false + +fun foo(a: Int) { + when (a) { + 1 -> { + foo(a) + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addBracesToWhenEntry/simple.kt b/idea/testData/intentions/addBracesToWhenEntry/simple.kt new file mode 100644 index 00000000000..fec91174dbf --- /dev/null +++ b/idea/testData/intentions/addBracesToWhenEntry/simple.kt @@ -0,0 +1,5 @@ +fun foo(a: Int) { + when (a) { + 1 -> foo(a) + } +} \ No newline at end of file diff --git a/idea/testData/intentions/addBracesToWhenEntry/simple.kt.after b/idea/testData/intentions/addBracesToWhenEntry/simple.kt.after new file mode 100644 index 00000000000..60f51a610a5 --- /dev/null +++ b/idea/testData/intentions/addBracesToWhenEntry/simple.kt.after @@ -0,0 +1,7 @@ +fun foo(a: Int) { + when (a) { + 1 -> { + foo(a) + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/.intention b/idea/testData/intentions/removeBracesFromWhenEntry/.intention new file mode 100644 index 00000000000..7ec357c6ca6 --- /dev/null +++ b/idea/testData/intentions/removeBracesFromWhenEntry/.intention @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.intentions.RemoveBracesFromWhenEntryIntention \ No newline at end of file diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/lambda.kt b/idea/testData/intentions/removeBracesFromWhenEntry/lambda.kt new file mode 100644 index 00000000000..b05610581d0 --- /dev/null +++ b/idea/testData/intentions/removeBracesFromWhenEntry/lambda.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false + +fun foo() { + when (1) { + else -> { + it: Int -> it.hashCode() + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/multiple.kt b/idea/testData/intentions/removeBracesFromWhenEntry/multiple.kt new file mode 100644 index 00000000000..45b10a7082d --- /dev/null +++ b/idea/testData/intentions/removeBracesFromWhenEntry/multiple.kt @@ -0,0 +1,13 @@ +// IS_APPLICABLE: false + +fun bar() {} +fun gav() {} + +fun foo() { + when (1) { + else -> { + bar() + gav() + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/noBraces.kt b/idea/testData/intentions/removeBracesFromWhenEntry/noBraces.kt new file mode 100644 index 00000000000..1aaa5d3eed7 --- /dev/null +++ b/idea/testData/intentions/removeBracesFromWhenEntry/noBraces.kt @@ -0,0 +1,7 @@ +// IS_APPLICABLE: false + +fun foo() { + when (1) { + else -> foo() + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/simple.kt b/idea/testData/intentions/removeBracesFromWhenEntry/simple.kt new file mode 100644 index 00000000000..d43e27f6c62 --- /dev/null +++ b/idea/testData/intentions/removeBracesFromWhenEntry/simple.kt @@ -0,0 +1,7 @@ +fun foo() { + when (1) { + else -> { + foo() + } + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/simple.kt.after b/idea/testData/intentions/removeBracesFromWhenEntry/simple.kt.after new file mode 100644 index 00000000000..da31736d093 --- /dev/null +++ b/idea/testData/intentions/removeBracesFromWhenEntry/simple.kt.after @@ -0,0 +1,5 @@ +fun foo() { + when (1) { + else -> foo() + } +} \ No newline at end of file diff --git a/idea/testData/intentions/removeBracesFromWhenEntry/statement.kt b/idea/testData/intentions/removeBracesFromWhenEntry/statement.kt new file mode 100644 index 00000000000..beb1067921a --- /dev/null +++ b/idea/testData/intentions/removeBracesFromWhenEntry/statement.kt @@ -0,0 +1,9 @@ +// IS_APPLICABLE: false + +fun foo() { + when (1) { + else -> { + val a = 1 + } + } +} \ 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 fbc0b079e2c..2386618202f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -98,6 +98,27 @@ public class IntentionTestGenerated extends AbstractIntentionTest { } } + @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"); + doTest(fileName); + } + + @TestMetadata("simple.kt") + public void testSimple() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addBracesToWhenEntry/simple.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/intentions/addConstModifier") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -8094,6 +8115,45 @@ 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)