2 * Copyright 2000-2009 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.
23 package com
.intellij
.codeInspection
.ex
;
25 import com
.intellij
.codeInspection
.*;
26 import com
.intellij
.codeInspection
.lang
.InspectionExtensionsFactory
;
27 import com
.intellij
.ide
.impl
.ContentManagerWatcher
;
28 import com
.intellij
.openapi
.application
.ApplicationManager
;
29 import com
.intellij
.openapi
.extensions
.Extensions
;
30 import com
.intellij
.openapi
.project
.Project
;
31 import com
.intellij
.openapi
.util
.IconLoader
;
32 import com
.intellij
.openapi
.util
.NotNullLazyValue
;
33 import com
.intellij
.openapi
.util
.TextRange
;
34 import com
.intellij
.openapi
.wm
.ToolWindow
;
35 import com
.intellij
.openapi
.wm
.ToolWindowAnchor
;
36 import com
.intellij
.openapi
.wm
.ToolWindowId
;
37 import com
.intellij
.openapi
.wm
.ToolWindowManager
;
38 import com
.intellij
.profile
.codeInspection
.InspectionProfileManager
;
39 import com
.intellij
.profile
.codeInspection
.InspectionProjectProfileManager
;
40 import com
.intellij
.psi
.PsiElement
;
41 import com
.intellij
.ui
.content
.ContentFactory
;
42 import com
.intellij
.ui
.content
.ContentManager
;
43 import com
.intellij
.ui
.content
.TabbedPaneContentUI
;
44 import org
.jetbrains
.annotations
.NonNls
;
45 import org
.jetbrains
.annotations
.NotNull
;
46 import org
.jetbrains
.annotations
.Nullable
;
48 import java
.util
.HashSet
;
51 public class InspectionManagerEx
extends InspectionManager
{
52 private GlobalInspectionContextImpl myGlobalInspectionContext
= null;
53 private final Project myProject
;
54 @NonNls private String myCurrentProfileName
;
55 private final NotNullLazyValue
<ContentManager
> myContentManager
;
57 private final Set
<GlobalInspectionContextImpl
> myRunningContexts
= new HashSet
<GlobalInspectionContextImpl
>();
59 public InspectionManagerEx(Project project
) {
61 if (!ApplicationManager
.getApplication().isHeadlessEnvironment()) {
62 myContentManager
= new NotNullLazyValue
<ContentManager
>() {
65 protected ContentManager
compute() {
66 ToolWindowManager toolWindowManager
= ToolWindowManager
.getInstance(myProject
);
67 ToolWindow toolWindow
=
68 toolWindowManager
.registerToolWindow(ToolWindowId
.INSPECTION
, true, ToolWindowAnchor
.BOTTOM
, myProject
);
69 ContentManager contentManager
= toolWindow
.getContentManager();
70 toolWindow
.setIcon(IconLoader
.getIcon("/general/toolWindowInspection.png"));
71 new ContentManagerWatcher(toolWindow
, contentManager
);
72 return contentManager
;
77 myContentManager
= new NotNullLazyValue
<ContentManager
>() {
80 protected ContentManager
compute() {
81 return ContentFactory
.SERVICE
.getInstance().createContentManager(new TabbedPaneContentUI(), true, myProject
);
88 public Project
getProject() {
93 public CommonProblemDescriptor
createProblemDescriptor(@NotNull String descriptionTemplate
, QuickFix
... fixes
) {
94 return new CommonProblemDescriptorImpl(fixes
, descriptionTemplate
);
98 public ProblemDescriptor
createProblemDescriptor(@NotNull PsiElement psiElement
,
99 @NotNull String descriptionTemplate
,
101 ProblemHighlightType highlightType
, boolean onTheFly
) {
102 LocalQuickFix
[] quickFixes
= fix
!= null ?
new LocalQuickFix
[]{fix
} : null;
103 return createProblemDescriptor(psiElement
, descriptionTemplate
, onTheFly
, quickFixes
, highlightType
);
107 public ProblemDescriptor
createProblemDescriptor(@NotNull PsiElement psiElement
,
108 @NotNull String descriptionTemplate
,
110 LocalQuickFix
[] fixes
,
111 ProblemHighlightType highlightType
) {
112 return createProblemDescriptor(psiElement
, descriptionTemplate
, fixes
, highlightType
, onTheFly
, false);
116 public ProblemDescriptor
createProblemDescriptor(@NotNull PsiElement psiElement
,
117 @NotNull String descriptionTemplate
,
118 LocalQuickFix
[] fixes
,
119 ProblemHighlightType highlightType
, boolean onTheFly
, boolean isAfterEndOfLine
) {
120 return new ProblemDescriptorImpl(psiElement
, psiElement
, descriptionTemplate
, fixes
, highlightType
, isAfterEndOfLine
, null, onTheFly
);
124 public ProblemDescriptor
createProblemDescriptor(@NotNull PsiElement startElement
,
125 @NotNull PsiElement endElement
,
126 @NotNull String descriptionTemplate
,
127 ProblemHighlightType highlightType
, boolean onTheFly
, LocalQuickFix
... fixes
) {
128 return new ProblemDescriptorImpl(startElement
, endElement
, descriptionTemplate
, fixes
, highlightType
, false, null, onTheFly
);
131 public ProblemDescriptor
createProblemDescriptor(@NotNull final PsiElement psiElement
,
132 final TextRange rangeInElement
,
133 @NotNull final String descriptionTemplate
,
134 final ProblemHighlightType highlightType
, boolean onTheFly
, final LocalQuickFix
... fixes
) {
135 return new ProblemDescriptorImpl(psiElement
, psiElement
, descriptionTemplate
, fixes
, highlightType
, false, rangeInElement
, onTheFly
);
138 public ProblemDescriptor
createProblemDescriptor(@NotNull final PsiElement psiElement
, @NotNull final String descriptionTemplate
, final ProblemHighlightType highlightType
,
139 @Nullable final HintAction hintAction
,
141 final LocalQuickFix
... fixes
) {
143 return new ProblemDescriptorImpl(psiElement
, psiElement
, descriptionTemplate
, fixes
, highlightType
, false, null, hintAction
, onTheFly
);
147 public ProblemDescriptor
createProblemDescriptor(@NotNull PsiElement psiElement
,
148 @NotNull String descriptionTemplate
,
150 ProblemHighlightType highlightType
, boolean onTheFly
, LocalQuickFix
... fixes
) {
151 return new ProblemDescriptorImpl(psiElement
, psiElement
, descriptionTemplate
, fixes
, highlightType
, false, null, showTooltip
, null,
155 public GlobalInspectionContextImpl
createNewGlobalContext(boolean reuse
) {
157 if (myGlobalInspectionContext
== null) {
158 myGlobalInspectionContext
= new GlobalInspectionContextImpl(myProject
, myContentManager
);
160 myRunningContexts
.add(myGlobalInspectionContext
);
161 return myGlobalInspectionContext
;
163 final GlobalInspectionContextImpl inspectionContext
= new GlobalInspectionContextImpl(myProject
, myContentManager
);
164 myRunningContexts
.add(inspectionContext
);
165 return inspectionContext
;
168 public void setProfile(final String name
) {
169 myCurrentProfileName
= name
;
172 public String
getCurrentProfile() {
173 if (myCurrentProfileName
== null) {
174 final InspectionProjectProfileManager profileManager
= InspectionProjectProfileManager
.getInstance(myProject
);
175 myCurrentProfileName
= profileManager
.getProjectProfile();
176 if (myCurrentProfileName
== null) {
177 myCurrentProfileName
= InspectionProfileManager
.getInstance().getRootProfile().getName();
180 return myCurrentProfileName
;
183 public void closeRunningContext(GlobalInspectionContextImpl globalInspectionContext
){
184 myRunningContexts
.remove(globalInspectionContext
);
187 public Set
<GlobalInspectionContextImpl
> getRunningContexts() {
188 return myRunningContexts
;
191 public static boolean inspectionResultSuppressed(PsiElement place
, LocalInspectionTool tool
) {
192 if (tool
instanceof CustomSuppressableInspectionTool
) {
193 return ((CustomSuppressableInspectionTool
)tool
).isSuppressedFor(place
);
195 return isSuppressed(place
, tool
.getID()) || isSuppressed(place
, tool
.getAlternativeID());
198 public static boolean canRunInspections(final Project project
, final boolean online
) {
199 for (InspectionExtensionsFactory factory
: Extensions
.getExtensions(InspectionExtensionsFactory
.EP_NAME
)) {
200 if (!factory
.isProjectConfiguredToRunInspections(project
, online
)) {
207 public static boolean isSuppressed(PsiElement psiElement
, String id
) {
208 if (id
== null) return false;
209 for (InspectionExtensionsFactory factory
: Extensions
.getExtensions(InspectionExtensionsFactory
.EP_NAME
)) {
210 if (!factory
.isToCheckMember(psiElement
, id
)) {
219 public ProblemDescriptor
createProblemDescriptor(@NotNull PsiElement psiElement
,
220 @NotNull String descriptionTemplate
,
222 ProblemHighlightType highlightType
) {
223 LocalQuickFix
[] quickFixes
= fix
!= null ?
new LocalQuickFix
[]{fix
} : null;
224 return createProblemDescriptor(psiElement
, descriptionTemplate
, quickFixes
, highlightType
);
229 public ProblemDescriptor
createProblemDescriptor(@NotNull PsiElement psiElement
,
230 @NotNull String descriptionTemplate
,
231 LocalQuickFix
[] fixes
,
232 ProblemHighlightType highlightType
) {
233 return createProblemDescriptor(psiElement
, descriptionTemplate
, fixes
, highlightType
, false);
238 public ProblemDescriptor
createProblemDescriptor(@NotNull PsiElement psiElement
,
239 @NotNull String descriptionTemplate
,
240 LocalQuickFix
[] fixes
,
241 ProblemHighlightType highlightType
,
242 boolean isAfterEndOfLine
) {
243 return new ProblemDescriptorImpl(psiElement
, psiElement
, descriptionTemplate
, fixes
, highlightType
, isAfterEndOfLine
, null, true);
248 public ProblemDescriptor
createProblemDescriptor(@NotNull PsiElement startElement
,
249 @NotNull PsiElement endElement
,
250 @NotNull String descriptionTemplate
,
251 ProblemHighlightType highlightType
,
252 LocalQuickFix
... fixes
) {
253 return new ProblemDescriptorImpl(startElement
, endElement
, descriptionTemplate
, fixes
, highlightType
, false, null, true);
257 public ProblemDescriptor
createProblemDescriptor(@NotNull final PsiElement psiElement
,
258 final TextRange rangeInElement
,
259 @NotNull final String descriptionTemplate
,
260 final ProblemHighlightType highlightType
,
261 final LocalQuickFix
... fixes
) {
262 return new ProblemDescriptorImpl(psiElement
, psiElement
, descriptionTemplate
, fixes
, highlightType
, false, rangeInElement
, true);
266 public ProblemDescriptor
createProblemDescriptor(@NotNull final PsiElement psiElement
,
267 @NotNull final String descriptionTemplate
,
268 final ProblemHighlightType highlightType
,
269 @Nullable final HintAction hintAction
,
270 final LocalQuickFix
... fixes
) {
272 return new ProblemDescriptorImpl(psiElement
, psiElement
, descriptionTemplate
, fixes
, highlightType
, false, null, hintAction
, true);
277 public ProblemDescriptor
createProblemDescriptor(@NotNull PsiElement psiElement
,
278 @NotNull String descriptionTemplate
,
280 ProblemHighlightType highlightType
,
281 LocalQuickFix
... fixes
) {
282 return new ProblemDescriptorImpl(psiElement
, psiElement
, descriptionTemplate
, fixes
, highlightType
, false, null, showTooltip
, null,