Skip to content

Commit

Permalink
Support to specify source paths for unmanaged folder (#1658)
Browse files Browse the repository at this point in the history
* Support to specify source paths for unmanaged folder

Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo authored Mar 9, 2021
1 parent 1783bf3 commit 7150116
Show file tree
Hide file tree
Showing 9 changed files with 485 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Microsoft Corporation and others.
* Copyright (c) 2018-2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -13,6 +13,7 @@
package org.eclipse.jdt.ls.core.internal.commands;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -81,7 +82,9 @@ public static Result addToSourcePath(String sourceFolderUri) {
IJavaProject javaProject = JavaCore.create(targetProject);
try {
if (ProjectUtils.addSourcePath(sourcePath, exclusionPath, javaProject)) {
return new Result(true, Messages.format("Successfully added ''{0}'' to the project {1}''s source path.", new String[] { getWorkspacePath(sourceFolderPath).toOSString(), targetProject.getName() }));
Result result = new Result(true, Messages.format("Successfully added ''{0}'' to the project {1}''s source path.", new String[] { getWorkspacePath(sourceFolderPath).toOSString(), targetProject.getName() }));
result.sourcePaths = getInvisibleProjectRelativeSourcePaths(javaProject);
return result;
} else {
return new Result(true, Messages.format("No need to add it to source path again, because the folder ''{0}'' is already in the project {1}''s source path.",
new String[] { getWorkspacePath(sourceFolderPath).toOSString(), targetProject.getName() }));
Expand Down Expand Up @@ -126,7 +129,9 @@ public static Result removeFromSourcePath(String sourceFolderUri) {
IJavaProject javaProject = JavaCore.create(targetProject);
try {
if (ProjectUtils.removeSourcePath(sourcePath, javaProject)) {
return new Result(true, Messages.format("Successfully removed ''{0}'' from the project {1}''s source path.", new String[] { getWorkspacePath(sourceFolderPath).toOSString(), targetProject.getName() }));
Result result = new Result(true, Messages.format("Successfully removed ''{0}'' from the project {1}''s source path.", new String[] { getWorkspacePath(sourceFolderPath).toOSString(), targetProject.getName() }));
result.sourcePaths = getInvisibleProjectRelativeSourcePaths(javaProject);
return result;
} else {
return new Result(true, Messages.format("No need to remove it from source path, because the folder ''{0}'' isn''t on any project''s source path.", getWorkspacePath(sourceFolderPath).toOSString()));
}
Expand Down Expand Up @@ -184,6 +189,20 @@ public static Result listSourcePaths() {
return new ListCommandResult(true, null, sourcePathList.toArray(new SourcePath[0]));
}

private static String[] getInvisibleProjectRelativeSourcePaths(IJavaProject javaProject) throws JavaModelException {
if (ProjectUtils.isVisibleProject(javaProject.getProject())) {
return null;
}
IFolder workspaceLinkFolder = javaProject.getProject().getFolder(ProjectUtils.WORKSPACE_LINK);
if (!workspaceLinkFolder.isLinked()) {
return null;
}
IPath[] paths = ProjectUtils.listSourcePaths(javaProject);
return Arrays.stream(paths)
.map(p -> p.makeRelativeTo(workspaceLinkFolder.getFullPath()).toString())
.toArray(String[]::new);
}

private static IProject findBelongedProject(IPath sourceFolder) {
List<IProject> projects = Stream.of(ProjectUtils.getAllProjects()).filter(ProjectUtils::isJavaProject).sorted(new Comparator<IProject>() {
@Override
Expand Down Expand Up @@ -218,6 +237,7 @@ private static IPath getWorkspacePath(IPath path) {
public static class Result {
public boolean status;
public String message;
public String[] sourcePaths;

Result(boolean status, String message) {
this.status = status;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016-2017 Red Hat Inc. and others.
* Copyright (c) 2016-2021 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -17,7 +17,9 @@
import java.util.Optional;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
Expand Down Expand Up @@ -79,7 +81,11 @@ public ICompilationUnit resolveCompilationUnit(String uri) {
boolean invisibleProjectEnabled = false;
if (belongedRootPath.isPresent()) {
IPath rootPath = belongedRootPath.get();
invisibleProjectEnabled = InvisibleProjectImporter.loadInvisibleProject(filePath, rootPath, false);
try {
invisibleProjectEnabled = InvisibleProjectImporter.loadInvisibleProject(filePath, rootPath, false, new NullProgressMonitor());
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Failed to load invisible project", e);
}
if (invisibleProjectEnabled) {
unit = JDTUtils.resolveCompilationUnit(uri);
}
Expand Down
Loading

0 comments on commit 7150116

Please sign in to comment.