Skip to content

Commit 978b93e

Browse files
committed
[performance] Cannot import maven project from workspace
1 parent 1643491 commit 978b93e

File tree

6 files changed

+103
-2
lines changed

6 files changed

+103
-2
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ public void update(Preferences preferences) {
227227
Hashtable<String, String> options = JavaCore.getOptions();
228228
preferences.updateTabSizeInsertSpaces(options);
229229
JavaCore.setOptions(options);
230+
List<String> resourceFilters = preferences.getResourceFilters();
231+
IEclipsePreferences eclipsePreferences = InstanceScope.INSTANCE.getNode(IConstants.PLUGIN_ID);
232+
eclipsePreferences.put(Preferences.JAVA_RESOURCE_FILTERS, String.join("::", resourceFilters));
230233
// TODO serialize preferences
231234
}
232235

org.eclipse.jdt.ls.filesystem/src/org/eclipse/jdt/ls/core/internal/filesystem/JDTLSFilesystemActivator.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package org.eclipse.jdt.ls.core.internal.filesystem;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Objects;
6+
import java.util.regex.Pattern;
7+
8+
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
9+
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
10+
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
11+
import org.eclipse.core.runtime.preferences.InstanceScope;
312
import org.osgi.framework.BundleActivator;
413
import org.osgi.framework.BundleContext;
514

@@ -15,17 +24,60 @@
1524
public class JDTLSFilesystemActivator implements BundleActivator {
1625

1726
private static BundleContext context;
27+
private static final String JAVA_LS_PLUGIN_ID = "org.eclipse.jdt.ls.core";
28+
private static final String JAVA_RESOURCE_FILTERS = "java.project.resourceFilters";
29+
private static final String JAVA_RESOURCE_FILTERS_DEFAULT = "node_modules::\\.git";
30+
private List<Pattern> resuorcePatterns;
31+
private String resourceFilters;
32+
private static JDTLSFilesystemActivator instance;
1833

1934
static BundleContext getContext() {
2035
return context;
2136
}
2237

2338
public void start(BundleContext bundleContext) throws Exception {
2439
JDTLSFilesystemActivator.context = bundleContext;
40+
this.instance = this;
41+
configureResourceFilters();
42+
}
43+
44+
private void configureResourceFilters() {
45+
IEclipsePreferences eclipsePreferences = InstanceScope.INSTANCE.getNode(JAVA_LS_PLUGIN_ID);
46+
if (eclipsePreferences != null) {
47+
resourceFilters = eclipsePreferences.get(JAVA_RESOURCE_FILTERS, JAVA_RESOURCE_FILTERS_DEFAULT);
48+
eclipsePreferences.addPreferenceChangeListener(new IPreferenceChangeListener() {
49+
50+
@Override
51+
public void preferenceChange(PreferenceChangeEvent event) {
52+
if (event.getNewValue() instanceof String newValue && Objects.equals(JAVA_RESOURCE_FILTERS, event.getKey()) && !Objects.equals(resourceFilters, event.getNewValue())) {
53+
resourceFilters = newValue;
54+
setResourcePatterns();
55+
}
56+
}
57+
});
58+
} else {
59+
resourceFilters = JAVA_RESOURCE_FILTERS_DEFAULT;
60+
}
61+
setResourcePatterns();
62+
}
63+
64+
protected void setResourcePatterns() {
65+
resuorcePatterns = new ArrayList<>();
66+
for (String element: resourceFilters.split("::")) {
67+
Pattern pattern = Pattern.compile(element);
68+
resuorcePatterns.add(pattern);
69+
}
2570
}
2671

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

76+
public static List<Pattern> getResuorcePatterns() {
77+
if (instance != null) {
78+
return instance.resuorcePatterns;
79+
}
80+
return null;
81+
}
82+
3183
}

org.eclipse.jdt.ls.filesystem/src/org/eclipse/jdt/ls/core/internal/filesystem/JLSFile.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.eclipse.core.filesystem.IFileStore;
2222
import org.eclipse.core.internal.filesystem.local.LocalFile;
23+
import org.eclipse.core.internal.preferences.EclipsePreferences;
2324
import org.eclipse.core.runtime.IPath;
2425
import org.eclipse.core.runtime.IProgressMonitor;
2526
import org.eclipse.core.runtime.Path;
@@ -50,6 +51,9 @@ public String[] childNames(int options, IProgressMonitor monitor) {
5051
}
5152

