From 9afb0d5f592d7f9e5c292ca980f9053724e53981 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 11 May 2016 20:48:15 +0300 Subject: [PATCH] KT-12284 "Add braces to else" has too wide range #KT-12284 Fixed --- .../kotlin/idea/intentions/AddBracesIntention.kt | 5 +++++ .../intentions/addBraces/notInsideElseIfBlock.kt | 11 +++++++++++ .../idea/intentions/IntentionTestGenerated.java | 6 ++++++ 3 files changed, 22 insertions(+) create mode 100644 idea/testData/intentions/addBraces/notInsideElseIfBlock.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/AddBracesIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/AddBracesIntention.kt index 11f7f4b7da0..0fc7f1cb862 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/AddBracesIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/AddBracesIntention.kt @@ -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::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)!! diff --git a/idea/testData/intentions/addBraces/notInsideElseIfBlock.kt b/idea/testData/intentions/addBraces/notInsideElseIfBlock.kt new file mode 100644 index 00000000000..901cfd8d34b --- /dev/null +++ b/idea/testData/intentions/addBraces/notInsideElseIfBlock.kt @@ -0,0 +1,11 @@ +// IS_APPLICABLE: false +fun foo(p: Int) { + if (p == 1) { + + } + else if (p == 2) { + bar() + } +} + +fun bar(){} diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 4bdda45f572..1639fa4b582 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -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")