diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/IntentionBasedInspection.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/IntentionBasedInspection.kt index 842870ae247..d7c09014624 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/IntentionBasedInspection.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/IntentionBasedInspection.kt @@ -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( @@ -64,7 +67,12 @@ abstract class IntentionBasedInspection( open fun additionalFixes(element: TElement): List? = 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( } } - val range = inspectionRange(targetElement) ?: problemRange + val range = inspectionTarget(targetElement)?.toRange(element) ?: problemRange if (range != null) { val allFixes = fixes ?: SmartList() additionalFixes(targetElement)?.let { allFixes.addAll(it) } diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/HasPlatformTypeInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/HasPlatformTypeInspection.kt index 347d3cb0c4b..9022720e084 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/HasPlatformTypeInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/HasPlatformTypeInspection.kt @@ -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) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertLambdaToReferenceIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertLambdaToReferenceIntention.kt index 28351338c0f..e54169295ee 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertLambdaToReferenceIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertLambdaToReferenceIntention.kt @@ -44,10 +44,7 @@ class ConvertLambdaToReferenceInspection() : IntentionBasedInspection 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( diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt index 37a7a547d47..63d33daa8d2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt @@ -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( diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSingleLineLetIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSingleLineLetIntention.kt index 107e5fe906c..44566d6103f 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSingleLineLetIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ReplaceSingleLineLetIntention.kt @@ -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(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(