Skip to content
Open
Show file tree
Hide file tree
Changes from 17 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 @@ -27,6 +27,8 @@
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -70,6 +72,8 @@
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IOpenable;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.IType;
Expand Down Expand Up @@ -117,6 +121,8 @@
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
import org.eclipse.jdt.internal.core.NamedMember;
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
import org.eclipse.jdt.internal.core.PackageFragmentRoot;
import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelComposerCore;
import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelsCore;
import org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation;
Expand Down Expand Up @@ -1182,6 +1188,8 @@ public static IResource findResource(URI uri, Function<URI, IResource[]> resourc
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
}

//find the resource which has PackageFragmentRoot
//find closest project containing that file, in case of nested projects

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
//find the resource which has PackageFragmentRoot
//find closest project containing that file, in case of nested projects
//find closest project containing that file, in case of nested projects
//ignore files outside a source folder (https://github.com/eclipse-jdtls/eclipse.jdt.ls/issues/3447)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

However, the method name findResource no longer matches the contract now. This should be addressed.

if (resource == null || f.getProjectRelativePath().segmentCount() < resource.getProjectRelativePath().segmentCount()) {
resource = f;
Expand All @@ -1191,6 +1199,26 @@ public static IResource findResource(URI uri, Function<URI, IResource[]> resourc
}
}

private static boolean hasPackageFragmentRoot(IResource resource) {
ICompilationUnit unit = resolveCompilationUnit((IFile)resource);
if (unit == null || unit.getJavaProject() == null) {
return false;
}
try {;
IPath path = unit.getPath();

IPackageFragmentRoot[] packageFragmentRoots = unit.getJavaProject().getPackageFragmentRoots();
boolean containsPackageFragmentRoot = Arrays.stream(packageFragmentRoots)
.anyMatch(root -> root.getClass().equals(PackageFragmentRoot.class)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That's odd. Don't use getClass().equals(..). Use isInstance. Don't use internal classes (eg., PackageFragmentRoot). Use official JDT API only. Also, this check is unnecessary. The array is already IPackageFragmentRoot

&& packageFragmentRoots[0].getPath().isPrefixOf(path));

return containsPackageFragmentRoot;
} catch (JavaModelException e) {
JavaLanguageServerPlugin.log(e);
return false;
}
}

public static URI toURI(String uriString) {
if (uriString == null || uriString.isEmpty()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/>
<classpathentry kind="output" path="eclipse-bin"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="linked-srcs" />
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/>
<classpathentry kind="output" path="eclipse-bin"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>mypackage_mytarget</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<linkedResources>
<link>
<name>linked-srcs/com/packages/foo/File.java</name>
<type>1</type>
<location>PARENT-3-PROJECT_LOC/com/packages/foo/File.java</location>
</link>
</linkedResources>
<filteredResources>
<filter>
<id>1748456351890</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
Binary file not shown.
34 changes: 34 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/maven/nested-project/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>workspace_root</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1748456351890</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.packages.foo;

public class File
{
public static void main(String[] args)
{
System.out.println("Hello, World");
}
}