IntentionBasedInspection minor refactoring: inspectionRange --> inspectionTarget

This commit is contained in:
Mikhail Glukhikh
2016-09-13 14:20:50 +03:00
parent e4c873dc6a
commit f973be22e2
5 changed files with 14 additions and 23 deletions
@@ -31,6 +31,9 @@ import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import com.intellij.util.SmartList import com.intellij.util.SmartList
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention 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 import kotlin.reflect.KClass
abstract class IntentionBasedInspection<TElement : PsiElement>( abstract class IntentionBasedInspection<TElement : PsiElement>(
@@ -64,7 +67,12 @@ abstract class IntentionBasedInspection<TElement : PsiElement>(
open fun additionalFixes(element: TElement): List<LocalQuickFix>? = null 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 { 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) { if (range != null) {
val allFixes = fixes ?: SmartList<LocalQuickFix>() val allFixes = fixes ?: SmartList<LocalQuickFix>()
additionalFixes(targetElement)?.let { allFixes.addAll(it) } additionalFixes(targetElement)?.let { allFixes.addAll(it) }
@@ -63,10 +63,7 @@ class HasPlatformTypeInspection(
return null return null
} }
override fun inspectionRange(element: KtCallableDeclaration) = element.nameIdentifier?.let { override fun inspectionTarget(element: KtCallableDeclaration) = element.nameIdentifier
val start = it.getStartOffsetIn(element)
TextRange(start, start + it.endOffset - it.startOffset)
}
override fun createOptionsPanel(): JComponent? { override fun createOptionsPanel(): JComponent? {
val panel = MultipleCheckboxOptionsPanel(this) val panel = MultipleCheckboxOptionsPanel(this)
@@ -44,10 +44,7 @@ class ConvertLambdaToReferenceInspection() : IntentionBasedInspection<KtLambdaEx
ConvertLambdaToReferenceIntention::class, ConvertLambdaToReferenceIntention::class,
{ it -> ConvertLambdaToReferenceIntention.shouldSuggestToConvert(it) } { it -> ConvertLambdaToReferenceIntention.shouldSuggestToConvert(it) }
) { ) {
override fun inspectionRange(element: KtLambdaExpression) = element.bodyExpression?.statements?.singleOrNull()?.let { override fun inspectionTarget(element: KtLambdaExpression) = element.bodyExpression?.statements?.singleOrNull()
val start = it.getStartOffsetIn(element)
TextRange(start, start + it.endOffset - it.startOffset)
}
} }
class ConvertLambdaToReferenceIntention : SelfTargetingOffsetIndependentIntention<KtLambdaExpression>( class ConvertLambdaToReferenceIntention : SelfTargetingOffsetIndependentIntention<KtLambdaExpression>(
@@ -32,10 +32,7 @@ class RemoveSetterParameterTypeInspection()
) { ) {
override val problemHighlightType = ProblemHighlightType.LIKE_UNUSED_SYMBOL override val problemHighlightType = ProblemHighlightType.LIKE_UNUSED_SYMBOL
override fun inspectionRange(element: KtCallableDeclaration) = (element as? KtParameter)?.typeReference?.let { override fun inspectionTarget(element: KtCallableDeclaration) = (element as? KtParameter)?.typeReference
val start = it.getStartOffsetIn(element)
TextRange(start, start + it.endOffset - it.startOffset)
}
} }
class RemoveExplicitTypeIntention : SelfTargetingRangeIntention<KtCallableDeclaration>( class RemoveExplicitTypeIntention : SelfTargetingRangeIntention<KtCallableDeclaration>(
@@ -17,23 +17,15 @@
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.openapi.util.TextRange
import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.lexer.KtToken
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.* 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.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
class ReplaceSingleLineLetInspection : IntentionBasedInspection<KtCallExpression>(ReplaceSingleLineLetIntention::class) { class ReplaceSingleLineLetInspection : IntentionBasedInspection<KtCallExpression>(ReplaceSingleLineLetIntention::class) {
override fun inspectionRange(element: KtCallExpression) = element.calleeExpression?.let { override fun inspectionTarget(element: KtCallExpression) = element.calleeExpression
val start = it.getStartOffsetIn(element)
TextRange(start, start + it.endOffset - it.startOffset)
}
} }
class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention<KtCallExpression>( class ReplaceSingleLineLetIntention : SelfTargetingOffsetIndependentIntention<KtCallExpression>(