Skip to content

Commit

Permalink
feat: Support documentSelector in registerCapability
Browse files Browse the repository at this point in the history
Fixes #517

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Oct 8, 2024
1 parent 0dfc9fa commit 5f2d875
Show file tree
Hide file tree
Showing 70 changed files with 2,576 additions and 804 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@
******************************************************************************/
package com.redhat.devtools.lsp4ij;

import com.intellij.lang.Language;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDocumentManager;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.net.URI;
import java.util.ArrayList;
Expand All @@ -50,8 +47,9 @@ public class DocumentContentSynchronizer implements DocumentListener {

public DocumentContentSynchronizer(@NotNull LanguageServerWrapper languageServerWrapper,
@NotNull URI fileUri,
@NotNull VirtualFile file,
@NotNull Document document,
TextDocumentSyncKind syncKind) {
@Nullable TextDocumentSyncKind syncKind) {
this.languageServerWrapper = languageServerWrapper;
this.fileUri = fileUri.toASCIIString();
this.syncKind = syncKind != null ? syncKind : TextDocumentSyncKind.Full;
Expand All @@ -62,7 +60,7 @@ public DocumentContentSynchronizer(@NotNull LanguageServerWrapper languageServer
textDocument.setUri(this.fileUri);
textDocument.setText(document.getText());

@NotNull String languageId = getLanguageId(document, languageServerWrapper);
@NotNull String languageId = languageServerWrapper.getServerDefinition().getLanguageId(file, languageServerWrapper.getProject());
textDocument.setLanguageId(languageId);
textDocument.setVersion(++version);
didOpenFuture = languageServerWrapper
Expand All @@ -89,54 +87,6 @@ public DocumentContentSynchronizer(@NotNull LanguageServerWrapper languageServer
changeEvents = new ArrayList<>();
}

/**
* Returns the LSP language id defined in mapping otherwise the {@link Language#getID()} otherwise the {@link FileType#getName()} otherwise 'unknown'.
*
* @param document the document.
* @param languageServer the language server.
* @return the LSP language id.
*/
private static @NotNull String getLanguageId(@NotNull Document document, @NotNull LanguageServerWrapper languageServer) {
VirtualFile file = FileDocumentManager.getInstance().getFile(document);
if (file == null) {
return FileTypes.UNKNOWN.getName().toLowerCase();
}

Project project = languageServer.getProject();

// 1. Try to get the LSP languageId by using language mapping
Language language = LSPIJUtils.getFileLanguage(file, project);
String languageId = languageServer.getLanguageId(language);
if (languageId != null) {
return languageId;
}

// 2. Try to get the LSP languageId by using the fileType mapping
FileType fileType = file.getFileType();
languageId = languageServer.getLanguageId(fileType);
if (languageId != null) {
return languageId;
}

// 3. Try to get the LSP languageId by using the file name pattern mapping
languageId = languageServer.getLanguageId(file.getName());
if (languageId != null) {
return languageId;
}

// At this step there is no mapping

// We return the language Id if it exists or file type name
// with 'lower case' to try to map the recommended languageId specified at
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocumentItem
if (language != null) {
// The language exists, use its ID with lower case
return language.getID().toLowerCase();
}
// Returns the existing file type or 'unknown' with lower case
return file.getName().toLowerCase();
}

@Override
public void documentChanged(@NotNull DocumentEvent event) {
DocumentListener.super.documentChanged(event);
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/com/redhat/devtools/lsp4ij/LSPIJUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,23 +321,6 @@ private static Language doGetFileLanguage(@NotNull VirtualFile file, @NotNull Pr
return LanguageUtil.getLanguageForPsi(project, file);
}

private static <T extends TextDocumentPositionParams> T toTextDocumentPositionParamsCommon(T param, int offset, Document document) {
Position start = toPosition(offset, document);
param.setPosition(start);
TextDocumentIdentifier id = new TextDocumentIdentifier();
URI uri = toUri(document);
if (uri != null) {
id.setUri(uri.toASCIIString());
}
param.setTextDocument(id);
return param;
}

public static HoverParams toHoverParams(int offset, Document document) {
return toTextDocumentPositionParamsCommon(new HoverParams(), offset, document);
}


/**
* Returns the Uri of the virtual file corresponding to the specified document.
*
Expand Down
Loading

0 comments on commit 5f2d875

Please sign in to comment.