Skip to content

Commit

Permalink
Merge pull request apache#7729 from sdedic/lsp/refresh-after-save
Browse files Browse the repository at this point in the history
Forces refresh of a FileObject after the LSP client reports the file has been saved.
  • Loading branch information
sdedic authored Sep 13, 2024
2 parents 06a2d99 + 5a4d719 commit 51dea0c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.RenameOptions;
import org.eclipse.lsp4j.SaveOptions;
import org.eclipse.lsp4j.SemanticTokensCapabilities;
import org.eclipse.lsp4j.SemanticTokensParams;
import org.eclipse.lsp4j.ServerCapabilities;
Expand Down Expand Up @@ -866,6 +867,9 @@ private InitializeResult constructInitResponse(InitializeParams init, JavaSource
textDocumentSyncOptions.setChange(TextDocumentSyncKind.Incremental);
textDocumentSyncOptions.setOpenClose(true);
textDocumentSyncOptions.setWillSaveWaitUntil(true);
// TODO: we now do not request to send saved contents, but in case of client side applied text edits, it could be cool to
// receive the current document contents at a savepoint.
textDocumentSyncOptions.setSave(new SaveOptions(false));
capabilities.setTextDocumentSync(textDocumentSyncOptions);
CompletionOptions completionOptions = new CompletionOptions();
completionOptions.setResolveProvider(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.nio.file.Path;
Expand Down Expand Up @@ -77,6 +78,7 @@
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.swing.JEditorPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
Expand Down Expand Up @@ -247,6 +249,7 @@
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.URLMapper;
import org.openide.loaders.DataObject;
import org.openide.text.CloneableEditorSupport;
import org.openide.text.NbDocument;
import org.openide.text.PositionBounds;
import org.openide.util.BaseUtilities;
Expand Down Expand Up @@ -1759,9 +1762,34 @@ public CompletableFuture<List<TextEdit>> willSaveWaitUntil(WillSaveTextDocumentP
}

@Override
public void didSave(DidSaveTextDocumentParams arg0) {
//TODO: nothing for now?
LOG.log(Level.FINER, "didSave: {0}", arg0);
public void didSave(DidSaveTextDocumentParams savedParams) {
LOG.log(Level.FINER, "didSave: {0}", savedParams);
FileObject file = fromURI(savedParams.getTextDocument().getUri());
if (file == null) {
return;
}
// refresh the file systems, potentially fire events
file.refresh();
EditorCookie cake = file.getLookup().lookup(EditorCookie.class);
if (cake == null) {
return;
}
StyledDocument alreadyLoaded = cake.getDocument();
if (alreadyLoaded == null) {
return;
}
try {
// if the FileObject.refresh() only now discovered a change, it have fired an event and initiated a reload, which might
// be still pending. Grab the reload task and wait for it:
Method reload = CloneableEditorSupport.class.getDeclaredMethod("reloadDocument");
reload.setAccessible(true);
org.openide.util.Task t = (org.openide.util.Task)reload.invoke(cake);
// wait for a limited time, this could be enough for the reload to complete, blocking LSP queue. We do not want to block LSP queue indefinitely:
// in case of an error, the server could become unresponsive.
t.waitFinished(300);
} catch (ReflectiveOperationException | InterruptedException | SecurityException ex) {
// nop
}
}

CompletableFuture<List<? extends Location>> superImplementations(String uri, Position position) {
Expand Down

0 comments on commit 51dea0c

Please sign in to comment.