IntentionBasedInspection minor refactoring: inspectionRange --> inspectionTarget
This commit is contained in:
+10
-2
@@ -31,6 +31,9 @@ import com.intellij.psi.PsiElementVisitor
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.util.SmartList
|
||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStartOffsetIn
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
abstract class IntentionBasedInspection<TElement : PsiElement>(
|
||||
@@ -64,7 +67,12 @@ abstract class IntentionBasedInspection<TElement : PsiElement>(
|
||||
|
||||
open fun additionalFixes(element: TElement): List<LocalQuickFix>? = null
|
||||
|
||||
open fun inspectionRange(element: TElement): TextRange? = null
|
||||
open fun inspectionTarget(element: TElement): PsiElement? = null
|
||||
|
||||
private fun PsiElement.toRange(baseElement: PsiElement): TextRange {
|
||||
val start = getStartOffsetIn(baseElement)
|
||||
return TextRange(start, start + endOffset - startOffset)
|
||||
}
|
||||
|
||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
|
||||
|
||||
@@ -100,7 +108,7 @@ abstract class IntentionBasedInspection<TElement : PsiElement>(
|
||||
}
|
||||
}
|
||||
|
||||
val range = inspectionRange(targetElement) ?: problemRange
|
||||
val range = inspectionTarget(targetElement)?.toRange(element) ?: problemRange
|
||||
if (range != null) {
|
||||
val allFixes = fixes ?: SmartList<LocalQuickFix>()
|
||||
additionalFixes(targetElement)?.let { allFixes.addAll(it) }
|
||||
|
||||
@@ -63,10 +63,7 @@ class HasPlatformTypeInspection(
|
||||
return null
|
||||
}
|
||||
|
||||
override fun inspectionRange(element: KtCallableDeclaration) = element.nameIdentifier?.let {
|
||||
val start = it.getStartOffsetIn(element)
|
||||
TextRange(start, start + it.endOffset - it.startOffset)
|
||||
}
|
||||
override fun inspectionTarget(element: KtCallableDeclaration) = element.nameIdentifier
|
||||
|
||||
override fun createOptionsPanel(): JComponent? {
|
||||
val panel = MultipleCheckboxOptionsPanel(this)
|
||||
|
||||
@@ -44,10 +44,7 @@ class ConvertLambdaToReferenceInspection() : IntentionBasedInspection<KtLambdaEx
|
||||
ConvertLambdaToReferenceIntention::class,
|
||||
{ it -> ConvertLambdaToReferenceIntention.shouldSuggestToConvert(it) }
|
||||
) {
|
||||
override fun inspectionRange(element: KtLambdaExpression) = element.bodyExpression?.statements?.singleOrNull()?.let {
|
||||
val start = it.getStartOffsetIn(element)
|
||||
TextRange(start, start + it.endOffset - it.startOffset)
|
||||
}
|
||||
override fun inspectionTarget(element: KtLambdaExpression) = element.bodyExpression?.statements?.singleOrNull()
|
||||
}
|
||||
|
||||
class ConvertLambdaToReferenceIntention : SelfTargetingOffsetIndependentIntention<KtLambdaExpression>(
|
||||
|
||||
@@ -32,10 +32,7 @@ class RemoveSetterParameterTypeInspection()
|
||||
) {
|
||||
override val problemHighlightType = ProblemHighlightType.LIKE_UNUSED_SYMBOL
|
||||
|
||||
override fun inspectionRange(element: KtCallableDeclaration) = (element as? KtParameter)?.typeReference?.let {
|
||||
val start = it.getStartOffsetIn(element)
|
||||
TextRange(start, start + it.endOffset - it.startOffset)
|
||||
}
|
||||
override fun inspectionTarget(element: KtCallableDeclaration) = (element as? KtParameter)?.typeReference
|
||||
}
|
||||
|
||||
class RemoveExplicitTypeIntention : SelfTargetingRangeIntention<KtCallableDeclaration>(
|
||||
|
||||
@@ -17,23 +17,15 @@
|
||||
package org.jetbrains.kotlin.idea.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
|
||||
import org.jetbrains.kotlin.lexer.KtToken
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStartOffsetIn
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
||||
|
||||
class ReplaceSingleLineLetInspection : IntentionBasedInspection<KtCallExpression>(ReplaceSingleLineLetIntention::class) {
|
||||
override fun inspectionRange(element: KtCallExpression) = element.calleeExpression?.let {
|
||||
val start = it.getStartOffsetIn(element)
|
||||
TextRange(start, start + it.endOffset - it.startOffset)
|
||||
}
|
||||
override fun inspectionTarget(element: KtCallExpression) = element.calleeExpression
|
||||
}
|
||||
|
||||
class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention<KtCallExpression>(
|
||||
|
||||
Reference in New Issue
Block a user