Inspection: do not report anything with manual INFORMATION level offline

So #KT-20369 Fixed
So #KT-20333 Fixed
This commit is contained in:
Mikhail Glukhikh
2017-10-05 17:29:07 +03:00
committed by Mikhail Glukhikh
parent ec64a7e422
commit e1ccd3afc5
5 changed files with 46 additions and 11 deletions
@@ -18,6 +18,8 @@ package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeHighlighting.HighlightDisplayLevel import com.intellij.codeHighlighting.HighlightDisplayLevel
import com.intellij.codeInspection.* import com.intellij.codeInspection.*
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.asJava.unwrapped import org.jetbrains.kotlin.asJava.unwrapped
import org.jetbrains.kotlin.caches.resolve.KotlinCacheService 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 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 { private fun toSeverity(highlightDisplayLevel: HighlightDisplayLevel): Severity {
@@ -123,8 +123,14 @@ abstract class IntentionBasedInspection<TElement : PsiElement>(
val allFixes = fixes ?: SmartList<LocalQuickFix>() val allFixes = fixes ?: SmartList<LocalQuickFix>()
additionalFixes(targetElement)?.let { allFixes.addAll(it) } additionalFixes(targetElement)?.let { allFixes.addAll(it) }
if (!allFixes.isEmpty()) { if (!allFixes.isEmpty()) {
holder.registerProblem(targetElement, problemText ?: allFixes.first().name, holder.registerProblemWithoutOfflineInformation(
problemHighlightType(targetElement), range, *allFixes.toTypedArray()) targetElement,
problemText ?: allFixes.first().name,
isOnTheFly,
problemHighlightType(targetElement),
range,
*allFixes.toTypedArray()
)
} }
} }
} }
@@ -48,9 +48,10 @@ class LiftReturnOrAssignmentInspection : AbstractKotlinInspection() {
} }
val assignmentNumber = BranchedFoldingUtils.getFoldableAssignmentNumber(expression) val assignmentNumber = BranchedFoldingUtils.getFoldableAssignmentNumber(expression)
if (assignmentNumber > 0) { if (assignmentNumber > 0) {
holder.registerProblem( holder.registerProblemWithoutOfflineInformation(
keyword, keyword,
"Assignment can be lifted out of '${keyword.text}'", "Assignment can be lifted out of '${keyword.text}'",
isOnTheFly,
if (assignmentNumber > 1) ProblemHighlightType.GENERIC_ERROR_OR_WARNING if (assignmentNumber > 1) ProblemHighlightType.GENERIC_ERROR_OR_WARNING
else ProblemHighlightType.INFORMATION, else ProblemHighlightType.INFORMATION,
LiftAssignmentOutFix(keyword.text) LiftAssignmentOutFix(keyword.text)
@@ -55,10 +55,13 @@ class NullableBooleanElvisInspection : AbstractKotlinInspection(), CleanupLocalI
else else
INFORMATION to "can" INFORMATION to "can"
holder.registerProblem(expression, holder.registerProblemWithoutOfflineInformation(
"Equality check $verb be used instead of elvis for nullable boolean check", expression,
highlightType, "Equality check $verb be used instead of elvis for nullable boolean check",
ReplaceWithEqualityCheckFix()) isOnTheFly,
highlightType,
ReplaceWithEqualityCheckFix()
)
} }
} }
} }
@@ -74,15 +74,14 @@ class UseExpressionBodyInspection(private val convertEmptyToUnit: Boolean) : Abs
declaration as? KtDeclarationWithBody ?: return declaration as? KtDeclarationWithBody ?: return
val (toHighlight, suffix, highlightType) = statusFor(declaration) ?: return val (toHighlight, suffix, highlightType) = statusFor(declaration) ?: return
val problemDescriptor = holder.manager.createProblemDescriptor( holder.registerProblemWithoutOfflineInformation(
declaration, declaration,
toHighlight?.textRange?.shiftRight(-declaration.startOffset),
"Use expression body instead of $suffix", "Use expression body instead of $suffix",
highlightType,
isOnTheFly, isOnTheFly,
highlightType,
toHighlight?.textRange?.shiftRight(-declaration.startOffset),
ConvertToExpressionBodyFix() ConvertToExpressionBodyFix()
) )
holder.registerProblem(problemDescriptor)
} }
} }