Skip to content

Commit

Permalink
refactor: Storing VirtualFile instead of Document for opened document
Browse files Browse the repository at this point in the history
Fixes #1082

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Sep 30, 2023
1 parent 28559b5 commit d178a2b
Show file tree
Hide file tree
Showing 19 changed files with 335 additions and 489 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,29 @@
******************************************************************************/
package com.redhat.devtools.intellij.lsp4ij;

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.ProjectManager;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.intellij.lsp4ij.lifecycle.LanguageServerLifecycleManager;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

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

@Override
public void projectOpened(@NotNull Project project) {
private final Project project;

ConnectDocumentToLanguageServerSetupParticipant(Project project) {
this.project = project;
project.getMessageBus().connect().subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, this);
project.getMessageBus().connect().subscribe(ProjectManager.TOPIC, this);
}

@Override
Expand All @@ -40,27 +43,28 @@ public void projectClosing(@NotNull Project project) {

@Override
public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
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, document);
});
} else {
// Force the start of all languages servers mapped with the given file immediately
startLanguageServer(source, document);
}
if (!Objects.equals(source.getProject(), project)) {
// The file has been opened from another project,don't send textDocument/didOpen
return;
}
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);
});
} else {
// Force the start of all languages servers mapped with the given file immediately
startLanguageServer(source, file);
}
}

private static void startLanguageServer(@NotNull FileEditorManager source, Document document) {
private static void startLanguageServer(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
// 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())
.getLanguageServers(document, null);
.getLanguageServers(file, null);
}

}
20 changes: 15 additions & 5 deletions src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.commons.lang.StringUtils;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -140,12 +141,20 @@ public static URI toUri(Document document) {
return file != null ? toUri(file) : null;
}

public static VirtualFile getFile(Document document) {
public static @Nullable VirtualFile getFile(Document document) {
return FileDocumentManager.getInstance().getFile(document);
}

public static @Nullable VirtualFile getFile(@NotNull PsiElement element) {
PsiFile psFile = element.getContainingFile();
return psFile != null ? psFile.getVirtualFile() : null;
}

public static Document getDocument(VirtualFile docFile) {
return FileDocumentManager.getInstance().getDocument(docFile);
if (ApplicationManager.getApplication().isReadAccessAllowed()) {
return FileDocumentManager.getInstance().getDocument(docFile);
}
return ReadAction.compute(() -> FileDocumentManager.getInstance().getDocument(docFile));
}

/**
Expand Down Expand Up @@ -405,11 +414,11 @@ public static Language getDocumentLanguage(Document document, Project project) {
return getFileLanguage(file, project);
}

public static VirtualFile findResourceFor(URI uri) {
public static @Nullable VirtualFile findResourceFor(URI uri) {
return LocalFileSystem.getInstance().findFileByIoFile(Paths.get(uri).toFile());
}

public static VirtualFile findResourceFor(String uri) {
public static @Nullable VirtualFile findResourceFor(String uri) {
if (uri.startsWith(JAR_SCHEME) || uri.startsWith(JRT_SCHEME)) {
// ex : jar:file:///C:/Users/azerr/.m2/repository/io/quarkus/quarkus-core/3.0.1.Final/quarkus-core-3.0.1.Final.jar!/io/quarkus/runtime/ApplicationConfig.class
try {
Expand All @@ -423,7 +432,7 @@ public static VirtualFile findResourceFor(String uri) {

public static Editor[] editorsForFile(VirtualFile file) {
Editor[] editors = new Editor[0];
Document document = FileDocumentManager.getInstance().getDocument(file);
Document document = getDocument(file);
if (document != null) {
editors = editorsForFile(file, document);
}
Expand Down Expand Up @@ -518,4 +527,5 @@ public static String getProjectUri(Project project) {
}
return project.getName();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.intellij.lsp4ij.operations.diagnostics.LSPDiagnosticsForServer;
import com.redhat.devtools.intellij.lsp4ij.operations.documentLink.LSPDocumentLinkForServer;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DocumentLink;

import java.util.List;

/**
* LSP data stored in {@link VirtualFile} which are used by some LSP operations.
Expand All @@ -28,16 +32,31 @@ public class LSPVirtualFileData {

private final LSPDocumentLinkForServer documentLinkForServer;

public LSPVirtualFileData(LanguageServerWrapper languageServerWrapper, VirtualFile file) {
private final DocumentContentSynchronizer synchronizer;

public LSPVirtualFileData(LanguageServerWrapper languageServerWrapper, VirtualFile file, DocumentContentSynchronizer synchronizer) {
this.synchronizer = synchronizer;
this.diagnosticsForServer = new LSPDiagnosticsForServer(languageServerWrapper,file);
this.documentLinkForServer = new LSPDocumentLinkForServer(languageServerWrapper, file);
}

public LSPDiagnosticsForServer getLSPDiagnosticsForServer() {
public DocumentContentSynchronizer getSynchronizer() {
return synchronizer;
}

public LSPDiagnosticsForServer getDiagnosticsForServer() {
return diagnosticsForServer;
}

public LSPDocumentLinkForServer getDocumentLinkForServer() {
return documentLinkForServer;
}

public void updateDiagnostics(List<Diagnostic> diagnostics) {
diagnosticsForServer.update(diagnostics);
}

public void updateDocumentLink(List<DocumentLink> documentLinks) {
documentLinkForServer.update(documentLinks);
}
}

This file was deleted.

Loading

0 comments on commit d178a2b

Please sign in to comment.