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 32da2e5
Show file tree
Hide file tree
Showing 18 changed files with 318 additions and 481 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
******************************************************************************/
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.ProjectManagerListener;
Expand All @@ -40,27 +37,24 @@ 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);
}
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);
}

}
13 changes: 10 additions & 3 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,10 +141,15 @@ 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);
}
Expand Down Expand Up @@ -405,11 +411,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 Down Expand Up @@ -518,4 +524,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 32da2e5

Please sign in to comment.