Implement intentions to add/remove braces to/from when entries #KT-12043 Fixed

This commit is contained in:
Kirill
2016-08-15 00:20:34 +03:00
committed by Mikhail Glukhikh
parent d68a681db5
commit 223fd9fad0
21 changed files with 239 additions and 0 deletions
@@ -0,0 +1,5 @@
when (a) {
1 -> <spot>{
b()
}</spot>
}
@@ -0,0 +1,3 @@
when (a) {
1 -> b()
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention adds braces to 'when' entries without braces.
</body>
</html>
@@ -0,0 +1,3 @@
when (a) {
1 -> <spot>b()</spot>
}
@@ -0,0 +1,5 @@
when (a) {
1 -> {
b()
}
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention removes braces from 'when' entries with only a single expression.
</body>
</html>
+10
View File
@@ -1283,6 +1283,16 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.AddBracesToWhenEntryIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveBracesFromWhenEntryIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupName="Kotlin"
@@ -0,0 +1,34 @@
/*
* 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>(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)) }
}
}
@@ -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>(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())
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.AddBracesToWhenEntryIntention
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
fun foo(a: Int) {
when (a) {
1 -> {
foo(a)<caret>
}
}
}
@@ -0,0 +1,5 @@
fun foo(a: Int) {
when (a) {
1 -> foo(a)<caret>
}
}
@@ -0,0 +1,7 @@
fun foo(a: Int) {
when (a) {
1 -> {
foo(a)
}<caret>
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.RemoveBracesFromWhenEntryIntention
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
fun foo() {
when (1) {
else -> {
<caret>it: Int -> it.hashCode()
}
}
}
@@ -0,0 +1,13 @@
// IS_APPLICABLE: false
fun bar() {}
fun gav() {}
fun foo() {
when (1) {
else -> {
bar()<caret>
gav()
}
}
}
@@ -0,0 +1,7 @@
// IS_APPLICABLE: false
fun foo() {
when (1) {
else -> foo()<caret>
}
}
@@ -0,0 +1,7 @@
fun foo() {
when (1) {
else -> {
foo()<caret>
}
}
}
@@ -0,0 +1,5 @@
fun foo() {
when (1) {
else -> foo()<caret>
}
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
fun foo() {
when (1) {
else -> {
val a = 1<caret>
}
}
}
@@ -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)