Skip to content

Commit

Permalink
Add java.project.resourceFilters preference
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <snjezana.peco@redhat.com>
  • Loading branch information
snjeza authored and fbricon committed Aug 21, 2020
1 parent f0ad8cb commit d5ca624
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public IStatus runInWorkspace(IProgressMonitor monitor) {
try {
projectsManager.setAutoBuilding(false);
projectsManager.initializeProjects(roots, subMonitor);
projectsManager.configureFilters(monitor);
projectsManager.setAutoBuilding(preferenceManager.getPreferences().isAutobuildEnabled());
JavaLanguageServerPlugin.logInfo("Workspace initialized in " + (System.currentTimeMillis() - start) + "ms");
connection.sendStatus(ServiceStatus.Started, "Ready");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.FileInfoMatcherDescription;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceFilterDescription;
import org.eclipse.core.resources.ISaveContext;
import org.eclipse.core.resources.ISaveParticipant;
import org.eclipse.core.resources.IWorkspace;
Expand Down Expand Up @@ -77,6 +80,9 @@ public abstract class ProjectsManager implements ISaveParticipant, IProjectsMana

public static final String DEFAULT_PROJECT_NAME = "jdt.ls-java-project";
public static final String PROJECTS_IMPORTED = "__PROJECTS_IMPORTED__";
private static final String CORE_RESOURCES_MATCHER_ID = "org.eclipse.core.resources.regexFilterMatcher";
public static final String CREATED_BY_JAVA_LANGUAGE_SERVER = "__CREATED_BY_JAVA_LANGUAGE_SERVER__";
private static final int JDTLS_FILTER_TYPE = IResourceFilterDescription.EXCLUDE_ALL | IResourceFilterDescription.INHERITABLE | IResourceFilterDescription.FILES | IResourceFilterDescription.FOLDERS;

private PreferenceManager preferenceManager;
protected JavaLanguageClient client;
Expand Down Expand Up @@ -509,4 +515,38 @@ public File findFile(String formatterUrl) {
return null;
}

public void configureFilters(IProgressMonitor monitor) throws CoreException {
List<String> resourceFilters = preferenceManager.getPreferences().getResourceFilters();
if (resourceFilters != null && !resourceFilters.isEmpty()) {
resourceFilters = new ArrayList<>(resourceFilters);
resourceFilters.add(CREATED_BY_JAVA_LANGUAGE_SERVER);
}
String resourceFilter = resourceFilters == null ? null : String.join("|", resourceFilters);
for (IProject project : ProjectUtils.getAllProjects()) {
if (project.equals(getDefaultProject())) {
continue;
}
List<IResourceFilterDescription> filters = Stream.of(project.getFilters())
.filter(f -> {
FileInfoMatcherDescription matcher = f.getFileInfoMatcherDescription();
return CORE_RESOURCES_MATCHER_ID.equals(matcher.getId()) && (matcher.getArguments() instanceof String) && ((String) matcher.getArguments()).contains(CREATED_BY_JAVA_LANGUAGE_SERVER);
})
.collect(Collectors.toList());
boolean filterExists = false;
for (IResourceFilterDescription filter : filters) {
if (resourceFilter == null || resourceFilter.isEmpty()) {
filter.delete(IResource.BACKGROUND_REFRESH, monitor);
} else if (!Objects.equals(resourceFilter, filter.getFileInfoMatcherDescription().getArguments())) {
filter.delete(IResource.BACKGROUND_REFRESH, monitor);
} else {
filterExists = true;
break;
}
}
if (!filterExists && resourceFilter != null && !resourceFilter.isEmpty()) {
project.createFilter(JDTLS_FILTER_TYPE, new FileInfoMatcherDescription(CORE_RESOURCES_MATCHER_ID, resourceFilter), IResource.BACKGROUND_REFRESH, monitor);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
Expand Down Expand Up @@ -418,6 +419,13 @@ public void preferencesChange(Preferences oldPreferences, Preferences newPrefere
registerWatcherJob.schedule(1000L);
UpdateClasspathJob.getInstance().updateClasspath();
}
if (!Objects.equals(oldPreferences.getResourceFilters(), newPreferences.getResourceFilters())) {
try {
configureFilters(new NullProgressMonitor());
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
}
}
};
this.preferenceManager.addPreferencesChangeListener(this.preferenceChangeListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.eclipse.jdt.ls.core.internal.handlers.MapFlattener.getValue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -73,6 +74,12 @@ public class Preferences {
* Preference key used to include the comments during the formatting.
*/
public static final String JAVA_FORMAT_COMMENTS = "java.format.comments.enabled";
/**
* Specifies filter applied on projects to exclude some file system objects
* while populating the resources tree.
*/
public static final String JAVA_RESOURCE_FILTERS = "java.project.resourceFilters";
public static final List<String> JAVA_RESOURCE_FILTERS_DEFAULT;
/**
* Preference key to enable/disable gradle importer.
*/
Expand Down Expand Up @@ -442,6 +449,7 @@ public class Preferences {
private int importOnDemandThreshold;
private int staticImportOnDemandThreshold;
private Set<RuntimeEnvironment> runtimes = new HashSet<>();
private List<String> resourceFilters;

static {
JAVA_IMPORT_EXCLUSIONS_DEFAULT = new LinkedList<>();
Expand All @@ -468,6 +476,7 @@ public class Preferences {
JAVA_COMPLETION_FILTERED_TYPES_DEFAULT = new ArrayList<>();
JAVA_COMPLETION_FILTERED_TYPES_DEFAULT.add("java.awt.*");
JAVA_COMPLETION_FILTERED_TYPES_DEFAULT.add("com.sun.*");
JAVA_RESOURCE_FILTERS_DEFAULT = Arrays.asList("node_modules", ".git");
}

public static enum Severity {
Expand Down Expand Up @@ -612,6 +621,7 @@ public Preferences() {
importOnDemandThreshold = IMPORTS_ONDEMANDTHRESHOLD_DEFAULT;
staticImportOnDemandThreshold = IMPORTS_STATIC_ONDEMANDTHRESHOLD_DEFAULT;
referencedLibraries = JAVA_PROJECT_REFERENCED_LIBRARIES_DEFAULT;
resourceFilters = JAVA_RESOURCE_FILTERS_DEFAULT;
}

/**
Expand Down Expand Up @@ -768,6 +778,9 @@ public static Preferences createFrom(Map<String, Object> configuration) {
String formatterUrl = getString(configuration, JAVA_FORMATTER_URL);
prefs.setFormatterUrl(formatterUrl);

List<String> resourceFilters = getList(configuration, JAVA_RESOURCE_FILTERS);
prefs.setResourceFilters(resourceFilters);

String formatterProfileName = getString(configuration, JAVA_FORMATTER_PROFILE_NAME);
prefs.setFormatterProfileName(formatterProfileName);

Expand Down Expand Up @@ -891,6 +904,11 @@ public Preferences setFormatterUrl(String formatterUrl) {
return this;
}

public Preferences setResourceFilters(List<String> resourceFilters) {
this.resourceFilters = resourceFilters == null ? new ArrayList<>() : resourceFilters;
return this;
}

public Preferences setJavaFormatComments(boolean javaFormatComments) {
this.javaFormatComments = javaFormatComments;
return this;
Expand Down Expand Up @@ -1151,6 +1169,10 @@ public String getFormatterUrl() {
return formatterUrl;
}

public List<String> getResourceFilters() {
return resourceFilters;
}

public String getFormatterProfileName() {
return formatterProfileName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@
package org.eclipse.jdt.ls.core.internal.managers;

import static org.eclipse.jdt.ls.core.internal.JobHelpers.waitForJobsToComplete;
import static org.eclipse.jdt.ls.core.internal.WorkspaceHelper.getProject;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.internal.resources.Resource;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.ls.core.internal.BuildWorkspaceStatus;
import org.eclipse.jdt.ls.core.internal.JavaClientConnection.JavaLanguageClient;
Expand Down Expand Up @@ -129,4 +134,42 @@ public void testCancelUpdateJob() throws Exception {
assertTrue("the update job hasn't been cancelled, status is: " + updateWorkspaceJob.getResult().getSeverity(), updateWorkspaceJob.getResult().matches(IStatus.CANCEL));
}

@SuppressWarnings("restriction")
@Test
public void testResourceFilters() throws Exception {
List<String> resourceFilters = preferenceManager.getPreferences().getResourceFilters();
try {
String name = "salut";
importProjects("maven/" + name);
IProject project = getProject(name);
assertIsJavaProject(project);
Resource nodeModules = (Resource) project.getFolder("/node_modules");
assertFalse(nodeModules.isFiltered());
Resource git = (Resource) project.getFolder("/.git");
assertFalse(git.isFiltered());
Resource src = (Resource) project.getFolder("/src");
assertFalse(src.isFiltered());
preferenceManager.getPreferences().setResourceFilters(Arrays.asList("node_modules", ".git"));
projectsManager.configureFilters(new NullProgressMonitor());
waitForJobsToComplete();
nodeModules = (Resource) project.getFolder("/node_modules");
assertTrue(nodeModules.isFiltered());
git = (Resource) project.getFolder("/.git");
assertTrue(git.isFiltered());
src = (Resource) project.getFolder("/src");
assertFalse(src.isFiltered());
preferenceManager.getPreferences().setResourceFilters(null);
projectsManager.configureFilters(new NullProgressMonitor());
waitForJobsToComplete();
nodeModules = (Resource) project.getFolder("/node_modules");
assertFalse(nodeModules.isFiltered());
git = (Resource) project.getFolder("/.git");
assertFalse(git.isFiltered());
src = (Resource) project.getFolder("/src");
assertFalse(src.isFiltered());
} finally {
preferenceManager.getPreferences().setResourceFilters(resourceFilters);
}
}

}

0 comments on commit d5ca624

Please sign in to comment.