Skip to content

Commit

Permalink
enable syntax mode when importing a partial folder of maven/gradle pr…
Browse files Browse the repository at this point in the history
…oject

Signed-off-by: Jinbo Wang <[email protected]>
  • Loading branch information
testforstephen committed Mar 5, 2020
1 parent 75a7033 commit fdd9152
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public static boolean loadInvisibleProject(IPath javaFile, IPath rootPath, boole

String packageName = getPackageName(javaFile, rootPath);
IPath sourceDirectory = inferSourceDirectory(javaFile.toFile().toPath(), packageName);
if (sourceDirectory == null || !rootPath.isPrefixOf(sourceDirectory)) {
if (sourceDirectory == null || !rootPath.isPrefixOf(sourceDirectory)
|| isPartOfMatureProject(sourceDirectory)) {
return false;
}

Expand Down Expand Up @@ -140,6 +141,20 @@ public static boolean loadInvisibleProject(IPath javaFile, IPath rootPath, boole
return true;
}

private static boolean isPartOfMatureProject(IPath sourcePath) {
sourcePath = sourcePath.removeTrailingSeparator();
List<String> segments = Arrays.asList(sourcePath.segments());
int index = segments.lastIndexOf("src");
if (index <= 0) {
return false;
}

IPath srcPath = sourcePath.removeLastSegments(segments.size() -1 - index);
IPath container = srcPath.removeLastSegments(1);
return container.append("pom.xml").toFile().exists()
|| container.append("build.gradle").toFile().exists();
}

private static String getPackageName(IPath javaFile, IPath workspaceRoot) {
IProject project = JavaLanguageServerPlugin.getProjectsManager().getDefaultProject();
if (project == null || !project.isAccessible()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ protected void addLibs(java.nio.file.Path projectPath) throws Exception {

protected IProject importRootFolder(File projectFolder, String triggerFile) throws Exception {
IPath rootPath = Path.fromOSString(projectFolder.getAbsolutePath());
return importRootFolder(rootPath, triggerFile);
}

protected IProject importRootFolder(IPath rootPath, String triggerFile) throws Exception {
if (StringUtils.isNotBlank(triggerFile)) {
IPath triggerFilePath = rootPath.append(triggerFile);
Preferences preferences = preferenceManager.getPreferences();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ public void importCompleteFolderWithoutTriggerFile() throws Exception {
assertFalse(invisibleProject.exists());
}

@Test
public void importPartialMavenFolder() throws Exception {
File projectFolder = copyFiles("maven/salut-java11", true);
IPath projectFullPath = Path.fromOSString(projectFolder.getAbsolutePath());
IPath rootPath = projectFullPath.append("src");
IProject invisibleProject = importRootFolder(rootPath, "main/java/org/sample/Bar.java");
assertFalse(invisibleProject.exists());
}

@Test
public void importPartialGradleFolder() throws Exception {
File projectFolder = copyFiles("gradle/gradle-11", true);
IPath projectFullPath = Path.fromOSString(projectFolder.getAbsolutePath());
IPath rootPath = projectFullPath.append("src");
IProject invisibleProject = importRootFolder(rootPath, "main/java/foo/bar/Foo.java");
assertFalse(invisibleProject.exists());
}

@Test
public void automaticJarDetectionLibUnderSource() throws Exception {
ClientPreferences mockCapabilies = mock(ClientPreferences.class);
Expand Down

0 comments on commit fdd9152

Please sign in to comment.