Skip to content

Commit

Permalink
perf: set read action allowed when file is opened
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Oct 9, 2023
1 parent 7d107b0 commit ac4a1c5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,39 @@
******************************************************************************/
package com.redhat.devtools.intellij.lsp4ij;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.intellij.lsp4ij.client.CoalesceByKey;
import com.redhat.devtools.intellij.lsp4ij.internal.PromiseToCompletableFuture;
import com.redhat.devtools.intellij.lsp4ij.lifecycle.LanguageServerLifecycleManager;
import org.jetbrains.annotations.NotNull;

import java.text.MessageFormat;

/**
* Track file opened / closed to start language servers / disconnect file from language servers.
*/
public class ConnectDocumentToLanguageServerSetupParticipant implements ProjectManagerListener, FileEditorManagerListener {

private static class ConnectToLanguageServerCompletableFuture extends PromiseToCompletableFuture<Void> {

private static final String MESSAGE_KEY = "Connect ''{0}'' file to language servers.";

public ConnectToLanguageServerCompletableFuture(@NotNull VirtualFile file, @NotNull Project project) {
super(monitor -> {
connectToLanguageServer(file, project);
return null;
}, MessageFormat.format(MESSAGE_KEY, file.getUrl()), project, null, new CoalesceByKey(ConnectDocumentToLanguageServerSetupParticipant.class.getName(), file.getUrl()));
}
}

@Override
public void projectOpened(@NotNull Project project) {
project.getMessageBus().connect().subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, this);
Expand All @@ -43,23 +59,26 @@ public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile f
Document document = FileDocumentManager.getInstance().getDocument(file);
if (document != null) {
Project project = source.getProject();
if (DumbService.isDumb(project)) {
// Force the start of all languages servers mapped with the given file when indexation is finished
DumbService.getInstance(project).runWhenSmart(() -> {
startLanguageServer(source, file);
});
boolean readAccessAllowed = ApplicationManager.getApplication().isReadAccessAllowed();
boolean dumb = DumbService.isDumb(project);
// As document matcher requires read action, we try to open the file in read action and when indexing is finishsed.
if (readAccessAllowed && !dumb) {
// No indexing and read action enabled
// --> force the start of all languages servers mapped with the given file immediately
connectToLanguageServer(file, project);
} else {
// Force the start of all languages servers mapped with the given file immediately
startLanguageServer(source, file);
// Wait for indexing is finished and read action is enabled
// --> force the start of all languages servers mapped with the given file when indexing is finished and read action is allowed
new ConnectToLanguageServerCompletableFuture(file, project);
}
}
}

private static void startLanguageServer(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
private static void connectToLanguageServer(@NotNull VirtualFile file, @NotNull Project project) {
// Force the start of all languages servers mapped with the given file
// Server capabilities filter is set to null to avoid waiting
// for the start of the server when server capabilities are checked
LanguageServiceAccessor.getInstance(source.getProject())
LanguageServiceAccessor.getInstance(project)
.getLanguageServers(file, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ private MatchedLanguageServerDefinitions getMatchedLanguageServerDefinitions(@No
}

private static boolean match(VirtualFile file, Project fileProject, ContentTypeToLanguageServerDefinition mapping) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
if (!ApplicationManager.getApplication().isReadAccessAllowed()) {
return ReadAction.compute(() -> mapping.match(file, fileProject));
}
return mapping.match(file, fileProject);
Expand Down

0 comments on commit ac4a1c5

Please sign in to comment.