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 13, 2023
1 parent 1643491 commit 0d82fcd
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ 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);
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> resuorcePatterns;
private String resourceFilters;
private static JDTLSFilesystemActivator instance;

static BundleContext getContext() {
return context;
}

public void start(BundleContext bundleContext) throws Exception {
JDTLSFilesystemActivator.context = bundleContext;
this.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() {
resuorcePatterns = new ArrayList<>();
for (String element: resourceFilters.split("::")) {
Pattern pattern = Pattern.compile(element);
resuorcePatterns.add(pattern);
}
}

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

public static List<Pattern> getResuorcePatterns() {
if (instance != null) {
return instance.resuorcePatterns;
}
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.getResuorcePatterns() != null) {
for (String segment: path.segments()) {
for (Pattern pattern: JDTLSFilesystemActivator.getResuorcePatterns()) {
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,8 @@
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.Test;

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

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

0 comments on commit 0d82fcd

Please sign in to comment.