Skip to content

Commit

Permalink
fix: fix language supported to use language substitor.
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Dec 12, 2023
1 parent a60c5b9 commit 7b5309e
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/com/redhat/devtools/lsp4ij/LSPIJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -109,10 +120,6 @@ private static <T extends TextDocumentPositionParams> 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);
}
Expand Down Expand Up @@ -428,7 +435,15 @@ private static void applyWorkspaceEdit(Document document, List<TextEdit> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -294,13 +295,33 @@ public Collection<LanguageServerDefinition> 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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,10 @@ private MatchedLanguageServerDefinitions getMatchedLanguageServerDefinitions(@No
// look for running language servers via content-type
Queue<Language> contentTypes = new LinkedList<>();
Set<Language> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void fillCompletionVariants(@NotNull CompletionParameters parameters, @No
return;
}

if (!LanguageServersRegistry.getInstance().isLanguageSupported(psiFile.getLanguage())) {
if (!LanguageServersRegistry.getInstance().isLanguageSupported(psiFile)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class LSPDiagnosticAnnotator extends ExternalAnnotator<Boolean, Boolean>
@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class LSPDocumentLinkAnnotator extends ExternalAnnotator<List<LSPVirtualF
@Nullable
@Override
public List<LSPVirtualFileData> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LSPHighlightPsiElement> targets = getTargets(editor, file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7b5309e

Please sign in to comment.