Skip to content

Commit

Permalink
Renaming or creating a java file requires restart
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza authored and fbricon committed Dec 13, 2017
1 parent 785ab5d commit 4854b40
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
import java.util.List;
import java.util.Set;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.IFileBuffer;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
Expand All @@ -33,6 +30,8 @@
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.IProblem;
Expand Down Expand Up @@ -210,6 +209,13 @@ public void handleOpen(DidOpenTextDocumentParams params) {
if (!unit.getResource().isAccessible()) {
try {
unit.getResource().refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor());
if (unit.getResource().exists()) {
IJavaElement parent = unit.getParent();
if (parent instanceof IPackageFragment) {
IPackageFragment pkg = (IPackageFragment) parent;
unit = pkg.createCompilationUnit(unit.getElementName(), unit.getSource(), true, new NullProgressMonitor());
}
}
} catch (CoreException e) {
// ignored
}
Expand Down Expand Up @@ -322,14 +328,10 @@ public void handleSaved(DidSaveTextDocumentParams params) {
}
// see https://github.com/redhat-developer/vscode-java/issues/274
unit = checkPackageDeclaration(uri, unit);
IFileBuffer fileBuffer = FileBuffers.getTextFileBufferManager().getFileBuffer(unit.getPath(), LocationKind.IFILE);
if (fileBuffer != null) {
fileBuffer.setDirty(false);
}
if (unit.isWorkingCopy()) {
try {
projectsManager.fileChanged(uri, CHANGE_TYPE.CHANGED);
unit.getBuffer().close();
unit.commitWorkingCopy(true, new NullProgressMonitor());
} catch (Exception e) {
JavaLanguageServerPlugin.logException("Error while handling document save", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@
import java.util.Collections;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaClientConnection;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.ResourceUtils;
import org.eclipse.jdt.ls.core.internal.SharedASTProvider;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
Expand Down Expand Up @@ -56,6 +65,21 @@ void didChangeWatchedFiles(DidChangeWatchedFilesParams param){
cleanUpDiagnostics(fileEvent.getUri());
}
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(fileEvent.getUri());
if (unit != null && changeType == CHANGE_TYPE.CREATED && !unit.exists()) {
final ICompilationUnit[] units = new ICompilationUnit[1];
units[0] = unit;
try {
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException {
units[0] = createCompilationUnit(units[0]);
}
}, new NullProgressMonitor());
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
unit = units[0];
}
if (unit != null && unit.isWorkingCopy()) {
continue;
}
Expand All @@ -66,6 +90,22 @@ void didChangeWatchedFiles(DidChangeWatchedFilesParams param){
}
}

private ICompilationUnit createCompilationUnit(ICompilationUnit unit) {
try {
unit.getResource().refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor());
if (unit.getResource().exists()) {
IJavaElement parent = unit.getParent();
if (parent instanceof IPackageFragment) {
IPackageFragment pkg = (IPackageFragment) parent;
unit = pkg.createCompilationUnit(unit.getElementName(), unit.getSource(), true, new NullProgressMonitor());
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
return unit;
}

private void cleanUpDiagnostics(String uri){
this.connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), Collections.emptyList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertRange;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.IOException;
Expand All @@ -23,7 +24,9 @@
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.ICompilationUnit;
Expand Down Expand Up @@ -454,6 +457,45 @@ public void testNotExpectedPackage() throws Exception {
assertEquals("Unexpected number of errors", 0, problems.length);
}

@Test
public void testCreateCompilationUnit() throws Exception {
IJavaProject javaProject = newEmptyProject();
// @formatter:off
String fooContent =
"package org;\n"+
"public class Foo {"+
"}\n";
String barContent =
"package org;\n"+
"public class Bar {\n"+
" Foo test() { return null; }\n" +
"}\n";
// @formatter:on
IFolder src = javaProject.getProject().getFolder("src");
javaProject.getPackageFragmentRoot(src);
File sourceDirectory = src.getRawLocation().makeAbsolute().toFile();
File org = new File(sourceDirectory, "org");
org.mkdir();
File file = new File(org, "Bar.java");
file.createNewFile();
FileUtils.writeStringToFile(file, barContent);
ICompilationUnit bar = JDTUtils.resolveCompilationUnit(file.toURI());
bar.getResource().refreshLocal(IResource.DEPTH_ONE, null);
assertNotNull("Bar doesn't exist", javaProject.findType("org.Bar"));
file = new File(org, "Foo.java");
file.createNewFile();
URI uri = file.toURI();
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
openDocument(unit, "", 1);
FileUtils.writeStringToFile(file, fooContent);
changeDocumentFull(unit, fooContent, 1);
saveDocument(unit);
closeDocument(unit);
CompilationUnit astRoot = sharedASTProvider.getAST(bar, null);
IProblem[] problems = astRoot.getProblems();
assertEquals("Unexpected number of errors", 0, problems.length);
}

@Test
public void testNotExpectedPackage2() throws Exception {
newDefaultProject();
Expand Down

0 comments on commit 4854b40

Please sign in to comment.