Inspections - pass onTheFly into ProblemDescriptors & use it to create LAZY refs...
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInspection / ex / GlobalInspectionToolWrapper.java
blob8a1113e73c1550982cbf17cbe29a16f87a8a8a41
1 /*
2 * Copyright (c) 2006 Your Corporation. All Rights Reserved.
3 */
4 package com.intellij.codeInspection.ex;
6 import com.intellij.analysis.AnalysisScope;
7 import com.intellij.codeHighlighting.HighlightDisplayLevel;
8 import com.intellij.codeInsight.intention.IntentionAction;
9 import com.intellij.codeInspection.*;
10 import com.intellij.codeInspection.reference.RefEntity;
11 import com.intellij.codeInspection.reference.RefGraphAnnotator;
12 import com.intellij.codeInspection.reference.RefManagerImpl;
13 import com.intellij.codeInspection.reference.RefVisitor;
14 import com.intellij.openapi.editor.Editor;
15 import com.intellij.openapi.project.Project;
16 import com.intellij.openapi.util.InvalidDataException;
17 import com.intellij.openapi.util.WriteExternalException;
18 import com.intellij.psi.PsiFile;
19 import com.intellij.util.IncorrectOperationException;
20 import org.jdom.Element;
21 import org.jetbrains.annotations.NonNls;
22 import org.jetbrains.annotations.NotNull;
23 import org.jetbrains.annotations.Nullable;
25 import javax.swing.*;
27 /**
28 * User: anna
29 * Date: 28-Dec-2005
31 public class GlobalInspectionToolWrapper extends DescriptorProviderInspection {
32 @NotNull private final GlobalInspectionTool myTool;
34 public GlobalInspectionToolWrapper(@NotNull GlobalInspectionTool globalInspectionTool) {
35 myTool = globalInspectionTool;
38 public void initialize(@NotNull GlobalInspectionContextImpl context) {
39 super.initialize(context);
40 final RefGraphAnnotator annotator = myTool.getAnnotator(getRefManager());
41 if (annotator != null) {
42 ((RefManagerImpl)getRefManager()).registerGraphAnnotator(annotator);
46 public void runInspection(final AnalysisScope scope, final InspectionManager manager) {
47 myTool.runInspection(scope, manager, getContext(), this);
50 public boolean queryExternalUsagesRequests(final InspectionManager manager) {
51 return myTool.queryExternalUsagesRequests(manager, getContext(), this);
54 @NotNull
55 public JobDescriptor[] getJobDescriptors() {
56 return isGraphNeeded() ? new JobDescriptor[]{GlobalInspectionContextImpl.BUILD_GRAPH}: JobDescriptor.EMPTY_ARRAY;
59 @NotNull
60 public String getDisplayName() {
61 return myTool.getDisplayName();
64 @NotNull
65 public String getGroupDisplayName() {
66 return myTool.getGroupDisplayName();
69 @NotNull
70 @Override
71 public String[] getGroupPath() {
72 return myTool.getGroupPath();
75 @NotNull
76 @NonNls
77 public String getShortName() {
78 return myTool.getShortName();
81 public boolean isEnabledByDefault() {
82 return myTool.isEnabledByDefault();
85 @NotNull
86 public HighlightDisplayLevel getDefaultLevel() {
87 return myTool.getDefaultLevel();
90 public void readSettings(Element element) throws InvalidDataException {
91 myTool.readSettings(element);
94 public void writeSettings(Element element) throws WriteExternalException {
95 myTool.writeSettings(element);
98 public JComponent createOptionsPanel() {
99 return myTool.createOptionsPanel();
102 public boolean isGraphNeeded() {
103 return myTool.isGraphNeeded();
106 @NotNull public GlobalInspectionTool getTool() {
107 return myTool;
110 public void processFile(final AnalysisScope analysisScope,
111 final InspectionManager manager,
112 final GlobalInspectionContext context,
113 final boolean filterSuppressed) {
114 context.getRefManager().iterate(new RefVisitor() {
115 @Override public void visitElement(RefEntity refEntity) {
116 CommonProblemDescriptor[] descriptors = myTool.checkElement(refEntity, analysisScope, manager, context, GlobalInspectionToolWrapper.this);
117 if (descriptors != null) {
118 addProblemElement(refEntity, filterSuppressed, descriptors);
123 public void projectOpened(Project project) {
124 myTool.projectOpened(project);
127 public void projectClosed(Project project) {
128 myTool.projectClosed(project);
132 public HTMLComposerImpl getComposer() {
133 return new DescriptorComposer(this) {
134 protected void composeAdditionalDescription(final StringBuffer buf, final RefEntity refEntity) {
135 myTool.compose(buf, refEntity, this);
140 @Nullable
141 public IntentionAction findQuickFixes(final CommonProblemDescriptor problemDescriptor, final String hint) {
142 final QuickFix fix = myTool.getQuickFix(hint);
143 if (fix != null) {
144 if (problemDescriptor instanceof ProblemDescriptor) {
145 final ProblemDescriptor descriptor = new ProblemDescriptorImpl(((ProblemDescriptor)problemDescriptor).getStartElement(),
146 ((ProblemDescriptor)problemDescriptor).getEndElement(),
147 problemDescriptor.getDescriptionTemplate(),
148 new LocalQuickFix[]{(LocalQuickFix)fix},
149 ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false, null, false);
150 return QuickFixWrapper.wrap(descriptor, 0);
152 else {
153 return new IntentionAction() {
154 @NotNull
155 public String getText() {
156 return fix.getName();
159 @NotNull
160 public String getFamilyName() {
161 return fix.getFamilyName();
164 public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
165 return true;
168 public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
169 fix.applyFix(project, problemDescriptor); //todo check type consistency
172 public boolean startInWriteAction() {
173 return true;
178 return null;
181 protected Class<? extends InspectionProfileEntry> getDescriptionContextClass() {
182 return myTool.getClass();
185 @Nullable
186 public String getStaticDescription() {
187 return myTool.getStaticDescription();
190 @Nullable
191 public SuppressIntentionAction[] getSuppressActions() {
192 if (myTool instanceof CustomSuppressableInspectionTool) {
193 return ((CustomSuppressableInspectionTool)myTool).getSuppressActions(null);
195 return super.getSuppressActions();