Skip to content

Commit 8b5224d

Browse files
committed
Add action to analyse context menu (#669)
1 parent 51359cf commit 8b5224d

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.infernus.idea.checkstyle.actions;
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent;
4+
import com.intellij.openapi.actionSystem.Presentation;
5+
import com.intellij.openapi.project.Project;
6+
import com.intellij.openapi.vfs.VirtualFile;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
import static com.intellij.openapi.actionSystem.CommonDataKeys.VIRTUAL_FILE;
11+
12+
/**
13+
* Action to execute a CheckStyle scan on the current editor file.
14+
*/
15+
public class AnalyseCurrentFile extends ScanCurrentFile {
16+
17+
@Override
18+
protected @Nullable VirtualFile selectedFile(@NotNull final Project project,
19+
@NotNull final AnActionEvent event) {
20+
return event.getDataContext().getData(VIRTUAL_FILE);
21+
}
22+
23+
@Override
24+
public void update(@NotNull final AnActionEvent event) {
25+
// update event does not expose selected file, so we always enable
26+
final Presentation presentation = event.getPresentation();
27+
project(event).ifPresentOrElse(
28+
project -> presentation.setEnabled(!staticScanner(project).isScanInProgress()),
29+
() -> presentation.setEnabled(false));
30+
}
31+
}

src/main/java/org/infernus/idea/checkstyle/actions/ScanCurrentFile.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ public void actionPerformed(final @NotNull AnActionEvent event) {
4343
final ToolWindow toolWindow = toolWindow(project);
4444
toolWindow.activate(() -> {
4545
try {
46+
final VirtualFile selectedFile = selectedFile(project, event);
47+
4648
setProgressText(toolWindow, "plugin.status.in-progress.current");
4749

4850
final ConfigurationLocation overrideIfExists = getSelectedOverride(toolWindow);
49-
final VirtualFile selectedFile = getSelectedValidFile(
50-
project,
51-
overrideIfExists);
52-
if (selectedFile != null) {
51+
if (validForScanning(selectedFile, project, overrideIfExists)) {
5352
staticScanner(project).asyncScanFiles(
5453
singletonList(selectedFile),
5554
overrideIfExists);
@@ -68,32 +67,30 @@ public void actionPerformed(final @NotNull AnActionEvent event) {
6867
});
6968
}
7069

71-
@Nullable
72-
private VirtualFile getSelectedValidFile(
73-
final Project project,
74-
final @Nullable ConfigurationLocation overrideIfExists) {
75-
final VirtualFile selectedFile = getSelectedFile(project);
70+
protected @Nullable VirtualFile selectedFile(@NotNull final Project project,
71+
@NotNull final AnActionEvent event) {
72+
return getSelectedFile(project);
73+
}
74+
75+
protected boolean validForScanning(final VirtualFile selectedFile,
76+
final @NotNull Project project,
77+
final @Nullable ConfigurationLocation overrideIfExists) {
7678
if (selectedFile == null) {
77-
return null;
79+
return false;
7880
}
7981

8082
final PluginConfiguration pluginConfiguration = configurationManager(project).getCurrent();
8183
if (!isFileValidAgainstScanScope(project, pluginConfiguration, selectedFile)) {
82-
return null;
84+
return false;
8385
}
8486

8587
final List<NamedScope> namedScopes = getNamedScopesToCheck(pluginConfiguration, overrideIfExists);
8688
if (!namedScopes.isEmpty() && namedScopes.stream().map(NamedScope::getValue).allMatch(Objects::isNull)) {
87-
return selectedFile;
89+
return true;
8890
}
8991

90-
final boolean isFileInScope = namedScopes.stream()
92+
return namedScopes.stream()
9193
.anyMatch((NamedScope namedScope) -> NamedScopeHelper.isFileInScope(psiFileFor(project, selectedFile), namedScope));
92-
if (isFileInScope) {
93-
return selectedFile;
94-
}
95-
96-
return null;
9794
}
9895

9996
@NotNull
@@ -171,7 +168,9 @@ public void update(final @NotNull AnActionEvent event) {
171168
final Presentation presentation = event.getPresentation();
172169

173170
project(event).ifPresentOrElse(project -> {
174-
if (getSelectedFile(project) != null) {
171+
final VirtualFile selectedFile = selectedFile(project, event);
172+
final ConfigurationLocation overrideIfExists = getSelectedOverride(toolWindow(project));
173+
if (selectedFile != null && validForScanning(selectedFile, project, overrideIfExists)) {
175174
presentation.setEnabled(!staticScanner(project).isScanInProgress());
176175
} else {
177176
presentation.setEnabled(false);

src/main/resources/META-INF/plugin.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@
104104
</extensions>
105105

106106
<actions>
107+
<action
108+
id="AnalyseCurrentFileAction"
109+
class="org.infernus.idea.checkstyle.actions.AnalyseCurrentFile"
110+
text="Scan with Checkstyle..."
111+
description="Scan the current file with the configured rules for Checkstyle">
112+
113+
<add-to-group group-id="AnalyzeMenu" anchor="last"/>
114+
</action>
115+
107116
<group id="CheckStylePluginTreeActions" text="Filter" popup="true">
108117
<action id="CheckStyleScrollToSourceAction"
109118
class="org.infernus.idea.checkstyle.actions.ScrollToSource"

0 commit comments

Comments
 (0)