diff --git a/src/main/java/com/redhat/devtools/lsp4ij/DocumentContentSynchronizer.java b/src/main/java/com/redhat/devtools/lsp4ij/DocumentContentSynchronizer.java index 4328f0a2b..97eff7c69 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/DocumentContentSynchronizer.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/DocumentContentSynchronizer.java @@ -73,9 +73,8 @@ public DocumentContentSynchronizer(@NotNull LanguageServerWrapper languageServer textDocument.setUri(this.fileUri); textDocument.setText(document.getText()); - Language contentTypes = LSPIJUtils.getDocumentLanguage(document, languageServerWrapper.getProject()); - - String languageId = languageServerWrapper.getLanguageId(contentTypes); + Language language = LSPIJUtils.getDocumentLanguage(document, languageServerWrapper.getProject()); + String languageId = languageServerWrapper.getLanguageId(language); //TODO: determine languageId more precisely /*IPath fromPortableString = Path.fromPortableString(this.fileUri.getPath()); diff --git a/src/main/java/com/redhat/devtools/lsp4ij/LSPIJUtils.java b/src/main/java/com/redhat/devtools/lsp4ij/LSPIJUtils.java index 1ca713307..066ca09f6 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/LSPIJUtils.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/LSPIJUtils.java @@ -92,8 +92,19 @@ public static void openInEditor(VirtualFile file, Position position, Project pro } } - @NotNull + /** + * Returns the file language of the given file and null otherwise. + * + * @param file the file. + * @param project the project. + * + * @return the file language of the given file and null otherwise. + */ + @Nullable public static Language getFileLanguage(@NotNull VirtualFile file, Project project) { + if (ApplicationManager.getApplication().isReadAccessAllowed()) { + return LanguageUtil.getLanguageForPsi(project, file); + } return ReadAction.compute(() -> LanguageUtil.getLanguageForPsi(project, file)); } @@ -109,10 +120,6 @@ private static T toTextDocumentPositionPa return param; } - public static TextDocumentPositionParams toTextDocumentPosistionParams(int offset, Document document) { - return toTextDocumentPositionParamsCommon(new TextDocumentPositionParams(), offset, document); - } - public static HoverParams toHoverParams(int offset, Document document) { return toTextDocumentPositionParamsCommon(new HoverParams(), offset, document); } @@ -428,7 +435,15 @@ private static void applyWorkspaceEdit(Document document, List edits) } - public static Language getDocumentLanguage(Document document, Project project) { + /** + * Returns the language of the given document and null otherwise. + * + * @param document teh document. + * @param project the project. + * + * @return the language of the given document and null otherwise. + */ + public static @Nullable Language getDocumentLanguage(Document document, Project project) { VirtualFile file = FileDocumentManager.getInstance().getFile(document); return getFileLanguage(file, project); } diff --git a/src/main/java/com/redhat/devtools/lsp4ij/LanguageServerWrapper.java b/src/main/java/com/redhat/devtools/lsp4ij/LanguageServerWrapper.java index fa3e6b7a7..7c0892896 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/LanguageServerWrapper.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/LanguageServerWrapper.java @@ -863,7 +863,7 @@ public ServerCapabilities getServerCapabilities() { * content type mapping for the language server */ @Nullable - public String getLanguageId(Language language) { + public String getLanguageId(@Nullable Language language) { while (language != null) { String languageId = serverDefinition.languageIdMappings.get(language); if (languageId != null) { diff --git a/src/main/java/com/redhat/devtools/lsp4ij/LanguageServersRegistry.java b/src/main/java/com/redhat/devtools/lsp4ij/LanguageServersRegistry.java index b79251868..bd1ad3667 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/LanguageServersRegistry.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/LanguageServersRegistry.java @@ -14,6 +14,8 @@ import com.intellij.ide.lightEdit.LightEdit; import com.intellij.lang.Language; import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiFile; import com.redhat.devtools.lsp4ij.client.LanguageClientImpl; import com.redhat.devtools.lsp4ij.server.StreamConnectionProvider; import org.eclipse.lsp4j.jsonrpc.Launcher; @@ -277,7 +279,6 @@ public DocumentMatcher getDocumentMatcher() { * Returns the language server definition for the given language server id and null otherwise. * * @param languageServerId the language server id. - * * @return the language server definition for the given language server id and null otherwise. */ public @Nullable LanguageServerDefinition getServerDefinition(@NotNull String languageServerId) { @@ -294,13 +295,33 @@ public Collection getServerDefinitions() { } /** - * Returns true if the given language is supported by a language server and false otherwise. + * Returns true if the language of the file is supported by a language server and false otherwise. * - * @param language the IJ language + * @param file the file. + * @return true if the language of the file is supported by a language server and false otherwise. + */ + public boolean isLanguageSupported(@Nullable PsiFile file) { + if (file == null) { + return false; + } + return isLanguageSupported(file.getVirtualFile(), file.getProject()); + } + + /** + * Returns true if the language of the file is supported by a language server and false otherwise. * - * @return true if the given language is supported by a language server and false otherwise. + * @param file the file. + * @param project the project. + * @return true if the language of the file is supported by a language server and false otherwise. */ - public boolean isLanguageSupported(@NotNull Language language) { + public boolean isLanguageSupported(@Nullable VirtualFile file, @NotNull Project project) { + if (file == null) { + return false; + } + Language language = LSPIJUtils.getFileLanguage(file, project); + if (language == null) { + return false; + } return connections .stream() .anyMatch(entry -> language.isKindOf(entry.getKey())); diff --git a/src/main/java/com/redhat/devtools/lsp4ij/LanguageServiceAccessor.java b/src/main/java/com/redhat/devtools/lsp4ij/LanguageServiceAccessor.java index 303da1e4b..ed25ff58f 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/LanguageServiceAccessor.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/LanguageServiceAccessor.java @@ -265,7 +265,10 @@ private MatchedLanguageServerDefinitions getMatchedLanguageServerDefinitions(@No // look for running language servers via content-type Queue contentTypes = new LinkedList<>(); Set processedContentTypes = new HashSet<>(); - contentTypes.add(LSPIJUtils.getFileLanguage(file, project)); + Language language= LSPIJUtils.getFileLanguage(file, project); + if (language != null) { + contentTypes.add(language); + } while (!contentTypes.isEmpty()) { Language contentType = contentTypes.poll(); diff --git a/src/main/java/com/redhat/devtools/lsp4ij/operations/AbstractLSPInlayProvider.java b/src/main/java/com/redhat/devtools/lsp4ij/operations/AbstractLSPInlayProvider.java index 8105f01ee..e61adbf54 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/operations/AbstractLSPInlayProvider.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/operations/AbstractLSPInlayProvider.java @@ -67,7 +67,7 @@ public final InlayHintsCollector getCollectorFor(@NotNull PsiFile psiFile, @NotNull NoSettings o, @NotNull InlayHintsSink inlayHintsSink) { - if (!LanguageServersRegistry.getInstance().isLanguageSupported(psiFile.getLanguage())) { + if (!LanguageServersRegistry.getInstance().isLanguageSupported(psiFile)) { return EMPTY_INLAY_HINTS_COLLECTOR; } diff --git a/src/main/java/com/redhat/devtools/lsp4ij/operations/completion/LSPCompletionContributor.java b/src/main/java/com/redhat/devtools/lsp4ij/operations/completion/LSPCompletionContributor.java index c2dcae629..98bca2a89 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/operations/completion/LSPCompletionContributor.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/operations/completion/LSPCompletionContributor.java @@ -57,7 +57,7 @@ public void fillCompletionVariants(@NotNull CompletionParameters parameters, @No return; } - if (!LanguageServersRegistry.getInstance().isLanguageSupported(psiFile.getLanguage())) { + if (!LanguageServersRegistry.getInstance().isLanguageSupported(psiFile)) { return; } diff --git a/src/main/java/com/redhat/devtools/lsp4ij/operations/diagnostics/LSPDiagnosticAnnotator.java b/src/main/java/com/redhat/devtools/lsp4ij/operations/diagnostics/LSPDiagnosticAnnotator.java index 1fea42d17..d6d9019fd 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/operations/diagnostics/LSPDiagnosticAnnotator.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/operations/diagnostics/LSPDiagnosticAnnotator.java @@ -44,7 +44,7 @@ public class LSPDiagnosticAnnotator extends ExternalAnnotator @Nullable @Override public Boolean collectInformation(@NotNull PsiFile file, @NotNull Editor editor, boolean hasErrors) { - if (!LanguageServersRegistry.getInstance().isLanguageSupported(file.getLanguage())) { + if (!LanguageServersRegistry.getInstance().isLanguageSupported(file)) { return Boolean.FALSE; } return Boolean.TRUE; diff --git a/src/main/java/com/redhat/devtools/lsp4ij/operations/documentLink/LSPDocumentLinkAnnotator.java b/src/main/java/com/redhat/devtools/lsp4ij/operations/documentLink/LSPDocumentLinkAnnotator.java index 136e318fb..c89e38f63 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/operations/documentLink/LSPDocumentLinkAnnotator.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/operations/documentLink/LSPDocumentLinkAnnotator.java @@ -54,7 +54,7 @@ public class LSPDocumentLinkAnnotator extends ExternalAnnotator collectInformation(@NotNull PsiFile psiFile, @NotNull Editor editor, boolean hasErrors) { - if (!LanguageServersRegistry.getInstance().isLanguageSupported(psiFile.getLanguage())) { + if (!LanguageServersRegistry.getInstance().isLanguageSupported(psiFile)) { return Collections.emptyList(); } Document document = editor.getDocument(); diff --git a/src/main/java/com/redhat/devtools/lsp4ij/operations/documentLink/LSPDocumentLinkGotoDeclarationHandler.java b/src/main/java/com/redhat/devtools/lsp4ij/operations/documentLink/LSPDocumentLinkGotoDeclarationHandler.java index e9ae891ab..eb8173e00 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/operations/documentLink/LSPDocumentLinkGotoDeclarationHandler.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/operations/documentLink/LSPDocumentLinkGotoDeclarationHandler.java @@ -41,7 +41,7 @@ public class LSPDocumentLinkGotoDeclarationHandler implements GotoDeclarationHan @Override public PsiElement @Nullable [] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) { - if (!LanguageServersRegistry.getInstance().isLanguageSupported(sourceElement.getLanguage())) { + if (!LanguageServersRegistry.getInstance().isLanguageSupported(sourceElement.getContainingFile())) { return PsiElement.EMPTY_ARRAY; } Document document = editor.getDocument(); diff --git a/src/main/java/com/redhat/devtools/lsp4ij/operations/documentation/LSPDocumentationProvider.java b/src/main/java/com/redhat/devtools/lsp4ij/operations/documentation/LSPDocumentationProvider.java index 19d3d1f67..6cac020ca 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/operations/documentation/LSPDocumentationProvider.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/operations/documentation/LSPDocumentationProvider.java @@ -70,7 +70,7 @@ public String getQuickNavigateInfo(PsiElement element, PsiElement originalElemen @Nullable @Override public String generateDoc(@NotNull PsiElement element, @Nullable PsiElement originalElement) { - if (!LanguageServersRegistry.getInstance().isLanguageSupported(originalElement.getLanguage())) { + if (!LanguageServersRegistry.getInstance().isLanguageSupported(originalElement.getContainingFile())) { return null; } try { diff --git a/src/main/java/com/redhat/devtools/lsp4ij/operations/highlight/LSPHighlightUsagesHandlerFactory.java b/src/main/java/com/redhat/devtools/lsp4ij/operations/highlight/LSPHighlightUsagesHandlerFactory.java index 8e3e7cf53..2ae36c376 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/operations/highlight/LSPHighlightUsagesHandlerFactory.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/operations/highlight/LSPHighlightUsagesHandlerFactory.java @@ -47,7 +47,7 @@ public class LSPHighlightUsagesHandlerFactory implements HighlightUsagesHandlerF @Override public @Nullable HighlightUsagesHandlerBase createHighlightUsagesHandler(@NotNull Editor editor, @NotNull PsiFile file) { - if (!LanguageServersRegistry.getInstance().isLanguageSupported(file.getLanguage())) { + if (!LanguageServersRegistry.getInstance().isLanguageSupported(file)) { return null; } List targets = getTargets(editor, file); diff --git a/src/main/java/com/redhat/devtools/lsp4ij/operations/navigation/LSPGotoDeclarationHandler.java b/src/main/java/com/redhat/devtools/lsp4ij/operations/navigation/LSPGotoDeclarationHandler.java index 49fb03074..32d9f91d0 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/operations/navigation/LSPGotoDeclarationHandler.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/operations/navigation/LSPGotoDeclarationHandler.java @@ -46,7 +46,7 @@ public class LSPGotoDeclarationHandler implements GotoDeclarationHandler { @Nullable @Override public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) { - if (!LanguageServersRegistry.getInstance().isLanguageSupported(sourceElement.getLanguage())) { + if (!LanguageServersRegistry.getInstance().isLanguageSupported(sourceElement.getContainingFile())) { return null; } VirtualFile file = LSPIJUtils.getFile(sourceElement);