Support global inspections in tests (KT-29549)

#KT-29549 Fixed
This commit is contained in:
Nikolay Krasko
2019-01-29 12:51:44 +03:00
parent 56d56ab842
commit b8bbe10086
@@ -17,6 +17,8 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.analysis.AnalysisScope
import com.intellij.codeInspection.GlobalInspectionTool
import com.intellij.codeInspection.InspectionProfileEntry
import com.intellij.codeInspection.LocalInspectionTool
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper
import com.intellij.codeInspection.ui.InspectionToolPresentation
@@ -50,9 +52,21 @@ fun runInspection(
inspectionClass: Class<*>, project: Project,
settings: Element? = null, files: List<VirtualFile>? = null, withTestDir: String? = null
): InspectionToolPresentation {
val inspection = inspectionClass.newInstance() as LocalInspectionTool
@Suppress("UNCHECKED_CAST")
val profileEntryClass = inspectionClass as Class<InspectionProfileEntry>
val inspection = InspectionTestUtil.instantiateTools(listOf(profileEntryClass)).singleOrNull()
?: error("Can't create `$inspectionClass` inspection")
if (settings != null) {
inspection.readSettings(settings)
}
return runInspection(inspection, project, files, withTestDir)
val localInspection = when (inspection) {
is LocalInspectionTool -> inspection
is GlobalInspectionTool -> inspection.sharedLocalInspectionTool ?: error("Global inspection ${inspection::class} without local counterpart")
else -> error("Unknown class for inspection instance")
}
return runInspection(localInspection, project, files, withTestDir)
}