Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use PsiFile instead of using VirtualFile #913

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile f
}

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(project)
.getLanguageServers(file, null, null);
PsiFile psiFile = LSPIJUtils.getPsiFile(file, project);
if (psiFile != null) {
// 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(project)
.getLanguageServers(psiFile, null, null);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ private ClientFeaturesBasedCommenter(@NotNull PsiFile file) {
@Nullable
private static LSPClientFeatures getClientFeatures(@NotNull PsiFile file) {
CompletableFuture<List<LanguageServerItem>> languageServersFuture = LanguageServiceAccessor.getInstance(file.getProject()).getLanguageServers(
file.getVirtualFile(),
file,
clientFeatures -> StringUtil.isNotEmpty(clientFeatures.getLineCommentPrefix(file)) ||
StringUtil.isNotEmpty(clientFeatures.getBlockCommentPrefix(file)) ||
StringUtil.isNotEmpty(clientFeatures.getBlockCommentSuffix(file)),
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/redhat/devtools/lsp4ij/LSPIJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,16 @@ private static Language doGetFileLanguage(@NotNull VirtualFile file, @NotNull Pr
}


/**
* Returns the virtual file corresponding to the PSI file.
*
* @param psiFile the PSI file
* @return the virtual file, or {@code null} if the file exists only in memory.
*/
public static @Nullable VirtualFile getFile(@NotNull PsiFile psiFile) {
return psiFile.getVirtualFile();
}

/**
* Returns the virtual file corresponding to the PSI element.
*
Expand All @@ -573,7 +583,7 @@ private static Language doGetFileLanguage(@NotNull VirtualFile file, @NotNull Pr
*/
public static @Nullable VirtualFile getFile(@NotNull PsiElement element) {
PsiFile psFile = element.getContainingFile();
return psFile != null ? psFile.getVirtualFile() : null;
return psFile != null ? getFile(psFile) : null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,8 @@
import com.redhat.devtools.lsp4ij.internal.StringUtils;
import com.redhat.devtools.lsp4ij.launching.ServerMappingSettings;
import com.redhat.devtools.lsp4ij.launching.UserDefinedLanguageServerSettings;
import com.redhat.devtools.lsp4ij.server.definition.LanguageServerDefinition;
import com.redhat.devtools.lsp4ij.server.definition.LanguageServerDefinitionListener;
import com.redhat.devtools.lsp4ij.server.definition.LanguageServerFileAssociation;
import com.redhat.devtools.lsp4ij.server.definition.ServerFileNamePatternMapping;
import com.redhat.devtools.lsp4ij.server.definition.ServerFileTypeMapping;
import com.redhat.devtools.lsp4ij.server.definition.ServerLanguageMapping;
import com.redhat.devtools.lsp4ij.server.definition.ServerMapping;
import com.redhat.devtools.lsp4ij.server.definition.extension.ExtensionLanguageServerDefinition;
import com.redhat.devtools.lsp4ij.server.definition.extension.FileNamePatternMappingExtensionPointBean;
import com.redhat.devtools.lsp4ij.server.definition.extension.FileTypeMappingExtensionPointBean;
import com.redhat.devtools.lsp4ij.server.definition.extension.LanguageMappingExtensionPointBean;
import com.redhat.devtools.lsp4ij.server.definition.extension.SemanticTokensColorsProviderExtensionPointBean;
import com.redhat.devtools.lsp4ij.server.definition.extension.ServerExtensionPointBean;
import com.redhat.devtools.lsp4ij.server.definition.*;
import com.redhat.devtools.lsp4ij.server.definition.extension.*;
import com.redhat.devtools.lsp4ij.server.definition.launching.UserDefinedLanguageServerDefinition;
import com.redhat.devtools.lsp4ij.usages.LSPFindUsagesProvider;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -286,13 +275,13 @@ private static String getServerNotAvailableMessage(ServerMapping mapping) {
/**
* @param language the language
* @param fileType the file type.
* @param file
* @param fileName the file name
* @return the {@link LanguageServerDefinition}s <strong>directly</strong> associated to the given content-type.
* This does <strong>not</strong> include the one that match transitively as per content-type hierarchy
*/
List<LanguageServerFileAssociation> findLanguageServerDefinitionFor(final @Nullable Language language, @Nullable FileType fileType, @NotNull VirtualFile file) {
List<LanguageServerFileAssociation> findLanguageServerDefinitionFor(final @Nullable Language language, @Nullable FileType fileType, @NotNull String fileName) {
return fileAssociations.stream()
.filter(mapping -> mapping.match(language, fileType, file.getName()))
.filter(mapping -> mapping.match(language, fileType, fileName))
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -475,7 +464,7 @@ private void removeAssociationsFor(LanguageServerDefinition definition) {
settings.setMappings(request.mappings());

if (nameChanged || commandChanged || userEnvironmentVariablesChanged || includeSystemEnvironmentVariablesChanged ||
mappingsChanged || configurationContentChanged || initializationOptionsContentChanged) {
mappingsChanged || configurationContentChanged || initializationOptionsContentChanged) {
// Notifications
LanguageServerDefinitionListener.LanguageServerChangedEvent event = new LanguageServerDefinitionListener.LanguageServerChangedEvent(
request.project(),
Expand Down Expand Up @@ -523,7 +512,9 @@ public boolean isFileSupported(@Nullable PsiFile file) {
if (file == null) {
return false;
}
return isFileSupported(file.getVirtualFile(), file.getProject());
Language language = file.getLanguage();
FileType fileType = file.getFileType();
return isFileSupported(language, fileType, file.getName(), null, file, file.getProject());
}

/**
Expand All @@ -539,15 +530,25 @@ public boolean isFileSupported(@Nullable VirtualFile file, @NotNull Project proj
}
Language language = LSPIJUtils.getFileLanguage(file, project);
FileType fileType = file.getFileType();
return isFileSupported(language, fileType, file.getName(), file, null, project);
}

private boolean isFileSupported(@Nullable Language language,
@Nullable FileType fileType,
@NotNull String filename,
@Nullable VirtualFile file,
@Nullable PsiFile psiFile,
@NotNull Project project) {
if (fileAssociations
.stream()
.anyMatch(mapping -> mapping.match(language, fileType, file.getName()))) {
if (!file.isInLocalFileSystem()) {
if (file instanceof LightVirtualFile) {
.anyMatch(mapping -> mapping.match(language, fileType, filename))) {
VirtualFile f = file != null ? file : psiFile.getVirtualFile();
if (!f.isInLocalFileSystem()) {
if (f instanceof LightVirtualFile) {
return false;
}
PsiFile psiFile = LSPIJUtils.getPsiFile(file, project);
if (psiFile != null && !psiFile.isPhysical()) {
PsiFile pf = psiFile != null ? psiFile : LSPIJUtils.getPsiFile(file, project);
if (pf != null && !pf.isPhysical()) {
return false;
}
}
Expand Down
Loading
Loading