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

refactor: Associate validation, completion, etc with any language #30

Merged
merged 1 commit into from
Dec 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public abstract static class LanguageServerDefinition {
enum Scope {
project, application
}

private static final int DEFAULT_LAST_DOCUMENTED_DISCONNECTED_TIMEOUT = 5;

public final @Nonnull
Expand All @@ -65,7 +66,7 @@ public LanguageServerDefinition(@Nonnull String id, @Nonnull String label, Strin
this.isSingleton = isSingleton;
this.lastDocumentDisconnectedTimeout = lastDocumentDisconnectedTimeout != null && lastDocumentDisconnectedTimeout > 0 ? lastDocumentDisconnectedTimeout : DEFAULT_LAST_DOCUMENTED_DISCONNECTED_TIMEOUT;
this.languageIdMappings = new ConcurrentHashMap<>();
this.scope = scope == null || scope.isBlank()? Scope.application : Scope.valueOf(scope);
this.scope = scope == null || scope.isBlank() ? Scope.application : Scope.valueOf(scope);
this.supportsLightEdit = supportsLightEdit;
setEnabled(true);
}
Expand Down Expand Up @@ -271,7 +272,7 @@ private static class LanguageMapping {

private final DocumentMatcher documentMatcher;

public LanguageMapping(@NotNull Language language, @Nullable String id, @Nullable String languageId, @NotNull DocumentMatcher documentMatcher) {
public LanguageMapping(@NotNull Language language, @Nullable String id, @Nullable String languageId, @NotNull DocumentMatcher documentMatcher) {
this.language = language;
this.id = id;
this.languageId = languageId;
Expand All @@ -290,5 +291,11 @@ public Set<LanguageServerDefinition> getAllDefinitions() {
.collect(Collectors.toSet());
}

public boolean isLanguageSupported(@NotNull Language language) {
return connections
.stream()
.anyMatch(entry -> language.isKindOf(entry.getKey()));
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.lsp4ij;
package com.redhat.devtools.lsp4ij.operations;

import com.intellij.codeInsight.hints.*;
import com.intellij.codeInsight.hints.presentation.PresentationFactory;
Expand All @@ -25,6 +25,8 @@
import com.intellij.psi.PsiFile;
import com.intellij.ui.layout.LCFlags;
import com.intellij.ui.layout.LayoutKt;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LanguageServersRegistry;
import com.redhat.devtools.lsp4ij.commands.CommandExecutor;
import com.redhat.devtools.lsp4ij.internal.CancellationSupport;
import org.eclipse.lsp4j.Command;
Expand All @@ -40,6 +42,15 @@ public abstract class AbstractLSPInlayProvider implements InlayHintsProvider<NoS

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractLSPInlayProvider.class);

private static final InlayHintsCollector EMPTY_INLAY_HINTS_COLLECTOR = new InlayHintsCollector() {

@Override
public boolean collect(@NotNull PsiElement psiElement, @NotNull Editor editor, @NotNull InlayHintsSink inlayHintsSink) {
// Do nothing
return true;
}
};

private final Key<CancellationSupport> cancellationSupportKey;

protected AbstractLSPInlayProvider(Key<CancellationSupport> cancellationSupportKey) {
Expand All @@ -55,6 +66,11 @@ public final InlayHintsCollector getCollectorFor(@NotNull PsiFile psiFile,
@NotNull Editor editor,
@NotNull NoSettings o,
@NotNull InlayHintsSink inlayHintsSink) {

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

CancellationSupport previousCancellationSupport = editor.getUserData(cancellationSupportKey);
if (previousCancellationSupport != null) {
previousCancellationSupport.cancel();
Expand Down Expand Up @@ -147,7 +163,7 @@ public boolean isLanguageSupported(@NotNull Language language) {
return true;
}

protected void executeClientCommand(Component source, Command command) {
protected void executeClientCommand(@NotNull Component source, @NotNull Command command, @NotNull Project project) {
if (command != null) {
AnAction action = ActionManager.getInstance().getAction(command.getCommand());
if (action != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.lsp4ij.AbstractLSPInlayProvider;
import com.redhat.devtools.lsp4ij.operations.AbstractLSPInlayProvider;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LanguageServiceAccessor;
import com.redhat.devtools.lsp4ij.internal.CancellationSupport;
Expand Down Expand Up @@ -148,10 +148,10 @@ private void executeClientCommand(LanguageServer languageServer, CodeLens codeLe
Boolean.TRUE.equals(capabilities.getCodeLensProvider().getResolveProvider()))
) {
languageServer.getTextDocumentService().resolveCodeLens(codeLens).thenAcceptAsync(resolvedCodeLens ->
executeClientCommand(source, resolvedCodeLens.getCommand())
executeClientCommand(source, resolvedCodeLens.getCommand(), project)
);
} else {
executeClientCommand(source, codeLens.getCommand());
executeClientCommand(source, codeLens.getCommand(), project);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.intellij.psi.PsiFile;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LanguageServerItem;
import com.redhat.devtools.lsp4ij.LanguageServersRegistry;
import com.redhat.devtools.lsp4ij.LanguageServiceAccessor;
import com.redhat.devtools.lsp4ij.internal.CancellationSupport;
import com.redhat.devtools.lsp4ij.internal.StringUtils;
Expand Down Expand Up @@ -56,6 +57,10 @@ public void fillCompletionVariants(@NotNull CompletionParameters parameters, @No
return;
}

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

Editor editor = parameters.getEditor();
Document document = editor.getDocument();
Project project = psiFile.getProject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.intellij.psi.PsiFile;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LSPVirtualFileData;
import com.redhat.devtools.lsp4ij.LanguageServersRegistry;
import com.redhat.devtools.lsp4ij.LanguageServiceAccessor;
import com.redhat.devtools.lsp4ij.operations.codeactions.LSPLazyCodeActionIntentionAction;
import org.eclipse.lsp4j.Diagnostic;
Expand All @@ -40,19 +41,25 @@
*/
public class LSPDiagnosticAnnotator extends ExternalAnnotator<Boolean, Boolean> {

@Nullable
@Override
public Boolean collectInformation(@NotNull PsiFile file, @NotNull Editor editor, boolean hasErrors) {
return Boolean.TRUE;
}

@Override
public @Nullable Boolean doAnnotate(Boolean unused) {
@Nullable
@Override
public Boolean collectInformation(@NotNull PsiFile file, @NotNull Editor editor, boolean hasErrors) {
if (!LanguageServersRegistry.getInstance().isLanguageSupported(file.getLanguage())) {
return Boolean.FALSE;
}
angelozerr marked this conversation as resolved.
Show resolved Hide resolved
return Boolean.TRUE;
}
}

@Override
public void apply(@NotNull PsiFile file, Boolean unused, @NotNull AnnotationHolder holder) {
public @Nullable Boolean doAnnotate(Boolean result) {
return result;
angelozerr marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void apply(@NotNull PsiFile file, Boolean applyAnnotator, @NotNull AnnotationHolder holder) {
if (!applyAnnotator) {
return;
}
URI fileUri = LSPIJUtils.toUri(file);
Document document = LSPIJUtils.getDocument(file.getVirtualFile());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LSPVirtualFileData;
import com.redhat.devtools.lsp4ij.LanguageServerWrapper;
import com.redhat.devtools.lsp4ij.LanguageServiceAccessor;
import com.redhat.devtools.lsp4ij.*;
import com.redhat.devtools.lsp4ij.internal.CancellationSupport;
import org.eclipse.lsp4j.DocumentLink;
import org.eclipse.lsp4j.DocumentLinkParams;
Expand All @@ -40,6 +37,7 @@

import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.CompletableFuture;
Expand All @@ -56,10 +54,13 @@ 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())) {
return Collections.emptyList();
}
Document document = editor.getDocument();
VirtualFile file = LSPIJUtils.getFile(document);
if (file == null) {
return null;
return Collections.emptyList();
}
List<LSPVirtualFileData> datas = new ArrayList<>();
final CancellationSupport cancellationSupport = new CancellationSupport();
Expand Down Expand Up @@ -110,7 +111,7 @@ public List<LSPVirtualFileData> collectInformation(@NotNull PsiFile psiFile, @No
}

@Override
public void apply(@NotNull PsiFile file, List<LSPVirtualFileData> datas, @NotNull AnnotationHolder holder) {
public void apply(@NotNull PsiFile file, @NotNull List<LSPVirtualFileData> datas, @NotNull AnnotationHolder holder) {
if (datas.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiManager;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LSPVirtualFileData;
import com.redhat.devtools.lsp4ij.LanguageServerBundle;
import com.redhat.devtools.lsp4ij.LanguageServiceAccessor;
import com.redhat.devtools.lsp4ij.*;
import org.eclipse.lsp4j.DocumentLink;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
* {@link GotoDeclarationHandler} implementation used to open LSP document link with CTrl+Click.
Expand All @@ -43,6 +41,9 @@ public class LSPDocumentLinkGotoDeclarationHandler implements GotoDeclarationHan

@Override
public PsiElement @Nullable [] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) {
if (!LanguageServersRegistry.getInstance().isLanguageSupported(sourceElement.getLanguage())) {
return PsiElement.EMPTY_ARRAY;
}
Document document = editor.getDocument();
VirtualFile file = LSPIJUtils.getFile(document);
Module module = LSPIJUtils.getModule(file, sourceElement.getProject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.intellij.psi.PsiManager;
import com.intellij.util.io.URLUtil;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LanguageServersRegistry;
import com.redhat.devtools.lsp4ij.operations.completion.LSPCompletionProposal;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
Expand All @@ -30,6 +31,7 @@
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -68,6 +70,9 @@ public String getQuickNavigateInfo(PsiElement element, PsiElement originalElemen
@Nullable
@Override
public String generateDoc(@NotNull PsiElement element, @Nullable PsiElement originalElement) {
if (!LanguageServersRegistry.getInstance().isLanguageSupported(originalElement.getLanguage())) {
return null;
}
try {
Project project = element.getProject();
if (project.isDisposed()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LanguageServersRegistry;
import com.redhat.devtools.lsp4ij.LanguageServiceAccessor;
import com.redhat.devtools.lsp4ij.internal.CancellationSupport;
import org.eclipse.lsp4j.DocumentHighlight;
Expand All @@ -45,6 +47,9 @@ public class LSPHighlightUsagesHandlerFactory implements HighlightUsagesHandlerF

@Override
public @Nullable HighlightUsagesHandlerBase createHighlightUsagesHandler(@NotNull Editor editor, @NotNull PsiFile file) {
if (!LanguageServersRegistry.getInstance().isLanguageSupported(file.getLanguage())) {
return null;
}
List<LSPHighlightPsiElement> targets = getTargets(editor, file);
return targets.isEmpty() ? null : new LSPHighlightUsagesHandler(editor, file, targets);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.redhat.devtools.lsp4ij.AbstractLSPInlayProvider;
import com.redhat.devtools.lsp4ij.operations.AbstractLSPInlayProvider;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LanguageServiceAccessor;
import com.redhat.devtools.lsp4ij.internal.CancellationSupport;
Expand Down Expand Up @@ -174,10 +174,10 @@ private void executeClientCommand(
languageServer.getTextDocumentService()
.resolveInlayHint(inlayHint)
.thenAcceptAsync(resolvedInlayHint -> {
executeClientCommand(source, resolvedInlayHint.getLabel().getRight().get(index).getCommand());
executeClientCommand(source, resolvedInlayHint.getLabel().getRight().get(index).getCommand(), project);
});
} else {
executeClientCommand(source, inlayHint.getLabel().getRight().get(index).getCommand());
executeClientCommand(source, inlayHint.getLabel().getRight().get(index).getCommand(), project);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.redhat.devtools.lsp4ij.LSPIJUtils;
import com.redhat.devtools.lsp4ij.LanguageServersRegistry;
import com.redhat.devtools.lsp4ij.LanguageServiceAccessor;
import com.redhat.devtools.lsp4ij.internal.CancellationSupport;
import com.redhat.devtools.lsp4ij.internal.CancellationUtil;
Expand All @@ -45,6 +46,9 @@ public class LSPGotoDeclarationHandler implements GotoDeclarationHandler {
@Nullable
@Override
public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement sourceElement, int offset, Editor editor) {
if (!LanguageServersRegistry.getInstance().isLanguageSupported(sourceElement.getLanguage())) {
return null;
}
VirtualFile file = LSPIJUtils.getFile(sourceElement);
if (file == null) {
return PsiElement.EMPTY_ARRAY;
Expand Down
Loading
Loading