Refactoring: add braces to when entry intention is integrated into general add braces intention

This commit is contained in:
Mikhail Glukhikh
2016-09-08 19:40:16 +03:00
parent cbc7f72f15
commit 06b7881776
11 changed files with 33 additions and 85 deletions
@@ -1,5 +0,0 @@
when (a) {
1 -> <spot>{
b()
}</spot>
}
@@ -1,3 +0,0 @@
when (a) {
1 -> b()
}
@@ -1,5 +0,0 @@
<html>
<body>
This intention adds braces to 'when' entries without braces.
</body>
</html>
-5
View File
@@ -1320,11 +1320,6 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.AddBracesToWhenEntryIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ReplaceRangeToWithUntilIntention</className>
<category>Kotlin</category>
@@ -22,17 +22,29 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import java.lang.IllegalArgumentException
class AddBracesIntention : SelfTargetingIntention<KtExpression>(KtExpression::class.java, "Add braces") {
override fun isApplicableTo(element: KtExpression, caretOffset: Int): Boolean {
class AddBracesIntention : SelfTargetingIntention<KtElement>(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>(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
}
}
}
@@ -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>(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)) }
}
}
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.AddBracesToWhenEntryIntention
@@ -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);
}
}