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..393b2efb1b 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 @@ -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 (uri.equals(toURI(toURI(workingCopy)))) { + 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");