Skip to content

Commit

Permalink
Improve document life cycle.
Browse files Browse the repository at this point in the history
Adds a textdocument/didSave handler. Handles the
case when a file is opened but not yet known to eclipse
workspace because the Workspace added element is not received
Also clears the diagnostics for a deleted file.
  • Loading branch information
gorkem committed Sep 7, 2016
1 parent 4970d0c commit 23e4573
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.jboss.tools.langs.DidChangeWatchedFilesParams;
import org.jboss.tools.langs.DidCloseTextDocumentParams;
import org.jboss.tools.langs.DidOpenTextDocumentParams;
import org.jboss.tools.langs.DidSaveTextDocumentParams;
import org.jboss.tools.langs.DocumentFormattingParams;
import org.jboss.tools.langs.DocumentHighlight;
import org.jboss.tools.langs.DocumentRangeFormattingParams;
Expand All @@ -35,6 +36,7 @@ public enum LSPMethods {
DOCUMENT_OPENED("textDocument/didOpen",DidOpenTextDocumentParams.class, Object.class),
DOCUMENT_CLOSED("textDocument/didClose",DidCloseTextDocumentParams.class, Object.class),
DOCUMENT_CHANGED("textDocument/didChange",DidChangeTextDocumentParams.class, Object.class),
DOCUMENT_SAVED("textDocument/didSave",DidSaveTextDocumentParams.class, Object.class),
DOCUMENT_HOVER("textDocument/hover", TextDocumentPositionParams.class, Hover.class),
DOCUMENT_DEFINITION("textDocument/definition", TextDocumentPositionParams.class, Location.class),
DOCUMENT_REFERENCES("textDocument/references", ReferenceParams.class, List.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.List;

import org.jboss.tools.langs.LogMessageParams;
import org.jboss.tools.langs.base.LSPException;
import org.jboss.tools.langs.base.LSPMethods;
import org.jboss.tools.langs.base.LSPServer;
import org.jboss.tools.langs.base.NotificationMessage;
Expand Down Expand Up @@ -53,9 +52,10 @@ private List<RequestHandler<?,?>> handlers(ProjectsManager pm) {
handlers.add(dh.new ChangeHandler());
handlers.add(dh.new ClosedHandler());
handlers.add(dh.new OpenHandler());
handlers.add(dh.new SaveHandler());
handlers.add(new CompletionHandler());
handlers.add(new NavigateToDefinitionHandler());
handlers.add(new WorkspaceEventsHandler(pm));
handlers.add(new WorkspaceEventsHandler(pm,this));
handlers.add(new DocumentSymbolHandler());
handlers.add(new WorkspaceSymbolHandler());
handlers.add(new ReferencesHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.filebuffers.LocationKind;
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.IBuffer;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.text.IDocument;
Expand All @@ -22,6 +24,7 @@
import org.jboss.tools.langs.DidChangeTextDocumentParams;
import org.jboss.tools.langs.DidCloseTextDocumentParams;
import org.jboss.tools.langs.DidOpenTextDocumentParams;
import org.jboss.tools.langs.DidSaveTextDocumentParams;
import org.jboss.tools.langs.Range;
import org.jboss.tools.langs.TextDocumentContentChangeEvent;
import org.jboss.tools.langs.base.LSPMethods;
Expand Down Expand Up @@ -100,7 +103,20 @@ public void run(IProgressMonitor monitor) throws CoreException {
}
return null;
}

}

public class SaveHandler implements RequestHandler<DidSaveTextDocumentParams, Object>{

@Override
public boolean canHandle(String request) {
return LSPMethods.DOCUMENT_SAVED.getMethod().equals(request);
}

@Override
public Object handle(DidSaveTextDocumentParams param) {
// Nothing to do just keeping the clients happy with a response
return null;
}
}

public DocumentLifeCycleHandler(JavaClientConnection connection) {
Expand All @@ -113,7 +129,21 @@ private void handleOpen(DidOpenTextDocumentParams params) {
return;
}
try {
// ToDo wire up cancelation.
// The open event can happen before the workspace element added event when a new file is added.
// checks if the underlying resource exists and refreshes to sync the newly created file.
if(unit.getResource() != null && !unit.getResource().isAccessible()){
try {
unit.getResource().refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor());
} catch (CoreException e) {
// ignored
}
}

IBuffer buffer = unit.getBuffer();
if(buffer != null)
buffer.setContents(params.getTextDocument().getText());

// TODO: wire up cancellation.
unit.becomeWorkingCopy(new DiagnosticsHandler(connection, unit.getUnderlyingResource()), null);
unit.reconcile();
} catch (JavaModelException e) {
Expand All @@ -131,7 +161,6 @@ private void handleChanged(DidChangeTextDocumentParams params) {
ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
ITextFileBuffer buffer = manager.getTextFileBuffer(unit.getResource().getFullPath(), LocationKind.IFILE);
IDocument document = buffer.getDocument();

try {
MultiTextEdit root = new MultiTextEdit();
List<TextDocumentContentChangeEvent> contentChanges = params.getContentChanges();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package org.jboss.tools.vscode.java.handlers;

import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.core.ICompilationUnit;
import org.jboss.tools.langs.DidChangeWatchedFilesParams;
import org.jboss.tools.langs.FileEvent;
import org.jboss.tools.langs.PublishDiagnosticsParams;
import org.jboss.tools.langs.base.LSPMethods;
import org.jboss.tools.langs.base.NotificationMessage;
import org.jboss.tools.vscode.ipc.RequestHandler;
import org.jboss.tools.vscode.java.JavaClientConnection;
import org.jboss.tools.vscode.java.managers.ProjectsManager;
import org.jboss.tools.vscode.java.managers.ProjectsManager.CHANGE_TYPE;

public class WorkspaceEventsHandler extends AbstractRequestHandler implements RequestHandler<DidChangeWatchedFilesParams, Object> {

private final ProjectsManager pm ;
private final JavaClientConnection connection;

public WorkspaceEventsHandler(ProjectsManager projects) {
public WorkspaceEventsHandler(ProjectsManager projects, JavaClientConnection connection ) {
this.pm = projects;
this.connection = connection;
}

@Override
Expand All @@ -40,12 +46,25 @@ private CHANGE_TYPE toChangeType(Number vtype){
public Object handle(DidChangeWatchedFilesParams param) {
List<FileEvent> changes = param.getChanges();
for (FileEvent fileEvent : changes) {
Double eventType = fileEvent.getType();
ICompilationUnit unit = resolveCompilationUnit(fileEvent.getUri());
if(toChangeType(eventType)==CHANGE_TYPE.DELETED){
cleanUpDiagnostics(fileEvent.getUri());
}
if (unit.isWorkingCopy()) {
continue;
}
pm.fileChanged(fileEvent.getUri(), toChangeType(fileEvent.getType()));
pm.fileChanged(fileEvent.getUri(), toChangeType(eventType));
}
return null;
}

private void cleanUpDiagnostics(String uri){
NotificationMessage<PublishDiagnosticsParams> message = new NotificationMessage<PublishDiagnosticsParams>();
message.setMethod(LSPMethods.DOCUMENT_DIAGNOSTICS.getMethod());
message.setParams(new PublishDiagnosticsParams().withUri(uri)
.withDiagnostics(Collections.emptyList()));
this.connection.send(message);
}

}

0 comments on commit 23e4573

Please sign in to comment.