-
Notifications
You must be signed in to change notification settings - Fork 161
/
CheckStyleInspection.java
200 lines (167 loc) · 8.63 KB
/
CheckStyleInspection.java
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package org.infernus.idea.checkstyle;
import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import org.infernus.idea.checkstyle.checker.CheckerFactory;
import org.infernus.idea.checkstyle.checker.Problem;
import org.infernus.idea.checkstyle.checker.ScannableFile;
import org.infernus.idea.checkstyle.config.ConfigurationLocationSource;
import org.infernus.idea.checkstyle.config.PluginConfigurationManager;
import org.infernus.idea.checkstyle.csapi.SeverityLevel;
import org.infernus.idea.checkstyle.exception.CheckStylePluginParseException;
import org.infernus.idea.checkstyle.model.ConfigurationLocation;
import org.infernus.idea.checkstyle.ui.CheckStyleInspectionPanel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
import static java.util.Collections.singletonList;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;
import static org.infernus.idea.checkstyle.CheckStyleBundle.message;
import static org.infernus.idea.checkstyle.util.Async.asyncResultOf;
import static org.infernus.idea.checkstyle.util.Notifications.showException;
import static org.infernus.idea.checkstyle.util.Notifications.showWarning;
public class CheckStyleInspection extends LocalInspectionTool {
private static final Logger LOG = Logger.getInstance(CheckStyleInspection.class);
private static final List<Problem> NO_PROBLEMS_FOUND = Collections.emptyList();
private static final long FIVE_SECONDS = 5000L;
private final CheckStyleInspectionPanel configPanel = new CheckStyleInspectionPanel();
@Nullable
public JComponent createOptionsPanel() {
return configPanel;
}
@Override
public ProblemDescriptor[] checkFile(@NotNull final PsiFile psiFile,
@NotNull final InspectionManager manager,
final boolean isOnTheFly) {
if (InjectedLanguageManager.getInstance(manager.getProject()).isInjectedFragment(psiFile)) {
LOG.debug("Ignoring file as it is an injected fragment: " + psiFile);
return noProblemsFound(manager);
}
final Module module = moduleOf(psiFile);
List<ScannableFile> scannableFiles = ScannableFile.createAndValidate(singletonList(psiFile), manager.getProject(), module);
if (scannableFiles.isEmpty()) {
LOG.debug("Inspection has been cancelled as file is not scannable: " + psiFile.getName());
return noProblemsFound(manager);
}
try {
return asProblemDescriptors(
asyncResultOf(() -> {
try {
return inspectFile(psiFile, scannableFiles, module, manager);
} finally {
scannableFiles.forEach(ScannableFile::deleteIfRequired);
}
}, NO_PROBLEMS_FOUND, FIVE_SECONDS),
manager);
} catch (ProcessCanceledException | AssertionError e) {
LOG.debug("Inspection cancelled when scanning: " + psiFile.getName());
return noProblemsFound(manager);
} catch (Throwable e) {
LOG.warn("CheckStyle threw an exception when inspecting: " + psiFile.getName(), e);
showException(manager.getProject(), e);
return noProblemsFound(manager);
}
}
private ConfigurationLocationSource configurationLocationSource(final Project project) {
return ServiceManager.getService(project, ConfigurationLocationSource.class);
}
@NotNull
private ProblemDescriptor[] noProblemsFound(@NotNull final InspectionManager manager) {
return asProblemDescriptors(NO_PROBLEMS_FOUND, manager);
}
@Nullable
private Module moduleOf(@NotNull final PsiFile psiFile) {
return ModuleUtil.findModuleForPsiElement(psiFile);
}
private List<Problem> inspectFile(@NotNull final PsiFile psiFile,
@NotNull final List<ScannableFile> scannableFiles,
@Nullable final Module module,
@NotNull final InspectionManager manager) {
LOG.debug("Inspection has been invoked for " + psiFile.getName());
ConfigurationLocation configurationLocation = null;
try {
configurationLocation = configurationLocationSource(manager.getProject())
.getConfigurationLocation(module, null);
if (configurationLocation == null || configurationLocation.isBlocked()) {
return NO_PROBLEMS_FOUND;
}
return checkerFactory(psiFile.getProject())
.checker(module, configurationLocation)
.map(checker -> checker.scan(scannableFiles, configurationManager(psiFile.getProject()).getCurrent().isSuppressErrors()))
.map(results -> results.get(psiFile))
.map(this::dropIgnoredProblems)
.orElse(NO_PROBLEMS_FOUND);
} catch (ProcessCanceledException | AssertionError e) {
LOG.debug("Process cancelled when scanning: " + psiFile.getName());
return NO_PROBLEMS_FOUND;
} catch (CheckStylePluginParseException e) {
LOG.debug("Parse exception caught when scanning: " + psiFile.getName(), e);
return NO_PROBLEMS_FOUND;
} catch (Throwable e) {
handlePluginException(e, psiFile, configurationLocation, manager.getProject());
return NO_PROBLEMS_FOUND;
} finally {
scannableFiles.forEach(ScannableFile::deleteIfRequired);
}
}
private List<Problem> dropIgnoredProblems(final List<Problem> problems) {
return problems.stream()
.filter(problem -> problem.severityLevel() != SeverityLevel.Ignore)
.collect(toList());
}
private void handlePluginException(final Throwable e,
final @NotNull PsiFile psiFile,
final ConfigurationLocation configurationLocation,
final @NotNull Project project) {
if (e.getCause() != null && e.getCause() instanceof ProcessCanceledException) {
LOG.debug("Process cancelled when scanning: " + psiFile.getName());
} else if (e.getCause() != null && e.getCause() instanceof FileNotFoundException) {
disableActiveConfiguration(project);
} else if (e.getCause() != null && e.getCause() instanceof IOException) {
showWarning(project, message("checkstyle.file-io-failed"));
block(configurationLocation);
} else {
LOG.warn("CheckStyle threw an exception when scanning: " + psiFile.getName(), e);
showException(project, e);
block(configurationLocation);
}
}
private void disableActiveConfiguration(final Project project) {
configurationManager(project).disableActiveConfiguration();
showWarning(project, message("checkstyle.configuration-disabled.file-not-found"));
}
private void block(final ConfigurationLocation configurationLocation) {
if (configurationLocation != null) {
configurationLocation.block();
}
}
@NotNull
private ProblemDescriptor[] asProblemDescriptors(final List<Problem> results, final InspectionManager manager) {
return ofNullable(results)
.map(TreeSet::new)
.map(problems -> problems.stream()
.map(problem -> problem.toProblemDescriptor(manager))
.toArray(ProblemDescriptor[]::new))
.orElse(ProblemDescriptor.EMPTY_ARRAY);
}
private CheckerFactory checkerFactory(final Project project) {
return ServiceManager.getService(project, CheckerFactory.class);
}
private PluginConfigurationManager configurationManager(final Project project) {
return ServiceManager.getService(project, PluginConfigurationManager.class);
}
}