Skip to content

Commit dc84135

Browse files
committed
Limit checked Gradle projects to those refreshed
Update the Eclipse plugin Gradle support so that only refresh projects are checked to see if they need to have settings applied. Closes gh-120
1 parent b93af6e commit dc84135

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

spring-javaformat-eclipse/io.spring.javaformat.eclipse/META-INF/MANIFEST.MF

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.8
99
Require-Bundle: org.eclipse.ui,
1010
org.eclipse.core.resources,
1111
org.eclipse.core.runtime,
12+
org.eclipse.ui.ide,
13+
org.slf4j.api;bundle-version="1.7.0",
1214
org.eclipse.jdt.core,
1315
org.eclipse.jface.text,
1416
org.eclipse.m2e.jdt;resolution:=optional,
1517
org.eclipse.m2e.core;resolution:=optional,
1618
org.eclipse.m2e.maven.runtime;resolution:=optional,
1719
org.eclipse.buildship.core;bundle-version="3.1.0";resolution:=optional,
18-
net.sf.eclipsecs.core;bundle-version="8.18.0";resolution:=optional,
19-
org.slf4j.api;bundle-version="1.7.0"
20+
net.sf.eclipsecs.core;bundle-version="8.18.0";resolution:=optional
2021
Bundle-ClassPath: .,
2122
lib/spring-javaformat-formatter-eclipse.jar,
2223
lib/spring-javaformat-formatter.jar,

spring-javaformat-eclipse/io.spring.javaformat.eclipse/src/io/spring/javaformat/eclipse/gradle/RefreshProjectSettingsListeners.java

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,32 @@
1616

1717
package io.spring.javaformat.eclipse.gradle;
1818

19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.LinkedHashSet;
22+
import java.util.Set;
23+
1924
import org.eclipse.buildship.core.internal.CorePlugin;
2025
import org.eclipse.buildship.core.internal.event.Event;
2126
import org.eclipse.buildship.core.internal.event.EventListener;
27+
import org.eclipse.buildship.core.internal.util.collections.AdapterFunction;
2228
import org.eclipse.buildship.core.internal.workspace.GradleNatureAddedEvent;
2329
import org.eclipse.buildship.core.internal.workspace.ProjectCreatedEvent;
2430
import org.eclipse.core.commands.Command;
2531
import org.eclipse.core.commands.ExecutionEvent;
2632
import org.eclipse.core.commands.ExecutionException;
2733
import org.eclipse.core.commands.IExecutionListener;
2834
import org.eclipse.core.commands.NotHandledException;
35+
import org.eclipse.core.resources.IFile;
36+
import org.eclipse.core.resources.IProject;
37+
import org.eclipse.core.resources.IResource;
38+
import org.eclipse.jface.viewers.ISelection;
39+
import org.eclipse.jface.viewers.IStructuredSelection;
40+
import org.eclipse.ui.IEditorInput;
2941
import org.eclipse.ui.PlatformUI;
3042
import org.eclipse.ui.commands.ICommandService;
43+
import org.eclipse.ui.handlers.HandlerUtil;
44+
import org.eclipse.ui.part.FileEditorInput;
3145

3246
/**
3347
* Listeners used to trigger the {@link RefreshProjectsSettingsJob}.
@@ -59,21 +73,55 @@ private static class CommandListener implements IExecutionListener {
5973

6074
private static final String COMMAND_NAME = "org.eclipse.buildship.ui.commands.refreshproject"; //$NON-NLS-1$
6175

76+
private ThreadLocal<ExecutionEvent> event = new ThreadLocal<ExecutionEvent>();
77+
6278
@Override
63-
public void notHandled(String commandId, NotHandledException exception) {
79+
public void preExecute(String commandId, ExecutionEvent event) {
80+
this.event.set(event);
6481
}
6582

6683
@Override
67-
public void postExecuteFailure(String commandId, ExecutionException exception) {
84+
public void postExecuteSuccess(String commandId, Object returnValue) {
85+
Set<IProject> projects = getProjects(this.event.get());
86+
this.event.set(null);
87+
new RefreshProjectsSettingsJob(projects).schedule();
88+
}
89+
90+
private Set<IProject> getProjects(ExecutionEvent event) {
91+
if (event == null) {
92+
return null;
93+
}
94+
ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
95+
if (currentSelection instanceof IStructuredSelection) {
96+
IStructuredSelection selection = (IStructuredSelection) currentSelection;
97+
return collectGradleProjects(selection.toList());
98+
}
99+
IEditorInput editorInput = HandlerUtil.getActiveEditorInput(event);
100+
if (editorInput instanceof FileEditorInput) {
101+
IFile file = ((FileEditorInput) editorInput).getFile();
102+
return collectGradleProjects(Collections.singleton(file));
103+
}
104+
return null;
105+
}
106+
107+
private Set<IProject> collectGradleProjects(Collection<?> candidates) {
108+
Set<IProject> projects = new LinkedHashSet<>(candidates.size());
109+
AdapterFunction<IResource> adapter = AdapterFunction.forType(IResource.class);
110+
for (Object candidate : candidates) {
111+
IResource resource = adapter.apply(candidate);
112+
if (resource != null) {
113+
projects.add(resource.getProject());
114+
}
115+
}
116+
return projects;
68117
}
69118

70119
@Override
71-
public void postExecuteSuccess(String commandId, Object returnValue) {
72-
new RefreshProjectsSettingsJob().schedule();
120+
public void postExecuteFailure(String commandId, ExecutionException exception) {
73121
}
74122

75123
@Override
76-
public void preExecute(String commandId, ExecutionEvent event) {
124+
public void notHandled(String commandId, NotHandledException exception) {
77125
}
78126

79127
static void attach() {
@@ -89,7 +137,7 @@ static void attach() {
89137
}
90138

91139
/**
92-
* Event Listener to triger an update after a project import or gradle nature change.
140+
* Event Listener to trigger an update after a project import or gradle nature change.
93141
*/
94142
private static class ProjectListener implements EventListener {
95143

spring-javaformat-eclipse/io.spring.javaformat.eclipse/src/io/spring/javaformat/eclipse/gradle/RefreshProjectsSettingsJob.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.util.Arrays;
2122
import java.util.Collection;
2223
import java.util.LinkedHashSet;
2324
import java.util.Optional;
@@ -55,9 +56,16 @@ public class RefreshProjectsSettingsJob extends Job {
5556

5657
private final CancellationTokenSource tokenSource;
5758

59+
private final Set<IProject> projects;
60+
5861
public RefreshProjectsSettingsJob() {
62+
this(null);
63+
}
64+
65+
public RefreshProjectsSettingsJob(Set<IProject> projects) {
5966
super("Refresh spring-javaformat project settings");
6067
this.tokenSource = GradleConnector.newCancellationTokenSource();
68+
this.projects = projects;
6169
}
6270

6371
@Override
@@ -75,7 +83,11 @@ protected IStatus run(IProgressMonitor monitor) {
7583

7684
private void configureProjects(IProgressMonitor monitor) throws CoreException, IOException {
7785
InternalGradleWorkspace workspace = CorePlugin.internalGradleWorkspace();
78-
for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
86+
Collection<IProject> projects = this.projects;
87+
if (projects == null) {
88+
projects = Arrays.asList(ResourcesPlugin.getWorkspace().getRoot().getProjects());
89+
}
90+
for (IProject project : projects) {
7991
Optional<GradleBuild> build = workspace.getBuild(project);
8092
if (build.isPresent()) {
8193
configureProject(project, (InternalGradleBuild) build.get(), monitor);

0 commit comments

Comments
 (0)