Support global inspections in tests (KT-29549)
#KT-29549 Fixed
This commit is contained in:
@@ -17,6 +17,8 @@
|
|||||||
package org.jetbrains.kotlin.idea.inspections
|
package org.jetbrains.kotlin.idea.inspections
|
||||||
|
|
||||||
import com.intellij.analysis.AnalysisScope
|
import com.intellij.analysis.AnalysisScope
|
||||||
|
import com.intellij.codeInspection.GlobalInspectionTool
|
||||||
|
import com.intellij.codeInspection.InspectionProfileEntry
|
||||||
import com.intellij.codeInspection.LocalInspectionTool
|
import com.intellij.codeInspection.LocalInspectionTool
|
||||||
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper
|
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper
|
||||||
import com.intellij.codeInspection.ui.InspectionToolPresentation
|
import com.intellij.codeInspection.ui.InspectionToolPresentation
|
||||||
@@ -50,9 +52,21 @@ fun runInspection(
|
|||||||
inspectionClass: Class<*>, project: Project,
|
inspectionClass: Class<*>, project: Project,
|
||||||
settings: Element? = null, files: List<VirtualFile>? = null, withTestDir: String? = null
|
settings: Element? = null, files: List<VirtualFile>? = null, withTestDir: String? = null
|
||||||
): InspectionToolPresentation {
|
): 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) {
|
if (settings != null) {
|
||||||
inspection.readSettings(settings)
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user