Restrict default applicability range for intentions to enclosing block
(cherry picked from commit bf2aade)
This commit is contained in:
committed by
Nikolay Krasko
parent
37492fcc78
commit
1e9db3c23e
+3
-1
@@ -29,6 +29,7 @@ import com.intellij.psi.PsiElement
|
|||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
import com.intellij.psi.util.PsiTreeUtil
|
import com.intellij.psi.util.PsiTreeUtil
|
||||||
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||||
|
import org.jetbrains.kotlin.psi.KtBlockExpression
|
||||||
import org.jetbrains.kotlin.psi.KtElement
|
import org.jetbrains.kotlin.psi.KtElement
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.containsInside
|
import org.jetbrains.kotlin.psi.psiUtil.containsInside
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||||
@@ -81,7 +82,8 @@ abstract class SelfTargetingIntention<TElement : PsiElement>(
|
|||||||
return null
|
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 {
|
final override fun isAvailable(project: Project, editor: Editor, file: PsiFile): Boolean {
|
||||||
val target = getTarget(editor, file) ?: return false
|
val target = getTarget(editor, file) ?: return false
|
||||||
|
|||||||
@@ -17,10 +17,10 @@
|
|||||||
package org.jetbrains.kotlin.idea.intentions
|
package org.jetbrains.kotlin.idea.intentions
|
||||||
|
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.psi.PsiElement
|
|
||||||
import com.intellij.psi.PsiWhiteSpace
|
import com.intellij.psi.PsiWhiteSpace
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
|
import java.lang.IllegalArgumentException
|
||||||
|
|
||||||
class AddBracesIntention : SelfTargetingIntention<KtExpression>(KtExpression::class.java, "Add braces") {
|
class AddBracesIntention : SelfTargetingIntention<KtExpression>(KtExpression::class.java, "Add braces") {
|
||||||
override fun isApplicableTo(element: KtExpression, caretOffset: Int): Boolean {
|
override fun isApplicableTo(element: KtExpression, caretOffset: Int): Boolean {
|
||||||
@@ -32,10 +32,6 @@ class AddBracesIntention : SelfTargetingIntention<KtExpression>(KtExpression::cl
|
|||||||
return true
|
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?) {
|
override fun applyTo(element: KtExpression, editor: Editor?) {
|
||||||
if (editor == null) throw IllegalArgumentException("This intention requires an editor")
|
if (editor == null) throw IllegalArgumentException("This intention requires an editor")
|
||||||
val expression = element.getTargetExpression(editor.caretModel.offset)!!
|
val expression = element.getTargetExpression(editor.caretModel.offset)!!
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ class AddNameToArgumentIntention
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun allowCaretInsideElement(element: PsiElement)
|
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?) {
|
override fun applyTo(element: KtValueArgument, editor: Editor?) {
|
||||||
val name = detectNameToAdd(element)!!
|
val name = detectNameToAdd(element)!!
|
||||||
|
|||||||
+4
-1
@@ -34,7 +34,10 @@ class ConvertPropertyInitializerToGetterIntention : SelfTargetingRangeIntention<
|
|||||||
return null
|
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?) {
|
override fun applyTo(element: KtProperty, editor: Editor?) {
|
||||||
convertPropertyInitializerToGetter(element, editor)
|
convertPropertyInitializerToGetter(element, editor)
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
|||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isNothing
|
import org.jetbrains.kotlin.types.typeUtil.isNothing
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
||||||
|
import java.lang.RuntimeException
|
||||||
|
|
||||||
class ConvertToBlockBodyIntention : SelfTargetingIntention<KtDeclarationWithBody>(
|
class ConvertToBlockBodyIntention : SelfTargetingIntention<KtDeclarationWithBody>(
|
||||||
KtDeclarationWithBody::class.java, "Convert to block body"
|
KtDeclarationWithBody::class.java, "Convert to block body"
|
||||||
@@ -47,7 +48,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?) {
|
override fun applyTo(element: KtDeclarationWithBody, editor: Editor?) {
|
||||||
convert(element)
|
convert(element)
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.kotlin.idea.intentions
|
package org.jetbrains.kotlin.idea.intentions
|
||||||
|
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.psi.KtBlockExpression
|
import org.jetbrains.kotlin.psi.KtBlockExpression
|
||||||
import org.jetbrains.kotlin.psi.KtNamedDeclaration
|
import org.jetbrains.kotlin.psi.KtNamedDeclaration
|
||||||
import org.jetbrains.kotlin.psi.KtWhenEntry
|
import org.jetbrains.kotlin.psi.KtWhenEntry
|
||||||
@@ -33,4 +34,8 @@ class RemoveBracesFromWhenEntryIntention : SelfTargetingIntention<KtWhenEntry>(K
|
|||||||
val block = element.expression as KtBlockExpression
|
val block = element.expression as KtBlockExpression
|
||||||
block.replace(block.statements.single())
|
block.replace(block.statements.single())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun allowCaretInsideElement(element: PsiElement): Boolean {
|
||||||
|
return element !is KtBlockExpression || element.parent is KtWhenEntry
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user