From 17c824f7f2f0fc776006c008871e076ca22699f3 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 26 Jul 2016 18:07:40 +0300 Subject: [PATCH] KT-13170 related: correct handling of inspection parameters in additional checker (cherry picked from commit 47fd990) --- .../inspections/IntentionBasedInspection.kt | 26 ++++++++++++++----- .../idea/intentions/SelfTargetingIntention.kt | 2 +- .../inspections/HasPlatformTypeInspection.kt | 6 ++++- .../ConvertLambdaToReferenceIntention.kt | 2 +- .../ConvertToStringTemplateIntention.kt | 2 +- .../intentions/RemoveExplicitTypeIntention.kt | 2 +- 6 files changed, 28 insertions(+), 12 deletions(-) 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 7890558dd0e..f8df4c2c88a 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 @@ -36,12 +36,24 @@ abstract class IntentionBasedInspection( protected val elementType: Class ) : AbstractKotlinInspection() { - constructor(intention: SelfTargetingRangeIntention, additionalChecker: (TElement) -> Boolean = { true }) - : this(listOf(IntentionData(intention, additionalChecker)), null, intention.elementType) + constructor( + intention: SelfTargetingRangeIntention + ) : this(listOf(IntentionData(intention)), null, intention.elementType) + + constructor( + intention: SelfTargetingRangeIntention, + additionalChecker: (TElement, IntentionBasedInspection) -> Boolean + ) : this(listOf(IntentionData(intention, additionalChecker)), null, intention.elementType) + + constructor( + intention: SelfTargetingRangeIntention, + additionalChecker: (TElement) -> Boolean + ) : this(listOf(IntentionData(intention, { element, inspection -> additionalChecker(element) } )), null, intention.elementType) + data class IntentionData( val intention: SelfTargetingRangeIntention, - val additionalChecker: (TElement) -> Boolean = { true } + val additionalChecker: (TElement, IntentionBasedInspection) -> Boolean = { element, inspection -> true } ) open fun additionalFixes(element: TElement): List? = null @@ -67,7 +79,7 @@ abstract class IntentionBasedInspection( range.shiftRight(-elementRange.startOffset) } - if (range != null && additionalChecker(targetElement)) { + if (range != null && additionalChecker(targetElement, this@IntentionBasedInspection)) { problemRange = problemRange?.union(range) ?: range if (fixes == null) { fixes = SmartList() @@ -94,10 +106,10 @@ abstract class IntentionBasedInspection( get() = ProblemHighlightType.GENERIC_ERROR_OR_WARNING /* we implement IntentionAction to provide isAvailable which will be used to hide outdated items and make sure we never call 'invoke' for such item */ - private class IntentionBasedQuickFix( + private inner class IntentionBasedQuickFix( private val intention: SelfTargetingRangeIntention, private val text: String, - private val additionalChecker: (TElement) -> Boolean, + private val additionalChecker: (TElement, IntentionBasedInspection) -> Boolean, targetElement: TElement ) : LocalQuickFixOnPsiElement(targetElement), IntentionAction { @@ -112,7 +124,7 @@ abstract class IntentionBasedInspection( override fun isAvailable(project: Project, file: PsiFile, startElement: PsiElement, endElement: PsiElement): Boolean { assert(startElement == endElement) - return intention.applicabilityRange(startElement as TElement) != null && additionalChecker(startElement) + return intention.applicabilityRange(startElement as TElement) != null && additionalChecker(startElement, this@IntentionBasedInspection) } override fun invoke(project: Project, editor: Editor?, file: PsiFile?) { diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt index 4f0ab4491a0..128ec4bf1e3 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/intentions/SelfTargetingIntention.kt @@ -95,7 +95,7 @@ abstract class SelfTargetingIntention( return false } - return inspection.intentions.single { it.intention.javaClass == javaClass }.additionalChecker(target) + return inspection.intentions.single { it.intention.javaClass == javaClass }.additionalChecker(target, inspection) } final override fun invoke(project: Project, editor: Editor, file: PsiFile): Unit { diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/HasPlatformTypeInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/HasPlatformTypeInspection.kt index 64c74a1f0b5..c1bc2291974 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/HasPlatformTypeInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/HasPlatformTypeInspection.kt @@ -38,7 +38,11 @@ class HasPlatformTypeInspection( @JvmField var reportPlatformArguments: Boolean = false ) : IntentionBasedInspection( intention, - { intention.dangerousFlexibleTypeOrNull(it, publicAPIOnly, reportPlatformArguments) != null } + { element, inspection -> + with(inspection as HasPlatformTypeInspection) { + intention.dangerousFlexibleTypeOrNull(element, this.publicAPIOnly, this.reportPlatformArguments) != null + } + } ) { override val problemHighlightType = ProblemHighlightType.WEAK_WARNING diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertLambdaToReferenceIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertLambdaToReferenceIntention.kt index 7e2aa493680..820799b139e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertLambdaToReferenceIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertLambdaToReferenceIntention.kt @@ -36,7 +36,7 @@ class ConvertLambdaToReferenceInspection( val intention: ConvertLambdaToReferenceIntention = ConvertLambdaToReferenceIntention() ) : IntentionBasedInspection( intention, - { intention.shouldSuggestToConvert(it) } + { it -> intention.shouldSuggestToConvert(it) } ) class ConvertLambdaToReferenceIntention : SelfTargetingOffsetIndependentIntention( diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt index 75ff4d8e246..8e0a6969d82 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt @@ -30,7 +30,7 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode class ConvertToStringTemplateInspection : IntentionBasedInspection( ConvertToStringTemplateIntention(), - { ConvertToStringTemplateIntention().shouldSuggestToConvert(it) } + { it -> ConvertToStringTemplateIntention().shouldSuggestToConvert(it) } ) class ConvertToStringTemplateIntention : SelfTargetingOffsetIndependentIntention(KtBinaryExpression::class.java, "Convert concatenation to template") { diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt index b581f5e2fd0..38d0cae74dc 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/RemoveExplicitTypeIntention.kt @@ -29,7 +29,7 @@ class RemoveSetterParameterTypeInspection( val intention: RemoveExplicitTypeIntention = RemoveExplicitTypeIntention() ) : IntentionBasedInspection( intention, - { intention.isSetterParameter(it) } + { it -> intention.isSetterParameter(it) } ) { override val problemHighlightType = ProblemHighlightType.LIKE_UNUSED_SYMBOL