Skip to content

Commit 89a1809

Browse files
authored
fix: Get correct Java project in multi-module case (#1865)
* fix: Get correct Java project in multi-module case Signed-off-by: sheche <[email protected]>
1 parent da11a7a commit 89a1809

File tree

6 files changed

+62
-2
lines changed

6 files changed

+62
-2
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommand.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,13 @@ public static IJavaProject getJavaProjectFromUri(String uri) throws CoreExceptio
271271

272272
// For multi-module scenario
273273
Arrays.sort(containers, (Comparator<IContainer>) (IContainer a, IContainer b) -> {
274-
return a.getFullPath().toPortableString().length() - b.getFullPath().toPortableString().length();
274+
return a.getFullPath().segmentCount() - b.getFullPath().segmentCount();
275275
});
276276

277277
IJavaElement targetElement = null;
278278
for (IContainer container : containers) {
279279
targetElement = JavaCore.create(container.getProject());
280-
if (targetElement != null) {
280+
if (targetElement != null && targetElement.exists()) {
281281
break;
282282
}
283283
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.eclipse</groupId>
7+
<artifactId>multimodule3</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>org.eclipse</groupId>
11+
<artifactId>this_is_a_very_long_module_name</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>this_is_a_very_long_module_name</name>
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>4.13</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.eclipse;
2+
3+
public class App {
4+
public static final String GREETING = "Hello World!";
5+
6+
public static void main(String[] args) {
7+
System.out.println(GREETING);
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.eclipse;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
7+
public class AppTest {
8+
@Test
9+
public void testApp() {
10+
assertTrue(true);
11+
}
12+
}

org.eclipse.jdt.ls.tests/projects/maven/multimodule3/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<modules>
1212
<module>module1</module>
1313
<module>module2</module>
14+
<module>module3</module>
1415
</modules>
1516

1617
<build>

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommandTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ public void testGetMavenProjectFromUri() throws Exception {
130130
assertNotNull("Can get project from project uri", javaProject);
131131
}
132132

133+
@Test
134+
public void testGetMultiModuleMavenProjectFromUri() throws Exception {
135+
importProjects("maven/multimodule3");
136+
IProject project = WorkspaceHelper.getProject("this_is_a_very_long_module_name");
137+
String javaSource = project.getFile("src/main/org/eclipse/App.java").getLocationURI().toString();
138+
IJavaProject javaProject = ProjectCommand.getJavaProjectFromUri(javaSource);
139+
assertEquals("this_is_a_very_long_module_name", javaProject.getElementName());
140+
141+
String projectUri = project.getLocationURI().toString();
142+
javaProject = ProjectCommand.getJavaProjectFromUri(projectUri);
143+
assertEquals("this_is_a_very_long_module_name", javaProject.getElementName());
144+
}
145+
133146
@Test
134147
public void testGetInvisibleProjectFromUri() throws Exception {
135148
IProject project = copyAndImportFolder("singlefile/simple", "src/App.java");

0 commit comments

Comments
 (0)