Skip to content

Commit

Permalink
Improve JDTUtils.resolveCompilationUnit
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza committed Oct 31, 2024
1 parent c616098 commit b226f36
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ public static ICompilationUnit resolveCompilationUnit(URI uri) {

IFile resource = (IFile) findResource(uri, ResourcesPlugin.getWorkspace().getRoot()::findFilesForLocationURI);
if(resource != null) {
for (ICompilationUnit workingCopy : JavaCore.getWorkingCopies(null)) {
if (toURI(toURI(workingCopy)).equals(uri)) {
return workingCopy;
}
}
return resolveCompilationUnit(resource);
} else {
return getFakeCompilationUnit(uri, new NullProgressMonitor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -340,6 +342,25 @@ public void testReconcile() throws Exception {
closeDocument(cu1);
}

@Test
public void testWorkingCopies() throws Exception {
importProjects("eclipse/hello");
IProject project = WorkspaceHelper.getProject("hello");
URI uri = project.getFile("/src/org/sample/Foo.java").getRawLocationURI();
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri);
String source = Files.readString(FileUtils.toFile(uri.toURL()).toPath());
openDocument(cu, source, 1);
Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, monitor);
ICompilationUnit cu1 = JDTUtils.resolveCompilationUnit(uri);
ICompilationUnit cu2 = JDTUtils.resolveCompilationUnit(uri);
assertSame(cu1, cu2);
closeDocument(cu);
Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, monitor);
cu1 = JDTUtils.resolveCompilationUnit(uri);
cu2 = JDTUtils.resolveCompilationUnit(uri);
assertNotSame(cu1, cu2);
}

@Test
public void testNonJdtError() throws Exception {
importProjects("eclipse/hello");
Expand Down

0 comments on commit b226f36

Please sign in to comment.