2 * Copyright 2000-2016 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.testFramework
18 import com.intellij.analysis.AnalysisScope
19 import com.intellij.codeInsight.daemon.HighlightDisplayKey
20 import com.intellij.codeInspection.InspectionProfileEntry
21 import com.intellij.codeInspection.LocalInspectionTool
22 import com.intellij.codeInspection.ex.*
23 import com.intellij.openapi.Disposable
24 import com.intellij.openapi.project.Project
25 import com.intellij.openapi.util.Disposer
26 import com.intellij.profile.codeInspection.ProjectInspectionProfileManager
27 import com.intellij.testFramework.fixtures.impl.GlobalInspectionContextForTests
28 import com.intellij.util.ReflectionUtil
29 import gnu.trove.THashMap
30 import org.jetbrains.annotations.TestOnly
33 fun configureInspections(tools: Array<InspectionProfileEntry>,
35 parentDisposable: Disposable): InspectionProfileImpl {
36 val profile = InspectionProfileImpl.createSimple(UUID.randomUUID().toString(), project, tools.map { InspectionToolRegistrar.wrapTool(it) })
37 val profileManager = ProjectInspectionProfileManager.getInstanceImpl(project)
38 // we don't restore old project profile because in tests it must be in any case null - app default profile
39 Disposer.register(parentDisposable, Disposable {
40 profileManager.deleteProfile(profile)
41 profileManager.setCurrentProfile(null)
42 clearAllToolsIn(InspectionProfileImpl.getDefaultProfile())
46 profileManager.addProfile(profile)
47 profile.initInspectionTools(project)
48 profileManager.setCurrentProfile(profile)
55 fun createGlobalContextForTool(scope: AnalysisScope,
57 toolWrappers: List<InspectionToolWrapper<*, *>> = emptyList()): GlobalInspectionContextForTests {
59 val profile = InspectionProfileImpl.createSimple("test", project, toolWrappers)
60 val context = object : GlobalInspectionContextForTests(project, (InspectionManagerEx.getInstance(project) as InspectionManagerEx).contentManager) {
61 override fun getUsedTools(): List<Tools> {
62 for (tool in toolWrappers) {
63 profile.enableTool(tool.shortName, project)
65 return profile.getAllEnabledInspectionTools(project)
68 context.currentScope = scope
73 private fun clearAllToolsIn(profile: InspectionProfileImpl) {
74 if (!profile.wasInitialized()) {
78 for (state in profile.getAllTools(null)) {
79 val wrapper = state.tool
80 if (wrapper.extension != null) {
81 // make it not initialized
82 ReflectionUtil.resetField(wrapper, InspectionProfileEntry::class.java, "myTool")
87 fun ProjectInspectionProfileManager.createProfile(localInspectionTool: LocalInspectionTool, disposable: Disposable): InspectionProfileImpl {
88 return configureInspections(arrayOf(localInspectionTool), project, disposable)
91 fun enableInspectionTool(project: Project, tool: InspectionProfileEntry, disposable: Disposable) = enableInspectionTool(project, InspectionToolRegistrar.wrapTool(tool), disposable)
93 private fun enableInspectionTool(project: Project, toolWrapper: InspectionToolWrapper<*, *>, disposable: Disposable) {
94 val profile = ProjectInspectionProfileManager.getInstanceImpl(project).currentProfile
95 val shortName = toolWrapper.shortName
96 val key = HighlightDisplayKey.find(shortName)
98 HighlightDisplayKey.register(shortName, toolWrapper.displayName, toolWrapper.id)
102 val existingWrapper = profile.getInspectionTool(shortName, project)
103 if (existingWrapper == null || existingWrapper.isInitialized != toolWrapper.isInitialized || toolWrapper.isInitialized && toolWrapper.tool !== existingWrapper.tool) {
104 profile.addTool(project, toolWrapper, THashMap<String, List<String>>())
106 profile.enableTool(shortName, project)
108 Disposer.register(disposable, Disposable { profile.disableTool(shortName, project) })
112 inline fun <T> runInInitMode(runnable: () -> T): T {
113 val old = InspectionProfileImpl.INIT_INSPECTIONS
115 InspectionProfileImpl.INIT_INSPECTIONS = true
119 InspectionProfileImpl.INIT_INSPECTIONS = old