-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionFilesAnalyzer.java
More file actions
176 lines (154 loc) · 5.88 KB
/
ActionFilesAnalyzer.java
File metadata and controls
176 lines (154 loc) · 5.88 KB
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
package software.xdev.pmd.action;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileVisitor;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import software.xdev.pmd.analysis.NoAnalysisReason;
import software.xdev.pmd.analysis.PMDAnalysisResult;
import software.xdev.pmd.analysis.PMDAnalyzer;
import software.xdev.pmd.analysis.PsiFileValidator;
import software.xdev.pmd.config.ConfigurationLocationSource;
import software.xdev.pmd.config.PluginConfiguration;
import software.xdev.pmd.config.PluginConfigurationManager;
import software.xdev.pmd.currentfile.CombinedPMDAnalysisResult;
import software.xdev.pmd.model.scope.ScanScope;
import software.xdev.pmd.ui.toolwindow.analysis.report.ReportViewManager;
public class ActionFilesAnalyzer
{
public void analyzeFromAction(@NotNull final AnActionEvent ev)
{
final Project project = ev.getData(CommonDataKeys.PROJECT);
if(project == null || project.isDisposed())
{
return;
}
final VirtualFile[] selectedFiles = ActionPlaces.MAIN_MENU.equals(ev.getPlace())
? VfsUtil.getCommonAncestors(ProjectRootManager.getInstance(project).getContentRoots())
: ev.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
if(selectedFiles == null || selectedFiles.length == 0)
{
return;
}
ProgressManager.getInstance().run(new Task.Backgroundable(project, "Analysing files...", true)
{
@Override
public void run(@NotNull final ProgressIndicator indicator)
{
indicator.setIndeterminate(true);
ActionFilesAnalyzer.this.analyzeAsync(project, indicator, selectedFiles, ev);
}
});
}
@SuppressWarnings("checkstyle:MagicNumber")
private void analyzeAsync(
final Project project,
final ProgressIndicator progressIndicator,
final VirtualFile[] selectedFiles,
final AnActionEvent triggeringEvent)
{
progressIndicator.setText("Collecting files...");
progressIndicator.setIndeterminate(true);
final PsiManager psiManager = PsiManager.getInstance(project);
final PluginConfiguration pluginConfiguration =
project.getService(PluginConfigurationManager.class).getCurrent();
final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final Map<Optional<com.intellij.openapi.module.Module>, Set<PsiFile>> psiFiles =
ReadAction.computeBlocking(() -> this.collectFiles(
projectFileIndex,
psiManager,
pluginConfiguration,
progressIndicator,
selectedFiles));
if(psiFiles.isEmpty())
{
project.getService(ReportViewManager.class).displayNewReport(
CombinedPMDAnalysisResult.combine(PMDAnalysisResult.empty(NoAnalysisReason.NO_APPLICABLE_FILES)),
triggeringEvent);
return;
}
progressIndicator.checkCanceled();
progressIndicator.setText("Launching analyses");
progressIndicator.setText2("");
final CombinedPMDAnalysisResult combined = CombinedPMDAnalysisResult.combine(psiFiles.entrySet()
.parallelStream()
.map(e ->
project.getService(PMDAnalyzer.class).analyze(
e.getKey(),
e.getValue(),
false,
project.getService(ConfigurationLocationSource.class)
.getConfigurationLocations(e.getKey().orElse(null)),
progressIndicator))
.toList());
project.getService(ReportViewManager.class).displayNewReport(combined, triggeringEvent);
}
@NotNull
private Map<Optional<com.intellij.openapi.module.Module>, Set<PsiFile>> collectFiles(
final ProjectFileIndex projectFileIndex,
final PsiManager psiManager,
final PluginConfiguration pluginConfiguration,
final ProgressIndicator progressIndicator,
final VirtualFile[] selectedFiles)
{
final Map<Optional<com.intellij.openapi.module.Module>, Set<PsiFile>> psiFiles = new HashMap<>();
final AtomicInteger counterScanned = new AtomicInteger();
final VirtualFileVisitor<Object> fileVisitor = new VirtualFileVisitor<>()
{
@Override
public boolean visitFile(@NotNull final VirtualFile file)
{
progressIndicator.checkCanceled();
progressIndicator.setText2(counterScanned.getAndIncrement() + "x elements checked");
if(file.isDirectory())
{
final ScanScope scanScope = pluginConfiguration.scanScope();
if(!scanScope.includeTestClasses()
&& projectFileIndex.isInTestSourceContent(file))
{
return false;
}
return !projectFileIndex.isInGeneratedSources(file)
&& !projectFileIndex.isExcluded(file)
&& !projectFileIndex.isInLibrary(file);
}
final PsiFile psiFile = psiManager.findFile(file);
final Optional<Module> optModule =
Optional.ofNullable(ModuleUtilCore.findModuleForFile(psiFile));
if(!PsiFileValidator.isScannable(psiFile, optModule, pluginConfiguration))
{
return false;
}
final Set<PsiFile> modulePsiFiles = psiFiles.computeIfAbsent(
optModule,
ignored -> new HashSet<>());
modulePsiFiles.add(psiFile);
return true;
}
};
Arrays.stream(selectedFiles).forEach(file ->
VfsUtilCore.visitChildrenRecursively(file, fileVisitor));
return psiFiles;
}
}