KT-12284 "Add braces to else" has too wide range

#KT-12284 Fixed
This commit is contained in:
Valentin Kipyatkov
2016-05-11 20:48:15 +03:00
parent 618b1b9a2a
commit 9afb0d5f59
3 changed files with 22 additions and 0 deletions
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.startOffset
@@ -31,6 +32,10 @@ class AddBracesIntention : SelfTargetingIntention<KtExpression>(KtExpression::cl
return true
}
override fun allowCaretInsideElement(element: PsiElement): Boolean {
return element !is KtBlockExpression // do not work inside another block to avoid confusion
}
override fun applyTo(element: KtExpression, editor: Editor?) {
if (editor == null) throw IllegalArgumentException("This intention requires an editor")
val expression = element.getTargetExpression(editor.caretModel.offset)!!
@@ -0,0 +1,11 @@
// IS_APPLICABLE: false
fun foo(p: Int) {
if (p == 1) {
}
else if (p == 2) {
bar()<caret>
}
}
fun bar(){}
@@ -90,6 +90,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
public void testAllFilesPresentInAddBraces() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addBraces"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("notInsideElseIfBlock.kt")
public void testNotInsideElseIfBlock() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addBraces/notInsideElseIfBlock.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/addConstModifier")