From 27c0522a40e4f9a3c6c02f93a60abef1be986a83 Mon Sep 17 00:00:00 2001 From: azerr Date: Wed, 9 Oct 2024 11:46:47 +0200 Subject: [PATCH] fix: add languageId filter for options similar to microprofile Signed-off-by: azerr --- .../redhat/quarkus/QuarkusLanguageIds.java | 25 +++++++ .../QuarkusRegistrationConfiguration.java | 27 ++++--- .../java/com/redhat/qute/QuteLanguageIds.java | 32 ++++++++ .../capabilities/QuteCapabilityManager.java | 75 ++++++++++++++----- .../redhat/qute/utils/DocumentationUtils.java | 5 +- 5 files changed, 129 insertions(+), 35 deletions(-) create mode 100644 quarkus.ls.ext/com.redhat.quarkus.ls/src/main/java/com/redhat/quarkus/QuarkusLanguageIds.java create mode 100644 qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/QuteLanguageIds.java diff --git a/quarkus.ls.ext/com.redhat.quarkus.ls/src/main/java/com/redhat/quarkus/QuarkusLanguageIds.java b/quarkus.ls.ext/com.redhat.quarkus.ls/src/main/java/com/redhat/quarkus/QuarkusLanguageIds.java new file mode 100644 index 000000000..cd8115bfd --- /dev/null +++ b/quarkus.ls.ext/com.redhat.quarkus.ls/src/main/java/com/redhat/quarkus/QuarkusLanguageIds.java @@ -0,0 +1,25 @@ +/******************************************************************************* +* Copyright (c) 2024 Red Hat Inc. and others. +* +* This program and the accompanying materials are made available under the +* terms of the Eclipse Public License v. 2.0 which is available at +* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +* which is available at https://www.apache.org/licenses/LICENSE-2.0. +* +* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +* +* Contributors: +* Red Hat Inc. - initial API and implementation +*******************************************************************************/ +package com.redhat.quarkus; + +/** + * Quarkus language ids. + */ +public class QuarkusLanguageIds { + + public static final String QUARKUS_PROPERTIES = "quarkus-properties"; + + private QuarkusLanguageIds() { + } +} diff --git a/quarkus.ls.ext/com.redhat.quarkus.ls/src/main/java/com/redhat/quarkus/settings/capabilities/QuarkusRegistrationConfiguration.java b/quarkus.ls.ext/com.redhat.quarkus.ls/src/main/java/com/redhat/quarkus/settings/capabilities/QuarkusRegistrationConfiguration.java index 831a5c929..b4c9e4bbb 100644 --- a/quarkus.ls.ext/com.redhat.quarkus.ls/src/main/java/com/redhat/quarkus/settings/capabilities/QuarkusRegistrationConfiguration.java +++ b/quarkus.ls.ext/com.redhat.quarkus.ls/src/main/java/com/redhat/quarkus/settings/capabilities/QuarkusRegistrationConfiguration.java @@ -12,15 +12,16 @@ */ package com.redhat.quarkus.settings.capabilities; -import static org.eclipse.lsp4mp.settings.capabilities.ServerCapabilitiesConstants.TEXT_DOCUMENT_FORMATTING; -import static org.eclipse.lsp4mp.settings.capabilities.ServerCapabilitiesConstants.TEXT_DOCUMENT_RANGE_FORMATTING; +import java.util.List; import org.eclipse.lsp4j.DocumentFilter; import org.eclipse.lsp4j.Registration; import org.eclipse.lsp4j.TextDocumentRegistrationOptions; - +import org.eclipse.lsp4mp.MicroProfileLanguageIds; import org.eclipse.lsp4mp.settings.capabilities.IMicroProfileRegistrationConfiguration; +import com.redhat.quarkus.QuarkusLanguageIds; + /** * Specific Quarkus LSP Registration configuration * @@ -29,18 +30,16 @@ public class QuarkusRegistrationConfiguration implements IMicroProfileRegistrati @Override public void configure(Registration registration) { - switch (registration.getMethod()) { - case TEXT_DOCUMENT_FORMATTING: - case TEXT_DOCUMENT_RANGE_FORMATTING: - // add "quarkus-properties" as language document filter for formatting - ((TextDocumentRegistrationOptions) registration.getRegisterOptions()).getDocumentSelector() - .add(new DocumentFilter("quarkus-properties", null, null)); - break; - default: - break; - + if (registration.getRegisterOptions() instanceof TextDocumentRegistrationOptions) { + List documentSelector = ((TextDocumentRegistrationOptions) registration + .getRegisterOptions()).getDocumentSelector(); + if (documentSelector != null && documentSelector.stream().anyMatch(filter -> filter != null + && MicroProfileLanguageIds.MICROPROFILE_PROPERTIES.equals(filter.getLanguage()))) { + // Add "quarkus-properties" in the list of filter which contains + // "microprofile-properties" + documentSelector.add(new DocumentFilter(QuarkusLanguageIds.QUARKUS_PROPERTIES, null, null)); + } } - } } diff --git a/qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/QuteLanguageIds.java b/qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/QuteLanguageIds.java new file mode 100644 index 000000000..2ecd54285 --- /dev/null +++ b/qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/QuteLanguageIds.java @@ -0,0 +1,32 @@ +/******************************************************************************* +* Copyright (c) 2024 Red Hat Inc. and others. +* All rights reserved. This program and the accompanying materials +* which accompanies this distribution, and is available at +* http://www.eclipse.org/legal/epl-v20.html +* +* SPDX-License-Identifier: EPL-2.0 +* +* Contributors: +* Red Hat Inc. - initial API and implementation +*******************************************************************************/ +package com.redhat.qute; + +/** + * Qute language Ids. + */ +public class QuteLanguageIds { + + public static final String QUTE_HTML = "qute-html"; + + public static final String QUTE_JSON = "qute-json"; + + public static final String QUTE_YAML = "qute-yaml"; + + public static final String QUTE_TXT = "qute-txt"; + + public static final String[] QUTE_ALL = { QUTE_HTML, QUTE_JSON, QUTE_YAML, QUTE_TXT }; + + private QuteLanguageIds() { + + } +} diff --git a/qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/settings/capabilities/QuteCapabilityManager.java b/qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/settings/capabilities/QuteCapabilityManager.java index 637374cd6..333595cd7 100644 --- a/qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/settings/capabilities/QuteCapabilityManager.java +++ b/qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/settings/capabilities/QuteCapabilityManager.java @@ -15,7 +15,6 @@ import static com.redhat.qute.settings.capabilities.ServerCapabilitiesConstants.CODE_LENS_ID; import static com.redhat.qute.settings.capabilities.ServerCapabilitiesConstants.COMPLETION_ID; import static com.redhat.qute.settings.capabilities.ServerCapabilitiesConstants.DEFAULT_CODELENS_OPTIONS; -import static com.redhat.qute.settings.capabilities.ServerCapabilitiesConstants.DEFAULT_COMPLETION_OPTIONS; import static com.redhat.qute.settings.capabilities.ServerCapabilitiesConstants.DEFAULT_DOCUMENT_LINK_OPTIONS; import static com.redhat.qute.settings.capabilities.ServerCapabilitiesConstants.DOCUMENT_DEFINITION_ID; import static com.redhat.qute.settings.capabilities.ServerCapabilitiesConstants.DOCUMENT_HIGHLIGHT_ID; @@ -44,20 +43,35 @@ import static com.redhat.qute.settings.capabilities.ServerCapabilitiesConstants.WORKSPACE_WATCHED_FILES_ID; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import org.eclipse.lsp4j.ClientCapabilities; +import org.eclipse.lsp4j.CodeActionKind; +import org.eclipse.lsp4j.CodeActionRegistrationOptions; +import org.eclipse.lsp4j.CompletionRegistrationOptions; +import org.eclipse.lsp4j.DefinitionRegistrationOptions; import org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions; +import org.eclipse.lsp4j.DocumentFilter; +import org.eclipse.lsp4j.DocumentHighlightRegistrationOptions; +import org.eclipse.lsp4j.DocumentSymbolRegistrationOptions; import org.eclipse.lsp4j.ExecuteCommandOptions; import org.eclipse.lsp4j.FileSystemWatcher; +import org.eclipse.lsp4j.HoverRegistrationOptions; +import org.eclipse.lsp4j.InlayHintRegistrationOptions; +import org.eclipse.lsp4j.LinkedEditingRangeRegistrationOptions; +import org.eclipse.lsp4j.ReferenceRegistrationOptions; import org.eclipse.lsp4j.Registration; import org.eclipse.lsp4j.RegistrationParams; +import org.eclipse.lsp4j.RenameOptions; +import org.eclipse.lsp4j.TextDocumentRegistrationOptions; import org.eclipse.lsp4j.jsonrpc.messages.Either; import org.eclipse.lsp4j.services.LanguageClient; +import com.redhat.qute.QuteLanguageIds; import com.redhat.qute.ls.commons.client.ExtendedClientCapabilities; /** @@ -80,43 +94,63 @@ public QuteCapabilityManager(LanguageClient languageClient) { */ public void initializeCapabilities() { if (this.getClientCapabilities().isCodeActionDynamicRegistered()) { - registerCapability(CODE_ACTION_ID, TEXT_DOCUMENT_CODE_ACTION, ServerCapabilitiesConstants.DEFAULT_CODE_ACTION_OPTIONS); + // Code action is only available for Qute templates + CodeActionRegistrationOptions options = new CodeActionRegistrationOptions( + Arrays.asList(CodeActionKind.QuickFix, CodeActionKind.Empty)); + options.setResolveProvider(true); + registerCapability(CODE_ACTION_ID, TEXT_DOCUMENT_CODE_ACTION, options, QuteLanguageIds.QUTE_ALL); } if (this.getClientCapabilities().isCodeLensDynamicRegistered()) { + // Code Lens is available for Qute templates and Java both registerCapability(CODE_LENS_ID, TEXT_DOCUMENT_CODE_LENS, DEFAULT_CODELENS_OPTIONS); } if (this.getClientCapabilities().isCompletionDynamicRegistrationSupported()) { - registerCapability(COMPLETION_ID, TEXT_DOCUMENT_COMPLETION, DEFAULT_COMPLETION_OPTIONS); + // Completion is only available for Qute templates + CompletionRegistrationOptions options = new CompletionRegistrationOptions( + Arrays.asList("{", "@", "#", ".", ":", "$", "!"), false); + registerCapability(COMPLETION_ID, TEXT_DOCUMENT_COMPLETION, options, QuteLanguageIds.QUTE_ALL); } if (this.getClientCapabilities().isDefinitionDynamicRegistered()) { - registerCapability(DOCUMENT_DEFINITION_ID, TEXT_DOCUMENT_DEFINITION); - } - if (this.getClientCapabilities().isDocumentLinkDynamicRegistered()) { - registerCapability(DOCUMENT_DEFINITION_ID, TEXT_DOCUMENT_DEFINITION); + // Definition is only available for Qute templates + registerCapability(DOCUMENT_DEFINITION_ID, TEXT_DOCUMENT_DEFINITION, new DefinitionRegistrationOptions(), + QuteLanguageIds.QUTE_ALL); } if (this.getClientCapabilities().isDocumentHighlightDynamicRegistered()) { - registerCapability(DOCUMENT_HIGHLIGHT_ID, TEXT_DOCUMENT_HIGHLIGHT); + // Document highlight is only available for Qute templates + registerCapability(DOCUMENT_HIGHLIGHT_ID, TEXT_DOCUMENT_HIGHLIGHT, + new DocumentHighlightRegistrationOptions(), QuteLanguageIds.QUTE_ALL); } if (this.getClientCapabilities().isDocumentLinkDynamicRegistered()) { + // Document link is available for Qute templates and Java both registerCapability(DOCUMENT_LINK_ID, TEXT_DOCUMENT_DOCUMENT_LINK, DEFAULT_DOCUMENT_LINK_OPTIONS); } if (this.getClientCapabilities().isDocumentSymbolDynamicRegistrationSupported()) { - registerCapability(DOCUMENT_SYMBOL_ID, TEXT_DOCUMENT_DOCUMENT_SYMBOL); + // Document symbol is only available for Qute templates + registerCapability(DOCUMENT_SYMBOL_ID, TEXT_DOCUMENT_DOCUMENT_SYMBOL, + new DocumentSymbolRegistrationOptions(), QuteLanguageIds.QUTE_ALL); } if (this.getClientCapabilities().isHoverDynamicRegistered()) { - registerCapability(HOVER_ID, TEXT_DOCUMENT_HOVER); + // Hover is only available for Qute templates + registerCapability(HOVER_ID, TEXT_DOCUMENT_HOVER, new HoverRegistrationOptions(), QuteLanguageIds.QUTE_ALL); } if (this.getClientCapabilities().isLinkedEditingRangeDynamicRegistered()) { - registerCapability(LINKED_EDITING_RANGE_ID, TEXT_DOCUMENT_LINKED_EDITING_RANGE); + // Linked editing range is only available for Qute templates + registerCapability(LINKED_EDITING_RANGE_ID, TEXT_DOCUMENT_LINKED_EDITING_RANGE, + new LinkedEditingRangeRegistrationOptions(), QuteLanguageIds.QUTE_ALL); } if (this.getClientCapabilities().isReferencesDynamicRegistrationSupported()) { - registerCapability(REFERENCES_ID, TEXT_DOCUMENT_REFERENCES); + // References is only available for Qute templates + registerCapability(REFERENCES_ID, TEXT_DOCUMENT_REFERENCES, new ReferenceRegistrationOptions(), + QuteLanguageIds.QUTE_ALL); } if (this.getClientCapabilities().isRenameDynamicRegistered()) { - registerCapability(RENAME_ID, TEXT_DOCUMENT_RENAME); + // Rename is only available for Qute templates + registerCapability(RENAME_ID, TEXT_DOCUMENT_RENAME, new RenameOptions(), QuteLanguageIds.QUTE_ALL); } if (this.getClientCapabilities().isInlayHintDynamicRegistered()) { - registerCapability(INLAY_HINT_ID, TEXT_DOCUMENT_INLAY_HINT); + // Inlay Hint is only available for Qute templates + registerCapability(INLAY_HINT_ID, TEXT_DOCUMENT_INLAY_HINT, new InlayHintRegistrationOptions(), + QuteLanguageIds.QUTE_ALL); } if (this.getClientCapabilities().isDidChangeWatchedFilesRegistered()) { registerWatchedFiles(); @@ -150,12 +184,15 @@ public Set getRegisteredCapabilities() { return registeredCapabilities; } - private void registerCapability(String id, String method) { - registerCapability(id, method, null); - } - - private void registerCapability(String id, String method, Object options) { + private void registerCapability(String id, String method, Object options, String... languageIds) { if (registeredCapabilities.add(id)) { + if (languageIds != null && languageIds.length > 0) { + List documentSelector = new ArrayList<>(); + ((TextDocumentRegistrationOptions) options).setDocumentSelector(documentSelector); + for (String languageId : languageIds) { + documentSelector.add(new DocumentFilter(languageId, null, null)); + } + } Registration registration = new Registration(id, method, options); RegistrationParams registrationParams = new RegistrationParams(Collections.singletonList(registration)); languageClient.registerCapability(registrationParams); diff --git a/qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/utils/DocumentationUtils.java b/qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/utils/DocumentationUtils.java index f856dc4c1..590a011c6 100644 --- a/qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/utils/DocumentationUtils.java +++ b/qute.ls/com.redhat.qute.ls/src/main/java/com/redhat/qute/utils/DocumentationUtils.java @@ -21,6 +21,7 @@ import org.eclipse.lsp4j.MarkupContent; import org.eclipse.lsp4j.MarkupKind; +import com.redhat.qute.QuteLanguageIds; import com.redhat.qute.commons.JavaElementKind; import com.redhat.qute.commons.JavaMemberInfo; import com.redhat.qute.commons.JavaMethodInfo; @@ -94,7 +95,7 @@ public static MarkupContent getDocumentation(MethodValueResolver resolver, documentation.append(":"); documentation.append(System.lineSeparator()); if (markdown) { - documentation.append("```qute-html"); + documentation.append("```" + QuteLanguageIds.QUTE_HTML); documentation.append(System.lineSeparator()); } for (int i = 0; i < resolver.getSample().size(); i++) { @@ -249,7 +250,7 @@ public static MarkupContent getDocumentation(CaseOperator operator, boolean mark documentation.append(":"); documentation.append(System.lineSeparator()); if (markdown) { - documentation.append("```qute-html"); + documentation.append("```" + QuteLanguageIds.QUTE_HTML); documentation.append(System.lineSeparator()); } for (int i = 0; i < operator.getSample().size(); i++) {