From e1ccd3afc57a56dd1d0f7fa584afa53ab6fcd8b0 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Thu, 5 Oct 2017 17:29:07 +0300 Subject: [PATCH] Inspection: do not report anything with manual INFORMATION level offline So #KT-20369 Fixed So #KT-20333 Fixed --- .../inspections/AbstractKotlinInspection.kt | 26 +++++++++++++++++++ .../inspections/IntentionBasedInspection.kt | 10 +++++-- .../LiftReturnOrAssignmentInspection.kt | 3 ++- .../NullableBooleanElvisInspection.kt | 11 +++++--- .../UseExpressionBodyInspection.kt | 7 +++-- 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/AbstractKotlinInspection.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/AbstractKotlinInspection.kt index f83e15a409e..1b6cf0da7d4 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/AbstractKotlinInspection.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/AbstractKotlinInspection.kt @@ -18,6 +18,8 @@ package org.jetbrains.kotlin.idea.inspections import com.intellij.codeHighlighting.HighlightDisplayLevel import com.intellij.codeInspection.* +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElement import org.jetbrains.kotlin.asJava.unwrapped import org.jetbrains.kotlin.caches.resolve.KotlinCacheService @@ -45,6 +47,30 @@ abstract class AbstractKotlinInspection: LocalInspectionTool(), CustomSuppressab } protected open val suppressionKey: String get() = this.shortName.removePrefix("Kotlin") + + protected fun ProblemsHolder.registerProblemWithoutOfflineInformation( + element: PsiElement, + description: String, + isOnTheFly: Boolean, + highlightType: ProblemHighlightType, + vararg fixes: LocalQuickFix + ) { + registerProblemWithoutOfflineInformation(element, description, isOnTheFly, highlightType, null, *fixes) + } + + protected fun ProblemsHolder.registerProblemWithoutOfflineInformation( + element: PsiElement, + description: String, + isOnTheFly: Boolean, + highlightType: ProblemHighlightType, + range: TextRange?, + vararg fixes: LocalQuickFix + ) { + if (!ApplicationManager.getApplication().isUnitTestMode && + !isOnTheFly && highlightType == ProblemHighlightType.INFORMATION) return + val problemDescriptor = manager.createProblemDescriptor(element, range, description, highlightType, isOnTheFly, *fixes) + registerProblem(problemDescriptor) + } } private fun toSeverity(highlightDisplayLevel: HighlightDisplayLevel): Severity { 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 f70f669f50c..9f025e66b4b 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 @@ -123,8 +123,14 @@ abstract class IntentionBasedInspection( val allFixes = fixes ?: SmartList() additionalFixes(targetElement)?.let { allFixes.addAll(it) } if (!allFixes.isEmpty()) { - holder.registerProblem(targetElement, problemText ?: allFixes.first().name, - problemHighlightType(targetElement), range, *allFixes.toTypedArray()) + holder.registerProblemWithoutOfflineInformation( + targetElement, + problemText ?: allFixes.first().name, + isOnTheFly, + problemHighlightType(targetElement), + range, + *allFixes.toTypedArray() + ) } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/LiftReturnOrAssignmentInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/LiftReturnOrAssignmentInspection.kt index 5446fb84ed0..5a69c7024ec 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/LiftReturnOrAssignmentInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/LiftReturnOrAssignmentInspection.kt @@ -48,9 +48,10 @@ class LiftReturnOrAssignmentInspection : AbstractKotlinInspection() { } val assignmentNumber = BranchedFoldingUtils.getFoldableAssignmentNumber(expression) if (assignmentNumber > 0) { - holder.registerProblem( + holder.registerProblemWithoutOfflineInformation( keyword, "Assignment can be lifted out of '${keyword.text}'", + isOnTheFly, if (assignmentNumber > 1) ProblemHighlightType.GENERIC_ERROR_OR_WARNING else ProblemHighlightType.INFORMATION, LiftAssignmentOutFix(keyword.text) diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/NullableBooleanElvisInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/NullableBooleanElvisInspection.kt index bebf323fec4..b9d121ac517 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/NullableBooleanElvisInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/NullableBooleanElvisInspection.kt @@ -55,10 +55,13 @@ class NullableBooleanElvisInspection : AbstractKotlinInspection(), CleanupLocalI else INFORMATION to "can" - holder.registerProblem(expression, - "Equality check $verb be used instead of elvis for nullable boolean check", - highlightType, - ReplaceWithEqualityCheckFix()) + holder.registerProblemWithoutOfflineInformation( + expression, + "Equality check $verb be used instead of elvis for nullable boolean check", + isOnTheFly, + highlightType, + ReplaceWithEqualityCheckFix() + ) } } } diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UseExpressionBodyInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UseExpressionBodyInspection.kt index 3d229042e40..b9f921a7893 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UseExpressionBodyInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UseExpressionBodyInspection.kt @@ -74,15 +74,14 @@ class UseExpressionBodyInspection(private val convertEmptyToUnit: Boolean) : Abs declaration as? KtDeclarationWithBody ?: return val (toHighlight, suffix, highlightType) = statusFor(declaration) ?: return - val problemDescriptor = holder.manager.createProblemDescriptor( + holder.registerProblemWithoutOfflineInformation( declaration, - toHighlight?.textRange?.shiftRight(-declaration.startOffset), "Use expression body instead of $suffix", - highlightType, isOnTheFly, + highlightType, + toHighlight?.textRange?.shiftRight(-declaration.startOffset), ConvertToExpressionBodyFix() ) - holder.registerProblem(problemDescriptor) } }