Skip to content

Commit

Permalink
[performance] Cannot import maven project from workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
snjeza committed Dec 28, 2023
1 parent 2b7857b commit f1728dd
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ public void update(Preferences preferences) {
Hashtable<String, String> options = JavaCore.getOptions();
preferences.updateTabSizeInsertSpaces(options);
JavaCore.setOptions(options);
List<String> resourceFilters = preferences.getResourceFilters();
IEclipsePreferences eclipsePreferences = InstanceScope.INSTANCE.getNode(IConstants.PLUGIN_ID);
// add the resourceFilters preference; the org.eclipse.jdt.ls.filesystem plugin uses it
eclipsePreferences.put(Preferences.JAVA_RESOURCE_FILTERS, String.join("::", resourceFilters));
// TODO serialize preferences
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package org.eclipse.jdt.ls.core.internal.filesystem;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

Expand All @@ -15,17 +24,60 @@
public class JDTLSFilesystemActivator implements BundleActivator {

private static BundleContext context;
private static final String JAVA_LS_PLUGIN_ID = "org.eclipse.jdt.ls.core";
private static final String JAVA_RESOURCE_FILTERS = "java.project.resourceFilters";
private static final String JAVA_RESOURCE_FILTERS_DEFAULT = "node_modules::\\.git";
private List<Pattern> resourcePatterns;
private String resourceFilters;
private static JDTLSFilesystemActivator instance;

static BundleContext getContext() {
return context;
}

public void start(BundleContext bundleContext) throws Exception {
JDTLSFilesystemActivator.context = bundleContext;
JDTLSFilesystemActivator.instance = this;
configureResourceFilters();
}

private void configureResourceFilters() {
IEclipsePreferences eclipsePreferences = InstanceScope.INSTANCE.getNode(JAVA_LS_PLUGIN_ID);
if (eclipsePreferences != null) {
resourceFilters = eclipsePreferences.get(JAVA_RESOURCE_FILTERS, JAVA_RESOURCE_FILTERS_DEFAULT);
eclipsePreferences.addPreferenceChangeListener(new IPreferenceChangeListener() {

@Override
public void preferenceChange(PreferenceChangeEvent event) {
if (event.getNewValue() instanceof String newValue && Objects.equals(JAVA_RESOURCE_FILTERS, event.getKey()) && !Objects.equals(resourceFilters, event.getNewValue())) {
resourceFilters = newValue;
setResourcePatterns();
}
}
});
} else {
resourceFilters = JAVA_RESOURCE_FILTERS_DEFAULT;
}
setResourcePatterns();
}

protected void setResourcePatterns() {
resourcePatterns = new ArrayList<>();
for (String element: resourceFilters.split("::")) {
Pattern pattern = Pattern.compile(element);
resourcePatterns.add(pattern);
}
}

public void stop(BundleContext bundleContext) throws Exception {
JDTLSFilesystemActivator.context = null;
}

public static List<Pattern> getResourcePatterns() {
if (instance != null) {
return instance.resourcePatterns;
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.internal.filesystem.local.LocalFile;
import org.eclipse.core.internal.preferences.EclipsePreferences;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
Expand Down Expand Up @@ -50,6 +51,9 @@ public String[] childNames(int options, IProgressMonitor monitor) {
}

IPath filePath = new Path(this.filePath);
if (JLSFsUtils.isExcluded(filePath)) {
return childNames;
}
String projectName = JLSFsUtils.getProjectNameIfLocationIsProjectRoot(filePath);
if (projectName == null) {
return childNames;
Expand All @@ -69,7 +73,7 @@ public String[] childNames(int options, IProgressMonitor monitor) {
@Override
public IFileStore getChild(String name) {
IPath path = new Path(this.filePath).append(name);
if (JLSFsUtils.shouldStoreInMetadataArea(path)) {
if (JLSFsUtils.shouldStoreInMetadataArea(path) && !JLSFsUtils.isExcluded(path)) {
IPath containerPath = JLSFsUtils.getContainerPath(path);
String projectName = JLSFsUtils.getProjectNameIfLocationIsProjectRoot(containerPath);
if (projectName == null) {
Expand All @@ -87,7 +91,7 @@ public IFileStore getChild(String name) {
@Override
public IFileStore getFileStore(IPath path) {
IPath fullPath = new Path(this.filePath).append(path);
if (JLSFsUtils.shouldStoreInMetadataArea(fullPath)) {
if (JLSFsUtils.shouldStoreInMetadataArea(fullPath) && !JLSFsUtils.isExcluded(fullPath)) {
IPath containerPath = JLSFsUtils.getContainerPath(fullPath);
String projectName = JLSFsUtils.getProjectNameIfLocationIsProjectRoot(containerPath);
if (projectName == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.internal.preferences.EclipsePreferences;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.core.IJavaProject;

/**
Expand Down Expand Up @@ -110,6 +114,27 @@ static boolean isProjectMetadataFile(IPath location) {
return true;
}

/**
* Check whether the given location is excluded
* @param location file location.
* @return whether the given location is excluded.
*/
public static boolean isExcluded(IPath path) {
if (path != null && JDTLSFilesystemActivator.getResourcePatterns() != null) {
for (String segment: path.segments()) {
for (Pattern pattern: JDTLSFilesystemActivator.getResourcePatterns()) {
Matcher m = pattern.matcher(segment);
if (m.matches()) {
return true;
}
}
}
return false;
} else {
return true;
}
}

/**
* Get the container path of the given file path.
* If the file path is a preferences file, the grand-parent container will be returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.junit.After;
import org.junit.Test;

public class JLSFsUtilsTest {
Expand All @@ -35,4 +38,15 @@ public void testNotGeneratesMetadataFilesAtProjectRoot() {
public void testGeneratesMetadataFilesAtProjectRootWhenNotSet() {
assertTrue(JLSFsUtils.generatesMetadataFilesAtProjectRoot());
}

@Test
public void testExcluded() {
IPath path = new Path("/project/node_modules");
assertTrue(JLSFsUtils.isExcluded(path));
}

@After
public void cleanUp() throws Exception {
System.clearProperty(JLSFsUtils.GENERATES_METADATA_FILES_AT_PROJECT_ROOT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public abstract class AbstractProjectsManagerBasedTest {

private static final java.lang.String REFERENCE_PREFIX = "reference:";

private static final String GENERATES_METADATA_FILES_AT_PROJECT_ROOT = "java.import.generatesMetadataFilesAtProjectRoot";

protected IProgressMonitor monitor;
protected StandardProjectsManager projectsManager;
@Mock
Expand Down Expand Up @@ -302,6 +304,7 @@ public void cleanUp() throws Exception {
}
ResourcesPlugin.getWorkspace().save(true/*full save*/, null/*no progress*/);
CoreASTProvider.getInstance().disposeAST();
System.clearProperty(GENERATES_METADATA_FILES_AT_PROJECT_ROOT);
}

protected void assertIsJavaProject(IProject project) {
Expand Down

0 comments on commit f1728dd

Please sign in to comment.