Restrict default applicability range for intentions to enclosing block

This commit is contained in:
Nikolay Krasko
2016-08-31 19:51:07 +03:00
parent 533fca11c7
commit bf2aadea66
6 changed files with 14 additions and 8 deletions

View File

@@ -29,6 +29,7 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.psiUtil.containsInside
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
@@ -81,7 +82,8 @@ abstract class SelfTargetingIntention<TElement : PsiElement>(
return null
}
protected open fun allowCaretInsideElement(element: PsiElement): Boolean = true
protected open fun allowCaretInsideElement(element: PsiElement): Boolean =
element !is KtBlockExpression
final override fun isAvailable(project: Project, editor: Editor, file: PsiFile): Boolean {
val target = getTarget(editor, file) ?: return false

View File

@@ -32,10 +32,6 @@ 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)!!

View File

@@ -48,7 +48,7 @@ class AddNameToArgumentIntention
}
override fun allowCaretInsideElement(element: PsiElement)
= element !is KtValueArgumentList && element !is KtContainerNode
= element !is KtValueArgumentList && element !is KtContainerNode && super.allowCaretInsideElement(element)
override fun applyTo(element: KtValueArgument, editor: Editor?) {
val name = detectNameToAdd(element)!!

View File

@@ -34,7 +34,10 @@ class ConvertPropertyInitializerToGetterIntention : SelfTargetingRangeIntention<
return null
}
override fun allowCaretInsideElement(element: PsiElement) = element !is KtDeclaration // do not work inside lambda's in initializer - they can be too big
override fun allowCaretInsideElement(element: PsiElement): Boolean {
// do not work inside lambda's in initializer - they can be too big
return element !is KtDeclaration && super.allowCaretInsideElement(element)
}
override fun applyTo(element: KtProperty, editor: Editor?) {
convertPropertyInitializerToGetter(element, editor)

View File

@@ -47,7 +47,7 @@ class ConvertToBlockBodyIntention : SelfTargetingIntention<KtDeclarationWithBody
}
}
override fun allowCaretInsideElement(element: PsiElement) = element !is KtDeclaration
override fun allowCaretInsideElement(element: PsiElement) = element !is KtDeclaration && super.allowCaretInsideElement(element)
override fun applyTo(element: KtDeclarationWithBody, editor: Editor?) {
convert(element)

View File

@@ -17,6 +17,7 @@
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
@@ -33,4 +34,8 @@ class RemoveBracesFromWhenEntryIntention : SelfTargetingIntention<KtWhenEntry>(K
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
}
}