Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot import maven project from workspace #2999

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have some unused imports here. IEclipsePreferences and InstanceScope.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just something to be aware of :
Although I'd have preferred making jdt.ls.filesystem directly depend on jdt.ls.core I'll allow this copying because in the long term , we have #2658, so we should avoid any link between this bundle and jdt.ls.core).

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,6 +17,8 @@
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;
Expand Down Expand Up @@ -110,6 +112,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