Skip to content

Commit

Permalink
chore: use CompletableFuture to wait after sending didOpen notification.
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Sep 26, 2024
1 parent c7a03a8 commit ae5a957
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,24 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
* Synchronize IntelliJ document (open, content changed, close, save)
* with LSP notifications (didOpen, didChanged, didClose, didSave).
*/
public class DocumentContentSynchronizer implements DocumentListener {

private static final long WAIT_AFTER_SENDING_DID_OPEN = 500L;

private final @NotNull LanguageServerWrapper languageServerWrapper;
private final @NotNull Document document;
private final @NotNull String fileUri;
private final TextDocumentSyncKind syncKind;

private int version = 0;
private final List<TextDocumentContentChangeEvent> changeEvents;
@NotNull
final CompletableFuture<Void> didOpenFuture;
private final Object lock = new Object();
@NotNull final CompletableFuture<Void> didOpenFuture;

public DocumentContentSynchronizer(@NotNull LanguageServerWrapper languageServerWrapper,
@NotNull URI fileUri,
Expand All @@ -68,21 +69,21 @@ public DocumentContentSynchronizer(@NotNull LanguageServerWrapper languageServer
.getInitializedServer()
.thenAcceptAsync(ls -> ls.getTextDocumentService()
.didOpen(new DidOpenTextDocumentParams(textDocument)))
.thenRun(() -> {
if (ApplicationManager.getApplication().isUnitTestMode()) {
return;
}
// Wait for 500 ms after to send the didOpen notification
// to be sure that the notification has been sent before
// consuming other LSP request like 'textDocument/codeLens'.
synchronized (lock) {
try {
lock.wait(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
.thenCompose(result ->
CompletableFuture.runAsync(() -> {
if (ApplicationManager.getApplication().isUnitTestMode()) {
return;
}
try {
// Wait 500ms after sending didOpen notification
// to be sure that the notification has been sent before
// consuming other LSP request like 'textDocument/codeLens'.
TimeUnit.MILLISECONDS.sleep(WAIT_AFTER_SENDING_DID_OPEN);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
})
);

// Initialize LSP change events
changeEvents = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public LSPFileListener(LanguageServerWrapper languageServerWrapper) {

@Override
public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
if (languageServerWrapper.initialProject != null && !Objects.equals(source.getProject(), languageServerWrapper.initialProject)) {
if (!Objects.equals(source.getProject(), languageServerWrapper.getProject())) {
// The file has been closed from another project,don't send textDocument/didClose
return;
}
Expand Down

0 comments on commit ae5a957

Please sign in to comment.