Refactoring: remove braces from when entry intention is integrated into general remove braces intention
This commit is contained in:
-3
@@ -1,3 +0,0 @@
|
|||||||
when (a) {
|
|
||||||
1 -> <spot>b()</spot>
|
|
||||||
}
|
|
||||||
-5
@@ -1,5 +0,0 @@
|
|||||||
when (a) {
|
|
||||||
1 -> {
|
|
||||||
b()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
-5
@@ -1,5 +0,0 @@
|
|||||||
<html>
|
|
||||||
<body>
|
|
||||||
This intention removes braces from 'when' entries with only a single expression.
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1325,11 +1325,6 @@
|
|||||||
<category>Kotlin</category>
|
<category>Kotlin</category>
|
||||||
</intentionAction>
|
</intentionAction>
|
||||||
|
|
||||||
<intentionAction>
|
|
||||||
<className>org.jetbrains.kotlin.idea.intentions.RemoveBracesFromWhenEntryIntention</className>
|
|
||||||
<category>Kotlin</category>
|
|
||||||
</intentionAction>
|
|
||||||
|
|
||||||
<intentionAction>
|
<intentionAction>
|
||||||
<className>org.jetbrains.kotlin.idea.intentions.ReplaceRangeToWithUntilIntention</className>
|
<className>org.jetbrains.kotlin.idea.intentions.ReplaceRangeToWithUntilIntention</className>
|
||||||
<category>Kotlin</category>
|
<category>Kotlin</category>
|
||||||
|
|||||||
@@ -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>(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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -24,24 +24,32 @@ import org.jetbrains.kotlin.psi.*
|
|||||||
class RemoveBracesIntention : SelfTargetingIntention<KtBlockExpression>(KtBlockExpression::class.java, "Remove braces") {
|
class RemoveBracesIntention : SelfTargetingIntention<KtBlockExpression>(KtBlockExpression::class.java, "Remove braces") {
|
||||||
override fun isApplicableTo(element: KtBlockExpression, caretOffset: Int): Boolean {
|
override fun isApplicableTo(element: KtBlockExpression, caretOffset: Int): Boolean {
|
||||||
val singleStatement = element.statements.singleOrNull() ?: return false
|
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
|
val lBrace = element.lBrace ?: return false
|
||||||
if (singleStatement is KtIfExpression && containerNode.parent is KtIfExpression) 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 description = container.description() ?: return false
|
||||||
val rBrace = element.rBrace ?: return false
|
text = "Remove braces from '$description' statement"
|
||||||
if (!lBrace.textRange.containsOffset(caretOffset) && !rBrace.textRange.containsOffset(caretOffset)) return false
|
return true
|
||||||
|
}
|
||||||
val description = containerNode.description() ?: return false
|
is KtWhenEntry -> {
|
||||||
text = "Remove braces from '$description' statement"
|
text = "Remove braces from 'when' entry"
|
||||||
return true
|
return singleStatement !is KtNamedDeclaration
|
||||||
|
}
|
||||||
|
else -> return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtBlockExpression, editor: Editor?) {
|
override fun applyTo(element: KtBlockExpression, editor: Editor?) {
|
||||||
val statement = element.statements.single()
|
val statement = element.statements.single()
|
||||||
|
|
||||||
val containerNode = element.parent as KtContainerNode
|
val container = element.parent!!
|
||||||
val construct = containerNode.parent as KtExpression
|
val construct = container.parent as KtExpression
|
||||||
handleComments(construct, element)
|
handleComments(construct, element)
|
||||||
|
|
||||||
val newElement = element.replace(statement.copy())
|
val newElement = element.replace(statement.copy())
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
org.jetbrains.kotlin.idea.intentions.RemoveBracesFromWhenEntryIntention
|
|
||||||
@@ -9641,6 +9641,36 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("while.kt")
|
||||||
public void testWhile() throws Exception {
|
public void testWhile() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/removeBraces/while.kt");
|
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")
|
@TestMetadata("idea/testData/intentions/removeCurlyBracesFromTemplate")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user