From bdcb89d123461c28793bebcb141cf171396068c6 Mon Sep 17 00:00:00 2001 From: azerr Date: Tue, 17 Oct 2023 15:33:00 +0200 Subject: [PATCH] fix: Remove LSPIJUtils.getProject(VirtualFile file) Signed-off-by: azerr --- .../devtools/intellij/lsp4ij/LSPIJUtils.java | 49 ++++++------------- .../lsp4ij/LanguageServerWrapper.java | 3 +- .../lsp4ij/LanguageServiceAccessor.java | 10 ++-- .../client/IndexAwareLanguageClient.java | 2 +- .../diagnostics/LSPDiagnosticHandler.java | 15 +++--- ...LSPDocumentLinkGotoDeclarationHandler.java | 2 +- .../ClasspathResourceChangedListener.java | 2 +- .../psi/internal/core/ls/PsiUtilsLSImpl.java | 2 +- .../lsp/AbstractQuarkusDocumentMatcher.java | 2 +- .../format/QuteFileIndentOptionsProvider.java | 40 +++++++-------- .../qute/lsp/AbstractQuteDocumentMatcher.java | 2 +- .../QuteDocumentMatcherForTemplateFile.java | 2 +- .../QuteUpdateConfigurationAction.java | 2 +- .../qute/psi/utils/PsiQuteProjectUtils.java | 6 ++- 14 files changed, 59 insertions(+), 80 deletions(-) diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java index 08fb20996..4ef821d58 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LSPIJUtils.java @@ -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 { @@ -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) { diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java index a336ca1bb..535f94fff 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServerWrapper.java @@ -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; @@ -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; diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServiceAccessor.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServiceAccessor.java index 0af6cd2ab..0af143e11 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServiceAccessor.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/LanguageServiceAccessor.java @@ -160,11 +160,7 @@ private static boolean capabilitiesComply(LanguageServerWrapper wrapper, @NotNull private CompletableFuture> 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()); @@ -174,14 +170,14 @@ private CompletableFuture> getMatchedLanguageS // Collect sync server definitions var serverDefinitions = mappings.getMatched(); - collectLanguageServersFromDefinition(file, fileProject, serverDefinitions, matchedServers); + collectLanguageServersFromDefinition(file, project, serverDefinitions, matchedServers); CompletableFuture> 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; }); } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/client/IndexAwareLanguageClient.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/client/IndexAwareLanguageClient.java index cdbec4078..2edfab37c 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/client/IndexAwareLanguageClient.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/client/IndexAwareLanguageClient.java @@ -63,7 +63,7 @@ protected CompletableFuture 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(); diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/diagnostics/LSPDiagnosticHandler.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/diagnostics/LSPDiagnosticHandler.java index 85d39698b..012ef8130 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/diagnostics/LSPDiagnosticHandler.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/diagnostics/LSPDiagnosticHandler.java @@ -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; @@ -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) { @@ -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); @@ -93,6 +93,5 @@ public void updateDiagnostics(PublishDiagnosticsParams params) { //Applic // which translates LSP Diagnostics into Intellij Annotation DaemonCodeAnalyzer.getInstance(project).restart(psiFile); } - } } \ No newline at end of file diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/documentLink/LSPDocumentLinkGotoDeclarationHandler.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/documentLink/LSPDocumentLinkGotoDeclarationHandler.java index 7b540dd9b..6960acfa4 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/documentLink/LSPDocumentLinkGotoDeclarationHandler.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/operations/documentLink/LSPDocumentLinkGotoDeclarationHandler.java @@ -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; diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/classpath/ClasspathResourceChangedListener.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/classpath/ClasspathResourceChangedListener.java index 9208050ca..8e6e70195 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/classpath/ClasspathResourceChangedListener.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/classpath/ClasspathResourceChangedListener.java @@ -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; } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/core/ls/PsiUtilsLSImpl.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/core/ls/PsiUtilsLSImpl.java index 801f7638d..9ec2105ae 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/core/ls/PsiUtilsLSImpl.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/core/ls/PsiUtilsLSImpl.java @@ -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 diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/lsp/AbstractQuarkusDocumentMatcher.java b/src/main/java/com/redhat/devtools/intellij/quarkus/lsp/AbstractQuarkusDocumentMatcher.java index d5488c11c..409b349c2 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/lsp/AbstractQuarkusDocumentMatcher.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/lsp/AbstractQuarkusDocumentMatcher.java @@ -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); } } \ No newline at end of file diff --git a/src/main/java/com/redhat/devtools/intellij/qute/lang/format/QuteFileIndentOptionsProvider.java b/src/main/java/com/redhat/devtools/intellij/qute/lang/format/QuteFileIndentOptionsProvider.java index 051363d6f..968fde66b 100644 --- a/src/main/java/com/redhat/devtools/intellij/qute/lang/format/QuteFileIndentOptionsProvider.java +++ b/src/main/java/com/redhat/devtools/intellij/qute/lang/format/QuteFileIndentOptionsProvider.java @@ -1,12 +1,12 @@ /** * Copyright 2012 Daniel Marcotte - * + *

* 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 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* 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. @@ -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; - } } \ No newline at end of file diff --git a/src/main/java/com/redhat/devtools/intellij/qute/lsp/AbstractQuteDocumentMatcher.java b/src/main/java/com/redhat/devtools/intellij/qute/lsp/AbstractQuteDocumentMatcher.java index f3f81c519..d02a19e16 100644 --- a/src/main/java/com/redhat/devtools/intellij/qute/lsp/AbstractQuteDocumentMatcher.java +++ b/src/main/java/com/redhat/devtools/intellij/qute/lsp/AbstractQuteDocumentMatcher.java @@ -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); } } \ No newline at end of file diff --git a/src/main/java/com/redhat/devtools/intellij/qute/lsp/QuteDocumentMatcherForTemplateFile.java b/src/main/java/com/redhat/devtools/intellij/qute/lsp/QuteDocumentMatcherForTemplateFile.java index 84c77f0ac..16d03e032 100644 --- a/src/main/java/com/redhat/devtools/intellij/qute/lsp/QuteDocumentMatcherForTemplateFile.java +++ b/src/main/java/com/redhat/devtools/intellij/qute/lsp/QuteDocumentMatcherForTemplateFile.java @@ -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)); } } \ No newline at end of file diff --git a/src/main/java/com/redhat/devtools/intellij/qute/psi/core/command/QuteUpdateConfigurationAction.java b/src/main/java/com/redhat/devtools/intellij/qute/psi/core/command/QuteUpdateConfigurationAction.java index 7f87e6c9f..e05e2e1e2 100644 --- a/src/main/java/com/redhat/devtools/intellij/qute/psi/core/command/QuteUpdateConfigurationAction.java +++ b/src/main/java/com/redhat/devtools/intellij/qute/psi/core/command/QuteUpdateConfigurationAction.java @@ -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); diff --git a/src/main/java/com/redhat/devtools/intellij/qute/psi/utils/PsiQuteProjectUtils.java b/src/main/java/com/redhat/devtools/intellij/qute/psi/utils/PsiQuteProjectUtils.java index 27bbcbfbf..abca3ba00 100644 --- a/src/main/java/com/redhat/devtools/intellij/qute/psi/utils/PsiQuteProjectUtils.java +++ b/src/main/java/com/redhat/devtools/intellij/qute/psi/utils/PsiQuteProjectUtils.java @@ -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 projectDependencies = new HashSet<>(); ModuleUtilCore.getDependencies(javaProject, projectDependencies); @@ -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. *