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

fix: Remove LSPIJUtils.getProject(VirtualFile file) #1232

Merged
merged 1 commit into from
Oct 18, 2023
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
49 changes: 15 additions & 34 deletions src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,27 +171,15 @@ public static URI toUri(Document document) {
return getDocument(documentFile);
}

public static @Nullable Module getModule(@Nullable VirtualFile file) {
@Nullable
public static Module getModule(@Nullable VirtualFile file, @NotNull Project project) {
if (file == null) {
return null;
}
for (Project project : ProjectManager.getInstance().getOpenProjects()) {
Module module = null;
if (ApplicationManager.getApplication().isReadAccessAllowed()) {
module = ProjectFileIndex.getInstance(project).getModuleForFile(file, false);
} else {
module = ReadAction.compute(() -> ProjectFileIndex.getInstance(project).getModuleForFile(file, false));
}
if (module != null) {
return module;
}
if (ApplicationManager.getApplication().isReadAccessAllowed()) {
return ProjectFileIndex.getInstance(project).getModuleForFile(file, false);
}
return null;
}

public static @Nullable Project getProject(@Nullable VirtualFile file) {
Module module = getModule(file);
return module != null ? module.getProject() : null;
return ReadAction.compute(() -> ProjectFileIndex.getInstance(project).getModuleForFile(file, false));
}

public static int toOffset(Position start, Document document) throws IndexOutOfBoundsException {
Expand Down Expand Up @@ -426,30 +414,23 @@ public static Language getDocumentLanguage(Document document, Project project) {
return VirtualFileManager.getInstance().findFileByUrl(VfsUtilCore.fixURLforIDEA(uri));
}

public static Editor[] editorsForFile(VirtualFile file) {
Editor[] editors = new Editor[0];
Document document = getDocument(file);
if (document != null) {
editors = editorsForFile(file, document);
public static @Nullable Editor editorForElement(@Nullable PsiElement element) {
if (element != null && element.getContainingFile() != null && element.getContainingFile().getVirtualFile() != null) {
return editorForFile(element.getContainingFile().getVirtualFile(), element.getProject());
}
return editors;
}

public static Editor[] editorsForFile(VirtualFile file, Document document) {
Project project = LSPIJUtils.getProject(file);
return project != null ? EditorFactory.getInstance().getEditors(document, project) : new Editor[0];
return null;
}

public static Editor editorForFile(VirtualFile file) {
Editor[] editors = editorsForFile(file);
private static @Nullable Editor editorForFile(@Nullable VirtualFile file, @NotNull Project project) {
Editor[] editors = editorsForDocument(getDocument(file), project);
return editors.length > 0 ? editors[0] : null;
}

public static Editor editorForElement(@Nullable PsiElement element) {
if (element != null && element.getContainingFile() != null && element.getContainingFile().getVirtualFile() != null) {
return editorForFile(element.getContainingFile().getVirtualFile());
private static @NotNull Editor[] editorsForDocument(@Nullable Document document, @Nullable Project project) {
if (document == null) {
return new Editor[0];
}
return null;
return EditorFactory.getInstance().getEditors(document, project);
}

public static CompletionParams toCompletionParams(URI fileUri, int offset, Document document) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
Expand Down Expand Up @@ -1006,7 +1005,7 @@ public boolean canOperate(@NotNull VirtualFile file) {
if (this.initialProject == null && this.connectedDocuments.isEmpty()) {
return true;
}
if (file != null && file.exists() && canOperate(LSPIJUtils.getProject(file))) {
if (file != null && file.exists()) {
return true;
}
return serverDefinition.isSingleton;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,7 @@ private static boolean capabilitiesComply(LanguageServerWrapper wrapper,

@NotNull
private CompletableFuture<Collection<LanguageServerWrapper>> getMatchedLanguageServersWrappers(@NotNull VirtualFile file) {
final Project fileProject = LSPIJUtils.getProject(file);
if (fileProject == null) {
return CompletableFuture.completedFuture(Collections.emptyList());
}
MatchedLanguageServerDefinitions mappings = getMatchedLanguageServerDefinitions(file, fileProject);
MatchedLanguageServerDefinitions mappings = getMatchedLanguageServerDefinitions(file, project);
if (mappings == MatchedLanguageServerDefinitions.NO_MATCH) {
// There are no mapping for the given file
return CompletableFuture.completedFuture(Collections.emptyList());
Expand All @@ -174,14 +170,14 @@ private CompletableFuture<Collection<LanguageServerWrapper>> getMatchedLanguageS

// Collect sync server definitions
var serverDefinitions = mappings.getMatched();
collectLanguageServersFromDefinition(file, fileProject, serverDefinitions, matchedServers);
collectLanguageServersFromDefinition(file, project, serverDefinitions, matchedServers);

CompletableFuture<Set<LanguageServersRegistry.LanguageServerDefinition>> async = mappings.getAsyncMatched();
if (async != null) {
// Collect async server definitions
return async
.thenApply(asyncServerDefinitions -> {
collectLanguageServersFromDefinition(file, fileProject, asyncServerDefinitions, matchedServers);
collectLanguageServersFromDefinition(file, project, asyncServerDefinitions, matchedServers);
return matchedServers;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected <R> CompletableFuture<R> runAsBackground(String progressTitle, Functio
protected String getFilePath(String fileUri) {
VirtualFile file = LSPIJUtils.findResourceFor(fileUri);
if (file != null) {
Module module = LSPIJUtils.getModule(file);
Module module = LSPIJUtils.getModule(file, getProject());
if (module != null) {
ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
VirtualFile[] contentRoots = rootManager.getContentRoots();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.redhat.devtools.intellij.lsp4ij.LanguageServerWrapper;
import com.redhat.devtools.intellij.lsp4ij.client.CoalesceByKey;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.jetbrains.annotations.NotNull;

import java.net.URI;
import java.util.function.Consumer;
Expand All @@ -52,12 +53,12 @@ public void accept(PublishDiagnosticsParams params) {
return;
}
if (ApplicationManager.getApplication().isReadAccessAllowed()) {
updateDiagnostics(params);
updateDiagnostics(params, project);
} else {
// Cancel if needed the previous "textDocument/publishDiagnostics" for a given uri.
var coalesceBy = new CoalesceByKey("textDocument/publishDiagnostics", params.getUri());
var executeInSmartMode = DumbService.getInstance(languageServerWrapper.getProject()).isDumb();
var action = ReadAction.nonBlocking(() -> updateDiagnostics(params))
var action = ReadAction.nonBlocking(() -> updateDiagnostics(params, project))
.expireWith(languageServerWrapper)
.coalesceBy(coalesceBy);
if (executeInSmartMode) {
Expand All @@ -67,13 +68,12 @@ public void accept(PublishDiagnosticsParams params) {
}
}

public void updateDiagnostics(PublishDiagnosticsParams params) { //ApplicationManager.getApplication().runReadAction(() -> {
VirtualFile file = LSPIJUtils.findResourceFor(params.getUri());
if (file == null) {
private void updateDiagnostics(@NotNull PublishDiagnosticsParams params, @NotNull Project project) {
if (project.isDisposed()) {
return;
}
Project project = LSPIJUtils.getProject(file);
if (project == null || project.isDisposed()) {
VirtualFile file = LSPIJUtils.findResourceFor(params.getUri());
if (file == null) {
return;
}
final PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
Expand All @@ -93,6 +93,5 @@ public void updateDiagnostics(PublishDiagnosticsParams params) { //Applic
// which translates LSP Diagnostics into Intellij Annotation
DaemonCodeAnalyzer.getInstance(project).restart(psiFile);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class LSPDocumentLinkGotoDeclarationHandler implements GotoDeclarationHan
public PsiElement @Nullable [] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) {
Document document = editor.getDocument();
VirtualFile file = LSPIJUtils.getFile(document);
Module module = LSPIJUtils.getModule(file);
Module module = LSPIJUtils.getModule(file, sourceElement.getProject());
Project project = module != null ? module.getProject() : null;
if (project == null || project.isDisposed()) {
return PsiElement.EMPTY_ARRAY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void tryToAddSourceFile(VirtualFile file, boolean checkExistingFile) {
return;
}
// The file is a Java file or microprofile-config.properties
Module module = LSPIJUtils.getModule(file);
Module module = LSPIJUtils.getModule(file, project);
if (module == null || module.isDisposed()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public IPsiUtils refine(Module module) {

@Override
public Module getModule(VirtualFile file) {
return LSPIJUtils.getModule(file);
return LSPIJUtils.getModule(file, project);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AbstractQuarkusDocumentMatcher extends AbstractDocumentMatcher {

@Override
public boolean match(VirtualFile file, Project fileProject) {
Module module = LSPIJUtils.getModule(file);
Module module = LSPIJUtils.getModule(file, fileProject);
return module != null && QuarkusModuleUtil.isQuarkusModule(module);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright 2012 Daniel Marcotte
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -36,22 +36,22 @@
*/
public class QuteFileIndentOptionsProvider extends FileIndentOptionsProvider {

@Nullable
@Override
public CommonCodeStyleSettings.@Nullable IndentOptions getIndentOptions(@NotNull CodeStyleSettings settings, @NotNull PsiFile file) {
if (file.getFileType().equals(QuteFileType.QUTE)) {
VirtualFile virtualFile = file.getVirtualFile();
Module module = LSPIJUtils.getModule(virtualFile);
if (module == null) {
@Nullable
@Override
public CommonCodeStyleSettings.@Nullable IndentOptions getIndentOptions(@NotNull CodeStyleSettings settings, @NotNull PsiFile file) {
if (file.getFileType().equals(QuteFileType.QUTE)) {
VirtualFile virtualFile = file.getVirtualFile();
Project project = file.getProject();
Module module = LSPIJUtils.getModule(virtualFile, project);
if (module == null) {
return null;
}
FileViewProvider provider = PsiManagerEx.getInstanceEx(project).findViewProvider(virtualFile);
if (provider instanceof TemplateLanguageFileViewProvider) {
Language language = ((TemplateLanguageFileViewProvider) provider).getTemplateDataLanguage();
return settings.getCommonSettings(language).getIndentOptions();
}
}
return null;
}
Project project = module.getProject();
FileViewProvider provider = PsiManagerEx.getInstanceEx(project).findViewProvider(virtualFile);
if (provider instanceof TemplateLanguageFileViewProvider) {
Language language = ((TemplateLanguageFileViewProvider)provider).getTemplateDataLanguage();
return settings.getCommonSettings(language).getIndentOptions();
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AbstractQuteDocumentMatcher extends AbstractDocumentMatcher {

@Override
public boolean match(VirtualFile file, Project fileProject) {
Module module = LSPIJUtils.getModule(file);
Module module = LSPIJUtils.getModule(file, fileProject);
return module != null && PsiQuteProjectUtils.hasQuteSupport(module);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public boolean match(VirtualFile file, Project fileProject) {
if (!super.match(file, fileProject)) {
return false;
}
return isQuteTemplate(file, LSPIJUtils.getModule(file));
return isQuteTemplate(file, LSPIJUtils.getModule(file, fileProject));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void updateConfiguration(Project project, JsonObject value) {
String scopeUri = value.get("scopeUri").getAsString();
VirtualFile resource = LSPIJUtils.findResourceFor(scopeUri);
if (resource != null) {
@Nullable Module module = LSPIJUtils.getModule(resource);
@Nullable Module module = LSPIJUtils.getModule(resource, project);
if (module != null) {
String modulePath = LSPIJUtils.toUri(module).resolve("**").toString();
addToExclusions(project, modulePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private PsiQuteProjectUtils() {

public static ProjectInfo getProjectInfo(Module javaProject) {
String projectUri = getProjectURI(javaProject);
String templateBaseDir = LSPIJUtils.toUri(javaProject).resolve(TEMPLATES_BASE_DIR).toASCIIString();
String templateBaseDir = getTemplateBaseDir(javaProject);
// Project dependencies
Set<Module> projectDependencies = new HashSet<>();
ModuleUtilCore.getDependencies(javaProject, projectDependencies);
Expand All @@ -59,6 +59,10 @@ public static ProjectInfo getProjectInfo(Module javaProject) {
.collect(Collectors.toList()), templateBaseDir);
}

private static String getTemplateBaseDir(Module javaProject) {
return LSPIJUtils.toUri(javaProject).resolve(TEMPLATES_BASE_DIR).toASCIIString();
}

/**
* Returns the project URI of the given project.
*
Expand Down
Loading