Skip to content

Commit

Permalink
fix: java.lang.Throwable: Can't remove document listener
Browse files Browse the repository at this point in the history
(com.redhat.devtools.intellij.lsp4ij.DocumentContentSynchronizer@3659a98f).
Registered listeners:
[com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl

Fixes #1097

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Sep 8, 2023
1 parent 5d3b511 commit 37b4885
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ private TextDocumentContentChangeEvent createChangeEvent(DocumentEvent event) {
return null;
}

public void documentSaved(DocumentEvent event) {
long timestamp = event.getOldTimeStamp();
Document document = event.getDocument();
this.modificationStamp = timestamp;
public void documentSaved() {
ServerCapabilities serverCapabilities = languageServerWrapper.getServerCapabilities();
if (serverCapabilities != null) {
Either<TextDocumentSyncKind, TextDocumentSyncOptions> textDocumentSync = serverCapabilities.getTextDocumentSync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.*;
import com.intellij.openapi.vfs.impl.BulkVirtualFileListenerAdapter;
import com.intellij.util.messages.MessageBusConnection;
import com.redhat.devtools.intellij.lsp4ij.client.LanguageClientImpl;
import com.redhat.devtools.intellij.lsp4ij.internal.SupportedFeatures;
Expand Down Expand Up @@ -65,27 +66,7 @@ public class LanguageServerWrapper implements Disposable {
private static final String CLIENT_NAME = "IntelliJ";
private static final int MAX_NUMBER_OF_RESTART_ATTEMPTS = 20; // TODO move this max value in settings

class Listener implements DocumentListener, FileDocumentManagerListener, FileEditorManagerListener {

@Override
public void documentChanged(@NotNull DocumentEvent event) {
URI uri = LSPIJUtils.toUri(event.getDocument());
if (uri != null) {
DocumentContentSynchronizer documentListener = connectedDocuments.get(uri);
if (documentListener != null && documentListener.getModificationStamp() < event.getOldTimeStamp()) {
documentListener.documentSaved(event);
}
}
}

@Override
public void beforeDocumentSaving(@NotNull Document document) {
/*VirtualFile file = LSPIJUtils.getFile(document);
URI uri = LSPIJUtils.toUri(file);
if (uri != null) {
disconnect(uri);
}*/
}
class Listener implements FileEditorManagerListener, VirtualFileListener {

@Override
public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
Expand All @@ -101,6 +82,17 @@ public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile f
}
}
}

@Override
public void contentsChanged(@NotNull VirtualFileEvent event) {
URI uri = LSPIJUtils.toUri(event.getFile());
if (uri != null) {
DocumentContentSynchronizer documentListener = connectedDocuments.get(uri);
if (documentListener != null) {
documentListener.documentSaved();
}
}
}
}

private Listener fileBufferListener = new Listener();
Expand Down Expand Up @@ -347,10 +339,11 @@ public synchronized void start() throws LanguageServerException {
}
}
});
EditorFactory.getInstance().getEventMulticaster().addDocumentListener(fileBufferListener);

messageBusConnection = ApplicationManager.getApplication().getMessageBus().connect();
messageBusConnection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, fileBufferListener);
messageBusConnection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, fileBufferListener);
messageBusConnection.subscribe(VirtualFileManager.VFS_CHANGES, new BulkVirtualFileListenerAdapter(fileBufferListener));

udateStatus(ServerStatus.started);
getLanguageServerLifecycleManager().onStatusChanged(this);
}).exceptionally(e -> {
Expand Down Expand Up @@ -552,7 +545,6 @@ public synchronized void stop(boolean alreadyStopping) {
this.languageServer = null;
this.languageClient = null;

EditorFactory.getInstance().getEventMulticaster().removeDocumentListener(fileBufferListener);
if (messageBusConnection != null) {
messageBusConnection.disconnect();
}
Expand Down

0 comments on commit 37b4885

Please sign in to comment.