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 29, 2023
1 parent 28559b5 commit 7e3e127
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 194 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
@@ -0,0 +1,18 @@
package com.redhat.devtools.intellij.lsp4ij;

import com.intellij.openapi.editor.Document;
import com.intellij.openapi.vfs.VirtualFile;
import org.eclipse.lsp4j.TextDocumentSyncKind;

public class LSPVirtualFileData2 {

private final DocumentContentSynchronizer synchronizer;

public LSPVirtualFileData2(VirtualFile file, DocumentContentSynchronizer synchronizer) {
this.synchronizer = synchronizer;
}

public DocumentContentSynchronizer getSynchronizer() {
return synchronizer;
}
}
Loading

0 comments on commit 7e3e127

Please sign in to comment.