5253
IPath filePath = new Path(this.filePath);
54+
if (JLSFsUtils.isExcluded(filePath)) {
55+
return childNames;
56+
}
5357
String projectName = JLSFsUtils.getProjectNameIfLocationIsProjectRoot(filePath);
5458
if (projectName == null) {
5559
return childNames;
@@ -69,7 +73,7 @@ public String[] childNames(int options, IProgressMonitor monitor) {
6973
@Override
7074
public IFileStore getChild(String name) {
7175
IPath path = new Path(this.filePath).append(name);
72-
if (JLSFsUtils.shouldStoreInMetadataArea(path)) {
76+
if (JLSFsUtils.shouldStoreInMetadataArea(path) && !JLSFsUtils.isExcluded(path)) {
7377
IPath containerPath = JLSFsUtils.getContainerPath(path);
7478
String projectName = JLSFsUtils.getProjectNameIfLocationIsProjectRoot(containerPath);
7579
if (projectName == null) {
@@ -87,7 +91,7 @@ public IFileStore getChild(String name) {
8791
@Override
8892
public IFileStore getFileStore(IPath path) {
8993
IPath fullPath = new Path(this.filePath).append(path);
90-
if (JLSFsUtils.shouldStoreInMetadataArea(fullPath)) {
94+
if (JLSFsUtils.shouldStoreInMetadataArea(fullPath) && !JLSFsUtils.isExcluded(fullPath)) {
9195
IPath containerPath = JLSFsUtils.getContainerPath(fullPath);
9296
String projectName = JLSFsUtils.getProjectNameIfLocationIsProjectRoot(containerPath);
9397
if (projectName == null) {

org.eclipse.jdt.ls.filesystem/src/org/eclipse/jdt/ls/core/internal/filesystem/JLSFsUtils.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
import java.util.HashSet;
1818
import java.util.Objects;
1919
import java.util.Set;
20+
import java.util.regex.Matcher;
21+
import java.util.regex.Pattern;
2022

2123
import org.eclipse.core.internal.preferences.EclipsePreferences;
2224
import org.eclipse.core.resources.IContainer;
2325
import org.eclipse.core.resources.IProject;
2426
import org.eclipse.core.resources.IProjectDescription;
2527
import org.eclipse.core.resources.ResourcesPlugin;
2628
import org.eclipse.core.runtime.IPath;
29+
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
30+
import org.eclipse.core.runtime.preferences.InstanceScope;
2731
import org.eclipse.jdt.core.IJavaProject;
2832

2933
/**
@@ -110,6 +114,27 @@ static boolean isProjectMetadataFile(IPath location) {
110114
return true;
111115
}
112116

117+
/**
118+
* Check whether the given location is excluded
119+
* @param location file location.
120+
* @return whether the given location is excluded.
121+
*/
122+
public static boolean isExcluded(IPath path) {
123+
if (path != null && JDTLSFilesystemActivator.getResuorcePatterns() != null) {
124+
for (String segment: path.segments()) {
125+
for (Pattern pattern: JDTLSFilesystemActivator.getResuorcePatterns()) {
126+
Matcher m = pattern.matcher(segment);
127+
if (m.matches()) {
128+
return true;
129+
}
130+
}
131+
}
132+
return false;
133+
} else {
134+
return true;
135+
}
136+
}
137+
113138
/**
114139
* Get the container path of the given file path.
115140
* If the file path is a preferences file, the grand-parent container will be returned.

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/filesystem/JLSFsUtilsTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import static org.junit.Assert.assertFalse;
1717
import static org.junit.Assert.assertTrue;
1818

19+
import org.eclipse.core.runtime.IPath;
20+
import org.eclipse.core.runtime.Path;
21+
import org.junit.After;
1922
import org.junit.Test;
2023

2124
public class JLSFsUtilsTest {
@@ -35,4 +38,15 @@ public void testNotGeneratesMetadataFilesAtProjectRoot() {
3538
public void testGeneratesMetadataFilesAtProjectRootWhenNotSet() {
3639
assertTrue(JLSFsUtils.generatesMetadataFilesAtProjectRoot());
3740
}
41+
42+
@Test
43+
public void testExcluded() {
44+
IPath path = new Path("/project/node_modules");
45+
assertTrue(JLSFsUtils.isExcluded(path));
46+
}
47+
48+
@After
49+
public void cleanUp() throws Exception {
50+
System.clearProperty(JLSFsUtils.GENERATES_METADATA_FILES_AT_PROJECT_ROOT);
51+
}
3852
}

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/AbstractProjectsManagerBasedTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public abstract class AbstractProjectsManagerBasedTest {
9797

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

100+
private static final String GENERATES_METADATA_FILES_AT_PROJECT_ROOT = "java.import.generatesMetadataFilesAtProjectRoot";
101+
100102
protected IProgressMonitor monitor;
101103
protected StandardProjectsManager projectsManager;
102104
@Mock
@@ -302,6 +304,7 @@ public void cleanUp() throws Exception {
302304
}
303305
ResourcesPlugin.getWorkspace().save(true/*full save*/, null/*no progress*/);
304306
CoreASTProvider.getInstance().disposeAST();
307+
System.clearProperty(GENERATES_METADATA_FILES_AT_PROJECT_ROOT);
305308
}
306309

307310
protected void assertIsJavaProject(IProject project) {

0 commit comments

Comments
 (0)