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 633d972
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
import org.eclipse.jdt.internal.codeassist.impl.Engine;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
import org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner;
import org.eclipse.jdt.internal.core.JavaModelManager;
import org.eclipse.jdt.internal.core.NamedMember;
import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelComposerCore;
import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelsCore;
Expand Down Expand Up @@ -196,6 +198,14 @@ public static ICompilationUnit resolveCompilationUnit(URI uri) {

IFile resource = (IFile) findResource(uri, ResourcesPlugin.getWorkspace().getRoot()::findFilesForLocationURI);
if(resource != null) {
ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(DefaultWorkingCopyOwner.PRIMARY, false);
if (workingCopies != null) {
for (ICompilationUnit workingCopy : workingCopies) {
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 633d972

Please sign in to comment.