Skip to content

Commit

Permalink
Fix package name not recognized when opening standalone java files
Browse files Browse the repository at this point in the history
Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo authored and rgrunber committed May 14, 2021
1 parent 748b774 commit 09af6aa
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
public void initialized(InitializedParams params) {
logInfo(">> initialized");
try {
Job.getJobManager().join(InitHandler.JAVA_LS_INITIALIZATION_JOBS, null);
} catch (OperationCanceledException | InterruptedException e) {
JobHelpers.waitForInitializeJobs();
} catch (OperationCanceledException e) {
logException(e.getMessage(), e);
}
logInfo(">> initialization job finished");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@ public class InvisibleProjectPreferenceChangeListener implements IPreferencesCha

@Override
public void preferencesChange(Preferences oldPreferences, Preferences newPreferences) {
if (!Objects.equals(oldPreferences.getInvisibleProjectOutputPath(), newPreferences.getInvisibleProjectOutputPath()) ||
!Objects.equals(oldPreferences.getInvisibleProjectSourcePaths(), newPreferences.getInvisibleProjectSourcePaths())) {
for (IJavaProject javaProject : ProjectUtils.getJavaProjects()) {
IProject project = javaProject.getProject();
if (ProjectUtils.isVisibleProject(project)) {
continue;
}
if (project.equals(ProjectsManager.getDefaultProject())) {
continue;
}

try {
try {
if (!Objects.equals(oldPreferences.getInvisibleProjectSourcePaths(), newPreferences.getInvisibleProjectSourcePaths())) {
for (IJavaProject javaProject : ProjectUtils.getJavaProjects()) {
IProject project = javaProject.getProject();
if (ProjectUtils.isVisibleProject(project)) {
continue;
}
if (project.equals(ProjectsManager.getDefaultProject())) {
continue;
}

IFolder workspaceLinkFolder = javaProject.getProject().getFolder(ProjectUtils.WORKSPACE_LINK);
IPath rootPath = ProjectUtils.findBelongedWorkspaceRoot(workspaceLinkFolder.getLocation());
if (rootPath == null) {
Expand All @@ -56,10 +55,23 @@ public void preferencesChange(Preferences oldPreferences, Preferences newPrefere
IPath outputPath = InvisibleProjectImporter.getOutputPath(javaProject, newPreferences.getInvisibleProjectOutputPath(), true /*isUpdate*/);
IClasspathEntry[] classpathEntries = InvisibleProjectImporter.resolveClassPathEntries(javaProject, sourcePaths, excludingPaths, outputPath);
javaProject.setRawClasspath(classpathEntries, outputPath, new NullProgressMonitor());
} catch (CoreException e) {
JavaLanguageServerPlugin.getProjectsManager().getConnection().showMessage(new MessageParams(MessageType.Error, e.getMessage()));
}
} else if (!Objects.equals(oldPreferences.getInvisibleProjectOutputPath(), newPreferences.getInvisibleProjectOutputPath())) {
for (IJavaProject javaProject : ProjectUtils.getJavaProjects()) {
IProject project = javaProject.getProject();
if (ProjectUtils.isVisibleProject(project)) {
continue;
}
if (project.equals(ProjectsManager.getDefaultProject())) {
continue;
}

IPath outputPath = InvisibleProjectImporter.getOutputPath(javaProject, newPreferences.getInvisibleProjectOutputPath(), true /*isUpdate*/);
javaProject.setOutputLocation(outputPath, new NullProgressMonitor());
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.getProjectsManager().getConnection().showMessage(new MessageParams(MessageType.Error, e.getMessage()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ public void testUpdateOutputPath() throws Exception {
assertEquals(String.join("/", "", javaProject.getElementName(), ProjectUtils.WORKSPACE_LINK, "bin"), javaProject.getOutputLocation().toString());
}

@Test
public void testUpdateOutputPathWontAffectSourcePath() throws Exception {
preferenceManager.getPreferences().setInvisibleProjectOutputPath("");
IProject project = copyAndImportFolder("singlefile/simple", "src/App.java");
IJavaProject javaProject = JavaCore.create(project);

int originalSourcePathCount = 0;
for (IClasspathEntry entry : javaProject.getRawClasspath()) {
if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
originalSourcePathCount++;
}
}

Preferences newPreferences = new Preferences();
initPreferences(newPreferences);
newPreferences.setInvisibleProjectOutputPath("bin");
InvisibleProjectPreferenceChangeListener listener = new InvisibleProjectPreferenceChangeListener();
listener.preferencesChange(preferenceManager.getPreferences(), newPreferences);
waitForBackgroundJobs();

int newSourcePathCount = 0;
for (IClasspathEntry entry : javaProject.getRawClasspath()) {
if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
newSourcePathCount++;
}
}
assertEquals(originalSourcePathCount, newSourcePathCount);
}

@Test
public void testUpdateOutputPathToUnEmptyFolder() throws Exception {
copyAndImportFolder("singlefile/simple", "src/App.java");
Expand All @@ -85,7 +114,6 @@ public void testUpdateOutputPathToUnEmptyFolder() throws Exception {

@Test
public void testUpdateSourcePaths() throws Exception {
preferenceManager.getPreferences().setInvisibleProjectOutputPath("");
IProject project = copyAndImportFolder("singlefile/simple", "src/App.java");
IJavaProject javaProject = JavaCore.create(project);
IFolder linkFolder = project.getFolder(ProjectUtils.WORKSPACE_LINK);
Expand Down

0 comments on commit 09af6aa

Please sign in to comment.