Skip to content

Commit

Permalink
fix: Closing one module window doesn't shutdown the running LS instances
Browse files Browse the repository at this point in the history
Fixes #1069

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Aug 2, 2023
1 parent 03ed42f commit 2536c02
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void projectOpened(@NotNull Project project) {
@Override
public void projectClosing(@NotNull Project project) {
LanguageServerLifecycleManager.getInstance(project).dispose();
LanguageServiceAccessor.getInstance(project).shutdownAllDispatchers();
LanguageServiceAccessor.getInstance(project).projectClosing(project);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*******************************************************************************/
package com.redhat.devtools.intellij.lsp4ij;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.intellij.lsp4ij.operations.diagnostics.LSPDiagnosticsForServer;
Expand All @@ -35,7 +38,7 @@
*
* @author Angelo ZERR
*/
public class LSPVirtualFileWrapper {
public class LSPVirtualFileWrapper implements Disposable {

private static final Key<LSPVirtualFileWrapper> KEY = new Key<>(LSPVirtualFileWrapper.class.getName());

Expand All @@ -46,6 +49,10 @@ public class LSPVirtualFileWrapper {
LSPVirtualFileWrapper(VirtualFile file) {
this.file = file;
this.dataPerServer = new HashMap<>();
Module project = LSPIJUtils.getProject(file);
if (project != null) {
Disposer.register(project, this);
}
}

public VirtualFile getFile() {
Expand Down Expand Up @@ -129,7 +136,9 @@ private <T> Collection<T> getData(Function<LSPVirtualFileData, ? extends T> mapp

// ------------------------ Static accessor

@Override
public void dispose() {
file.putUserData(KEY, null);
this.dataPerServer.clear();
}

Expand Down Expand Up @@ -165,7 +174,6 @@ public static void dispose(VirtualFile file) {
LSPVirtualFileWrapper wrapper = file.getUserData(KEY);
if (wrapper != null) {
wrapper.dispose();
file.putUserData(KEY, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.intellij.AppTopics;
import com.intellij.ProjectTopics;
import com.intellij.lang.Language;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
Expand All @@ -29,6 +30,7 @@
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.ModuleListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.messages.MessageBusConnection;
import com.redhat.devtools.intellij.lsp4ij.client.LanguageClientImpl;
Expand Down Expand Up @@ -60,12 +62,16 @@
/**
* Language server wrapper.
*/
public class LanguageServerWrapper {
public class LanguageServerWrapper implements Disposable {

private static final Logger LOGGER = LoggerFactory.getLogger(LanguageServerWrapper.class);//$NON-NLS-1$
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 {

private boolean projectClosing;

@Override
public void documentChanged(@NotNull DocumentEvent event) {
URI uri = LSPIJUtils.toUri(event.getDocument());
Expand All @@ -87,6 +93,11 @@ public void beforeDocumentSaving(@NotNull Document document) {
}



public void projectClosing() {
this.projectClosing = true;
}

@Override
public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
URI uri = LSPIJUtils.toUri(file);
Expand All @@ -95,7 +106,7 @@ public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile f
// Remove the cached file wrapper if needed
LSPVirtualFileWrapper.dispose(file);
// Disconnect the given file from all language servers
disconnect(uri);
disconnect(uri, projectClosing);
} catch (Exception e) {
LOGGER.warn("Error while disconnecting the file '" + uri + "' from all language servers", e);
}
Expand Down Expand Up @@ -179,6 +190,9 @@ private LanguageServerWrapper(@Nullable Module project, @Nonnull LanguageServers
this.listener = Executors
.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat(listenerThreadNameFormat).build());
udateStatus(ServerStatus.none);
if (project != null) {
Disposer.register(project, this);
}
}

@Nonnull
Expand Down Expand Up @@ -469,6 +483,16 @@ public boolean isActive() {
return this.launcherFuture != null && !this.launcherFuture.isDone() && !this.launcherFuture.isCancelled();
}

public void projectClosing() {
this.fileBufferListener.projectClosing();
}

@Override
public void dispose() {
stop();
stopDispatcher();
}

/**
* Returns true if the language server is stopping and false otherwise.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ void shutdownAllDispatchers() {
startedServers.forEach(LanguageServerWrapper::stopDispatcher);
}

public void projectClosing(Project project) {
startedServers.forEach(ls -> {
if (project.equals(ls.getProject())) {
ls.projectClosing();
}
});
}

/**
* A bean storing association of a Document/File with a language server.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
package com.redhat.devtools.intellij.lsp4ij.client;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.progress.*;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.intellij.lsp4ij.LSPIJUtils;
Expand All @@ -29,7 +28,6 @@ public class IndexAwareLanguageClient extends LanguageClientImpl {

public IndexAwareLanguageClient(Project project) {
super(project);
Disposer.register(project, this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.redhat.devtools.intellij.lsp4ij.LSPIJUtils;
import com.redhat.devtools.intellij.lsp4ij.LanguageServerWrapper;
import com.redhat.devtools.intellij.lsp4ij.ServerMessageHandler;
Expand All @@ -30,6 +31,7 @@ public class LanguageClientImpl implements LanguageClient, Disposable {

public LanguageClientImpl(Project project) {
this.project = project;
Disposer.register(project, this);
}

public Project getProject() {
Expand Down

0 comments on commit 2536c02

Please sign in to comment.