diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java index c1df7fa866..59b1cf48a8 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java @@ -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; @@ -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()); diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/DocumentLifeCycleHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/DocumentLifeCycleHandlerTest.java index 54a356cd5f..8e62ec7f34 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/DocumentLifeCycleHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/DocumentLifeCycleHandlerTest.java @@ -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; @@ -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");