1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection.actions;
import com.intellij.codeInsight.FileModificationService;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.codeInsight.intention.EmptyIntentionAction;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.ex.InspectionToolWrapper;
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Computable;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* User: anna
* Date: 21-Feb-2006
*/
public class CleanupInspectionIntention implements IntentionAction, HighPriorityAction {
private final InspectionToolWrapper myToolWrapper;
private final Class myQuickfixClass;
private final String myText;
public CleanupInspectionIntention(@NotNull InspectionToolWrapper toolWrapper, @NotNull Class quickFixClass, String text) {
myToolWrapper = toolWrapper;
myQuickfixClass = quickFixClass;
myText = text;
}
@Override
@NotNull
public String getText() {
return InspectionsBundle.message("fix.all.inspection.problems.in.file", myToolWrapper.getDisplayName());
}
@Override
@NotNull
public String getFamilyName() {
return getText();
}
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
if (!FileModificationService.getInstance().preparePsiElementForWrite(file)) return;
final List<ProblemDescriptor> descriptions =
ProgressManager.getInstance().runProcess(new Computable<List<ProblemDescriptor>>() {
@Override
public List<ProblemDescriptor> compute() {
InspectionManager inspectionManager = InspectionManager.getInstance(project);
return InspectionEngine.runInspectionOnFile(file, myToolWrapper, inspectionManager.createNewGlobalContext(false));
}
}, new EmptyProgressIndicator());
Collections.sort(descriptions, new Comparator<CommonProblemDescriptor>() {
@Override
public int compare(final CommonProblemDescriptor o1, final CommonProblemDescriptor o2) {
final ProblemDescriptorBase d1 = (ProblemDescriptorBase)o1;
final ProblemDescriptorBase d2 = (ProblemDescriptorBase)o2;
return d2.getTextRange().getStartOffset() - d1.getTextRange().getStartOffset();
}
});
boolean applicableFixFound = false;
for (final ProblemDescriptor descriptor : descriptions) {
final QuickFix[] fixes = descriptor.getFixes();
if (fixes != null && fixes.length > 0) {
for (final QuickFix<CommonProblemDescriptor> fix : fixes) {
if (fix != null && fix.getClass().isAssignableFrom(myQuickfixClass)) {
final PsiElement element = descriptor.getPsiElement();
if (element != null && element.isValid()) {
applicableFixFound = true;
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
fix.applyFix(project, descriptor);
}
});
PsiDocumentManager.getInstance(project).commitAllDocuments();
}
break;
}
}
}
}
if (!applicableFixFound) {
HintManager.getInstance().showErrorHint(editor, "Unfortunately '" + myText + "' is currently not available for batch mode");
}
}
@Override
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
return myQuickfixClass != EmptyIntentionAction.class &&
!(myToolWrapper instanceof LocalInspectionToolWrapper && ((LocalInspectionToolWrapper)myToolWrapper).isUnfair());
}
@Override
public boolean startInWriteAction() {
return false;
}
}
|