Skip to content

Commit c990680

Browse files
committed
Add command for running checkstyle
This adds a command for the already existing action to run checkstyle on the selected resources. That way users can invoke the command also via Ctrl-3 quick assist or via a key binding. There is no default key binding, as the normal workflow for users is to rely on the Eclipse builder and to not invoke this command manually. fixes #99
1 parent c6bb3f2 commit c990680

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

net.sf.eclipsecs.ui/plugin.xml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,13 @@
308308
point="org.eclipse.ui.commands">
309309
<category id="net.sf.eclipsecs.ui" name="%commands.category.name"/>
310310
<command
311-
categoryId="org.eclipse.jdt.ui.category.source"
311+
categoryId="net.sf.eclipsecs.ui"
312+
description="%CheckSelectedFilesAction.tooltip"
313+
id="CheckstylePlugin.CheckSelectedFiles"
314+
name="%CheckSelectedFilesAction.label"
315+
defaultHandler="net.sf.eclipsecs.ui.actions.CheckSelectedFilesAction"/>
316+
<command
317+
categoryId="net.sf.eclipsecs.ui"
312318
description="%FixCheckstyleMarkersAction.tooltip"
313319
id="CheckstylePlugin.ApplyCheckstyleFixes"
314320
name="%FixCheckstyleMarkersAction.label"
@@ -326,7 +332,8 @@
326332
sequence="M1+M3+C"
327333
commandId="CheckstylePlugin.ApplyCheckstyleFixes"
328334
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
329-
contextId="org.eclipse.jdt.ui.javaEditorScope"/>></extension>
335+
contextId="org.eclipse.jdt.ui.javaEditorScope"/>
336+
</extension>
330337

331338
<!-- Popupmenu action for the 'Configure Project(s) from blueprint...' action -->
332339
<extension
@@ -535,6 +542,10 @@
535542
commandId="CheckstylePlugin.PurgeCaches"
536543
icon="platform:/plugin/org.eclipse.ui/icons/full/etool16/clear.png">
537544
</image>
545+
<image
546+
commandId="CheckstylePlugin.CheckSelectedFiles"
547+
icon="icons/checkstyle_command.png">
548+
</image>
538549
</extension>
539550
<extension
540551
point="net.sf.eclipsecs.ui.quickfix">

net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/actions/CheckSelectedFilesAction.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,34 @@
2222

2323
import java.util.ArrayList;
2424
import java.util.Arrays;
25+
import java.util.HashSet;
2526
import java.util.List;
27+
import java.util.Set;
2628

29+
import org.eclipse.core.commands.AbstractHandler;
30+
import org.eclipse.core.commands.ExecutionEvent;
31+
import org.eclipse.core.commands.ExecutionException;
2732
import org.eclipse.core.resources.IContainer;
2833
import org.eclipse.core.resources.IFile;
2934
import org.eclipse.core.resources.IResource;
3035
import org.eclipse.core.runtime.CoreException;
36+
import org.eclipse.core.runtime.IAdaptable;
3137
import org.eclipse.jface.action.IAction;
3238
import org.eclipse.jface.viewers.ISelection;
3339
import org.eclipse.jface.viewers.IStructuredSelection;
3440
import org.eclipse.ui.IObjectActionDelegate;
3541
import org.eclipse.ui.IWorkbenchPart;
42+
import org.eclipse.ui.handlers.HandlerUtil;
3643

3744
import net.sf.eclipsecs.core.jobs.RunCheckstyleOnFilesJob;
3845
import net.sf.eclipsecs.ui.CheckstyleUIPlugin;
3946

4047
/**
41-
* Action to diable Checkstyle on one ore more projects.
48+
* Action to run Checkstyle on one ore more projects.
4249
*
4350
* @author Lars Ködderitzsch
4451
*/
45-
public class CheckSelectedFilesAction implements IObjectActionDelegate {
52+
public class CheckSelectedFilesAction extends AbstractHandler implements IObjectActionDelegate {
4653

4754
private IWorkbenchPart mPart;
4855

@@ -62,13 +69,26 @@ public void selectionChanged(IAction action, ISelection selection) {
6269
}
6370

6471
@Override
65-
@SuppressWarnings("unchecked")
6672
public void run(IAction action) {
73+
checkSelection(mSelection);
74+
}
6775

76+
private void checkSelection(IStructuredSelection selection) {
77+
Set<IResource> resources = new HashSet<>();
78+
for (Object object : selection.toList()) {
79+
if (object instanceof IAdaptable adaptable) {
80+
var resource = adaptable.getAdapter(IResource.class);
81+
if (resource != null) {
82+
resources.add(resource);
83+
}
84+
}
85+
}
6886
List<IFile> filesToCheck = new ArrayList<>();
69-
7087
try {
71-
addFileResources(mSelection.toList(), filesToCheck);
88+
addFileResources(List.copyOf(resources), filesToCheck);
89+
if (filesToCheck.isEmpty()) {
90+
return;
91+
}
7292

7393
RunCheckstyleOnFilesJob job = new RunCheckstyleOnFilesJob(filesToCheck);
7494
job.setRule(job);
@@ -79,18 +99,16 @@ public void run(IAction action) {
7999
}
80100

81101
/**
82-
* Recursively add all files contained in the given resource collection to the
83-
* second list.
102+
* Recursively add all files contained in the given resource collection to the second list.
84103
*
85104
* @param resources
86105
* list of resource
87106
* @param files
88107
* the list of files
89108
* @throws CoreException
90-
* en unexpected exception
109+
* an unexpected exception
91110
*/
92111
private void addFileResources(List<IResource> resources, List<IFile> files) throws CoreException {
93-
94112
for (IResource resource : resources) {
95113

96114
if (!resource.isAccessible()) {
@@ -104,4 +122,13 @@ private void addFileResources(List<IResource> resources, List<IFile> files) thro
104122
}
105123
}
106124
}
125+
126+
@Override
127+
public Object execute(ExecutionEvent event) throws ExecutionException {
128+
var selection = HandlerUtil.getCurrentSelection(event);
129+
if (selection instanceof IStructuredSelection structuredSelection) {
130+
checkSelection(structuredSelection);
131+
}
132+
return null;
133+
}
107134
}

0 commit comments

Comments
 (0)