Skip to content

Commit

Permalink
perf: Avoid using ModuleUtilCore#findModuleForFile which can block IJ
Browse files Browse the repository at this point in the history
since 2023.3 Beta2

Fixes #1272

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Nov 24, 2023
1 parent 4282405 commit 5a13949
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,11 @@ public void projectClosing(@NotNull Project project) {
@Override
public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
Project project = source.getProject();
boolean readAccessAllowed = ApplicationManager.getApplication().isReadAccessAllowed();
boolean dumb = DumbService.isDumb(project);
// As document matcher requires read action, we try to open the file in read action and when indexing is finishsed.
if (readAccessAllowed && !dumb) {
// No indexing and read action enabled
// --> force the start of all languages servers mapped with the given file immediately
connectToLanguageServer(file, project);
} else {
// Wait for indexing is finished and read action is enabled
// --> force the start of all languages servers mapped with the given file when indexing is finished and read action is allowed
new ConnectToLanguageServerCompletableFuture(file, project);
}
// As document matcher requires read action, and language server starts can take some times, we connect the file in a future
// to avoid starting language server in the EDT Thread which could freeze IJ.
// Wait for indexing is finished and read action is enabled
// --> force the start of all languages servers mapped with the given file when indexing is finished and read action is allowed
new ConnectToLanguageServerCompletableFuture(file, project);
}

private static void connectToLanguageServer(@NotNull VirtualFile file, @NotNull Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import com.intellij.facet.FacetManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.LibraryOrderEntry;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.OrderEnumerator;
import com.intellij.openapi.roots.RootPolicy;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.intellij.lsp4ij.LSPIJUtils;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.internal.core.ls.PsiUtilsLSImpl;
import com.redhat.devtools.intellij.quarkus.facet.QuarkusFacet;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -144,7 +144,7 @@ public static boolean isQuarkusYAMLFile(VirtualFile file, Project project) {
}

private static boolean isQuarkusModule(VirtualFile file, Project project) {
Module module = ModuleUtilCore.findModuleForFile(file, project);
Module module = LSPIJUtils.getModule(file, project);
return module != null && (FacetManager.getInstance(module).getFacetByType(QuarkusFacet.FACET_TYPE_ID) != null || QuarkusModuleUtil.isQuarkusModule(module));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
package com.redhat.devtools.intellij.quarkus.json;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.jsonSchema.extension.JsonSchemaFileProvider;
import com.jetbrains.jsonSchema.extension.SchemaType;
import com.redhat.devtools.intellij.lsp4ij.LSPIJUtils;
import com.redhat.devtools.intellij.quarkus.QuarkusModuleUtil;
import com.redhat.devtools.intellij.quarkus.QuarkusProjectService;
import org.jetbrains.annotations.NotNull;
Expand All @@ -29,7 +29,7 @@ public QuarkusJsonSchemaProvider(Module module) {

@Override
public boolean isAvailable(@NotNull VirtualFile file) {
return isApplicationYAMLFile(file) && module.equals(ModuleUtilCore.findModuleForFile(file, module.getProject()));
return isApplicationYAMLFile(file) && module.equals(LSPIJUtils.getModule(file, module.getProject()));
}

private boolean isApplicationYAMLFile(VirtualFile file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.intellij.openapi.roots.RootPolicy;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.LanguageSubstitutor;
import com.redhat.devtools.intellij.lsp4ij.LSPIJUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -70,7 +71,7 @@ private Module findModule(VirtualFile file) {

@Override
public @Nullable Language getLanguage(@NotNull VirtualFile file, @NotNull Project project) {
Module module = ModuleUtilCore.findModuleForFile(file, project);
Module module = LSPIJUtils.getModule(file, project);
if (module != null) {
if (isTemplate(file, module) && isQuteModule(module)) {
return QuteLanguage.INSTANCE;
Expand Down

0 comments on commit 5a13949

Please sign in to comment.