Skip to content
Open
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 @@ -27,6 +27,7 @@
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -54,7 +55,6 @@
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.jdt.core.CompletionProposal;
import org.eclipse.jdt.core.Flags;

import org.eclipse.jdt.core.IAnnotatable;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.IBuffer;
Expand All @@ -70,6 +70,7 @@
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.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.IType;
Expand Down Expand Up @@ -106,7 +107,6 @@
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
import org.eclipse.jdt.core.dom.Type;

import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
import org.eclipse.jdt.core.manipulation.SharedASTProviderCore;
Expand All @@ -117,6 +117,7 @@
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.manipulation.JavaElementLabelComposerCore;
import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelsCore;
import org.eclipse.jdt.internal.core.manipulation.search.IOccurrencesFinder.OccurrenceLocation;
Expand Down Expand Up @@ -1142,7 +1143,7 @@ public static IResource findResource(URI uri, Function<URI, IResource[]> resourc
if (uri == null || !"file".equals(uri.getScheme())) {
return null;
}
IResource[] resources = resourceFinder.apply(uri);
IResource[] resources = resourceFinder.apply(uri);
if (resources.length == 0) {
//On Mac, Linked resources are referenced via the "real" URI, i.e file://USERS/username/...
//instead of file://Users/username/..., so we check against that real URI.
Expand All @@ -1165,15 +1166,15 @@ public static IResource findResource(URI uri, Function<URI, IResource[]> resourc
}
resources = resourceFinder.apply(uri);
}
}
}
switch(resources.length) {
case 0:
return null;
case 1:
return resources[0];
default://several candidates if a linked resource was created before the real project was configured
IResource resource = null;
for (IResource f : resources) {
for (IResource f : resources) {
//delete linked resource
if (JavaLanguageServerPlugin.getProjectsManager().getDefaultProject().equals(f.getProject())) {
try {
Expand All @@ -1182,15 +1183,38 @@ public static IResource findResource(URI uri, Function<URI, IResource[]> resourc
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
}

//find closest project containing that file, in case of nested projects
if (resource == null || f.getProjectRelativePath().segmentCount() < resource.getProjectRelativePath().segmentCount()) {
resource = f;
//ignore files outside a source folder (https://github.com/eclipse-jdtls/eclipse.jdt.ls/issues/3447)
if (hasPackageFragmentRoot(f)
&& (resource == null || f.getProjectRelativePath().segmentCount() < resource.getProjectRelativePath().segmentCount())) {
resource = f;
}
}
return resource;
}
}

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");
}
}