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 Oct 1, 2023
1 parent 28559b5 commit 43616fb
Show file tree
Hide file tree
Showing 20 changed files with 382 additions and 528 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile f
}
}

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import org.eclipse.lsp4j.TextDocumentSyncOptions;
import org.eclipse.lsp4j.VersionedTextDocumentIdentifier;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -33,29 +33,30 @@
public class DocumentContentSynchronizer implements DocumentListener {
private final static Logger LOGGER = LoggerFactory.getLogger(DocumentContentSynchronizer.class);

private final @Nonnull LanguageServerWrapper languageServerWrapper;
private final @Nonnull Document document;
private final @Nonnull URI fileUri;
private final @NotNull LanguageServerWrapper languageServerWrapper;
private final @NotNull Document document;
private final @NotNull String fileUri;
private final TextDocumentSyncKind syncKind;

private int version = 0;
private final List<TextDocumentContentChangeEvent> changeEvents;
private long modificationStamp;
final @Nonnull
final @NotNull
CompletableFuture<Void> didOpenFuture;

public DocumentContentSynchronizer(@Nonnull LanguageServerWrapper languageServerWrapper,
@Nonnull Document document,
public DocumentContentSynchronizer(@NotNull LanguageServerWrapper languageServerWrapper,
@NotNull URI fileUri,
@NotNull Document document,
TextDocumentSyncKind syncKind) {
this.languageServerWrapper = languageServerWrapper;
this.fileUri = LSPIJUtils.toUri(document);
this.fileUri = fileUri.toASCIIString();
this.modificationStamp = -1;
this.syncKind = syncKind != null ? syncKind : TextDocumentSyncKind.Full;

this.document = document;
// add a document buffer
TextDocumentItem textDocument = new TextDocumentItem();
textDocument.setUri(fileUri.toString());
textDocument.setUri(this.fileUri);
textDocument.setText(document.getText());

Language contentTypes = LSPIJUtils.getDocumentLanguage(document, languageServerWrapper.getProject());
Expand Down
22 changes: 16 additions & 6 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 Document getDocument(VirtualFile docFile) {
return FileDocumentManager.getInstance().getDocument(docFile);
public static @Nullable VirtualFile getFile(@NotNull PsiElement element) {
PsiFile psFile = element.getContainingFile();
return psFile != null ? psFile.getVirtualFile() : null;
}

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

/**
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 43616fb

Please sign in to comment.