From 83be54359a7394209d614b3989897a23ea6e1394 Mon Sep 17 00:00:00 2001 From: azerr Date: Sat, 28 Nov 2026 09:26:37 +0100 Subject: [PATCH] Implement xml.update.configuration command to register/unregister catalogs - Add UpdateConfigurationHandler: LSP command handler for xml.update.configuration that processes ConfigurationItemEdit requests (Add/Delete) for xml.catalogs section - Implement ConfigurationItemEditType enum for type-safe edit operations (Add=1, Delete=2) compatible with Gson deserialization from LSP command arguments - Use ConfigurationItemEdit record to hold section, value, and editType from LSP request - Register handler in plugin.xml for xml.update.configuration command - Support catalog registration/unregistration via "Register Catalog" / "Unregister Catalog" code lens in LemMinX - Refactor AssociateGrammarHandler, OpenUriHandler, ShowReferencesHandler to use ArgumentsUtils.getArgAt() for type-safe argument extraction instead of manual casting - Add inline documentation comments explaining command flow and configuration updates Signed-off-by: azerr --- org.eclipse.wildwebdeveloper.xml/plugin.xml | 4 + .../xml/internal/commands/ArgumentsUtils.java | 96 +++++++++++++ .../commands/AssociateGrammarHandler.java | 12 +- .../xml/internal/commands/OpenUriHandler.java | 5 +- .../commands/ShowReferencesHandler.java | 8 +- .../commands/UpdateConfigurationHandler.java | 127 ++++++++++++++++++ 6 files changed, 236 insertions(+), 16 deletions(-) create mode 100644 org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/ArgumentsUtils.java create mode 100644 org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/UpdateConfigurationHandler.java diff --git a/org.eclipse.wildwebdeveloper.xml/plugin.xml b/org.eclipse.wildwebdeveloper.xml/plugin.xml index 4f393bdaee..1cb6053de1 100644 --- a/org.eclipse.wildwebdeveloper.xml/plugin.xml +++ b/org.eclipse.wildwebdeveloper.xml/plugin.xml @@ -238,5 +238,9 @@ class="org.eclipse.wildwebdeveloper.xml.internal.commands.ShowReferencesHandler" commandId="xml.show.references"> + + diff --git a/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/ArgumentsUtils.java b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/ArgumentsUtils.java new file mode 100644 index 0000000000..17a19ed40a --- /dev/null +++ b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/ArgumentsUtils.java @@ -0,0 +1,96 @@ +/******************************************************************************* + * Copyright (c) 2026 Red Hat Inc. and others. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Angelo ZERR (Red Hat Inc.) - initial implementation + *******************************************************************************/ +package org.eclipse.wildwebdeveloper.xml.internal.commands; + +import java.util.List; +import java.util.Map; + +import org.eclipse.lsp4j.Command; +import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter; +import org.eclipse.lsp4j.jsonrpc.json.adapters.EnumTypeAdapter; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; + +/** + * Arguments utilities. + * + * @author Angelo ZERR + * + */ +public class ArgumentsUtils { + + private static Gson gson = getDefaultGsonBuilder().create(); + + /** + * Returns the object from the given index as a given class type and null + * otherwise. + * + * @param the class type + * @param command the LSP command. + * @param index the index + * @param clazz the class type. + * @return the object from the given index as a given class type and null + * otherwise. + * @throws UnsupportedOperationException if the object from the given index + * cannot be retrieved from the params. + */ + public static T getArgAt(Command command, int index, Class clazz) { + Object obj = getArgAt(command.getArguments(), index); + if (obj == null) { + throw new IllegalArgumentException("Cannot find command argument at " + index); + } + return toModel(obj, clazz); + } + + /** + * Returns the object from the given index and null otherwise. + * + * @param arguments the argument list. + * @param index the index. + * + * @return the object from the given index and null otherwise. + */ + public static Object getArgAt(List arguments, int index) { + if (arguments == null || index >= arguments.size()) { + return null; + } + return arguments.get(index); + } + + private static T toModel(Object object, Class clazz) { + if (object == null) { + return null; + } + if (clazz == null) { + throw new IllegalArgumentException("Class can not be null"); + } + if (object instanceof JsonElement) { + return gson.fromJson((JsonElement) object, clazz); + } + if (object instanceof Map) { + return gson.fromJson(gson.toJsonTree(object), clazz); + } + if (clazz.isInstance(object)) { + return clazz.cast(object); + } + return null; + } + + private static GsonBuilder getDefaultGsonBuilder() { + return new GsonBuilder() // + .registerTypeAdapterFactory(new EnumTypeAdapter.Factory()) // + .registerTypeAdapterFactory(new EitherTypeAdapter.Factory()); + } + +} diff --git a/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/AssociateGrammarHandler.java b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/AssociateGrammarHandler.java index bccde9d7a1..65a7947790 100644 --- a/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/AssociateGrammarHandler.java +++ b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/AssociateGrammarHandler.java @@ -59,7 +59,7 @@ public class AssociateGrammarHandler extends LSPCommandHandler { @Override public Object execute(ExecutionEvent event, Command command, IPath path) throws ExecutionException { - String uri = (String) command.getArguments().get(0); + String uri = ArgumentsUtils.getArgAt(command, 0, String.class); // Open the association grammar dialog. AssociateGrammarDialog dialog = new AssociateGrammarDialog(HandlerUtil.getActiveShell(event), path); if (dialog.open() == IDialogConstants.OK_ID) { @@ -88,11 +88,11 @@ public Object execute(ExecutionEvent event, Command command, IPath path) throws private static CompletableFuture executeServerCommand(String commandId, Object... params) throws Exception { List commandHandlers = new ArrayList<>(); LanguageServers.forProject(null).withCapability(serverCapabilities -> { - ExecuteCommandOptions executeCommandProvider = serverCapabilities.getExecuteCommandProvider(); - return Either.forLeft(executeCommandProvider != null ? - executeCommandProvider.getCommands().contains(commandId) : false); - }).excludeInactive().collectAll((w, ls) -> CompletableFuture.completedFuture(ls)) - .thenAccept(commandHandlers::addAll).get(100, TimeUnit.MILLISECONDS); + ExecuteCommandOptions executeCommandProvider = serverCapabilities.getExecuteCommandProvider(); + return Either.forLeft( + executeCommandProvider != null ? executeCommandProvider.getCommands().contains(commandId) : false); + }).excludeInactive().collectAll((w, ls) -> CompletableFuture.completedFuture(ls)) + .thenAccept(commandHandlers::addAll).get(100, TimeUnit.MILLISECONDS); if (commandHandlers.size() == 1) { LanguageServer handler = commandHandlers.get(0); diff --git a/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/OpenUriHandler.java b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/OpenUriHandler.java index d39417620d..79aec7675a 100644 --- a/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/OpenUriHandler.java +++ b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/OpenUriHandler.java @@ -51,10 +51,7 @@ public class OpenUriHandler extends LSPCommandHandler { @Override public Object execute(ExecutionEvent event, Command command, IPath path) throws ExecutionException { - String uri = (String) command.getArguments().get(0); - if (uri == null) { - return null; - } + String uri = ArgumentsUtils.getArgAt(command, 0, String.class); try { if (uri.startsWith("jar:")) { var url = URI.create(uri).toURL(); diff --git a/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/ShowReferencesHandler.java b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/ShowReferencesHandler.java index 8c40adefc1..94bae8bf98 100644 --- a/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/ShowReferencesHandler.java +++ b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/ShowReferencesHandler.java @@ -31,8 +31,6 @@ import org.eclipse.ui.ide.IDE; import org.eclipse.ui.texteditor.ITextEditor; -import com.google.gson.Gson; - /** * Handles the {@code xml.show.references} command sent by LemMinX via a code * lens, triggering a "Find References" search at the given position in the @@ -55,12 +53,10 @@ public class ShowReferencesHandler extends LSPCommandHandler { private static final String FIND_REFERENCES_COMMAND = "org.eclipse.ui.genericeditor.findReferences"; - private static final Gson GSON = new Gson(); - @Override public Object execute(ExecutionEvent event, Command command, IPath path) throws ExecutionException { - String uri = (String) command.getArguments().get(0); - Position position = GSON.fromJson(GSON.toJsonTree(command.getArguments().get(1)), Position.class); + String uri = ArgumentsUtils.getArgAt(command, 0, String.class); + Position position = ArgumentsUtils.getArgAt(command, 1, Position.class); try { IFileStore fileStore = EFS.getStore(URI.create(uri)); PlatformUI.getWorkbench().getDisplay().asyncExec(() -> { diff --git a/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/UpdateConfigurationHandler.java b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/UpdateConfigurationHandler.java new file mode 100644 index 0000000000..247a5d58cc --- /dev/null +++ b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/UpdateConfigurationHandler.java @@ -0,0 +1,127 @@ +/******************************************************************************* + * Copyright (c) 2026 Red Hat Inc. and others. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Angelo ZERR (Red Hat Inc.) - initial implementation + *******************************************************************************/ +package org.eclipse.wildwebdeveloper.xml.internal.commands; + +import java.io.File; +import java.net.URI; +import java.util.Set; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.runtime.ILog; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.lsp4e.command.LSPCommandHandler; +import org.eclipse.lsp4j.Command; +import org.eclipse.wildwebdeveloper.xml.internal.Activator; +import org.eclipse.wildwebdeveloper.xml.internal.ui.preferences.XMLCatalogs; + +/** + * Handles the {@code xml.update.configuration} command sent by LemMinX when the + * user clicks the "Register Catalog" or "Unregister Catalog" code lens on an + * XML catalog file. + * + *

+ * The command carries a {@code ConfigurationItemEdit} argument specifying: + *

    + *
  • {@code section} — the setting to update (e.g. + * {@code "xml.catalogs"})
  • + *
  • {@code value} — the catalog file URI to add or remove
  • + *
  • {@code editType} — {@code "Add"} or {@code "Delete"}
  • + *
+ * Currently only {@code "xml.catalogs"} is supported. + *

+ * + * @author Angelo ZERR + */ +public class UpdateConfigurationHandler extends LSPCommandHandler { + + private static final String SECTION_XML_CATALOGS = "xml.catalogs"; + + /** + * Configuration item edit type. Represents a configuration edit operation + * Maps to ordinal values (Add=0, Delete=1) for Gson deserialization + */ + private static enum ConfigurationItemEditType { + // Add a new catalog URI to the configuration + Add(0), + + // Delete a catalog URI from the configuration + Delete(1); + + private final int value; + + ConfigurationItemEditType(final int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + // IMPORTANT: This method is used by LSP4J EnumTypeAdapter for deserialization. + // Do not remove without updating the LSP configuration. + public static ConfigurationItemEditType forValue(final int value) { + ConfigurationItemEditType[] allValues = ConfigurationItemEditType.values(); + if (value < 0 || value >= allValues.length) { + throw new IllegalArgumentException("Illegal enum value: " + value); + } + return allValues[value]; + } + } + + // Record holding a configuration edit request: section to edit, value to add/delete, and edit type (Add/Delete) + private static record ConfigurationItemEdit(String section, String value, ConfigurationItemEditType editType) { + } + + @Override + public Object execute(ExecutionEvent event, Command command, IPath path) throws ExecutionException { + // Extract configuration edit request from LSP command arguments + ConfigurationItemEdit edit = ArgumentsUtils.getArgAt(command, 0, ConfigurationItemEdit.class); + if (edit == null) { + return null; + } + String section = edit.section(); + String value = edit.value(); + ConfigurationItemEditType editType = edit.editType(); + + // Validate required fields before proceeding + if (section == null || value == null || editType == null) { + return null; + } + // Currently only handle xml.catalogs configuration; other sections can be added here + if (SECTION_XML_CATALOGS.equals(section)) { + updateCatalogs(value, editType); + } + return null; + } + + private static void updateCatalogs(String catalogUri, ConfigurationItemEditType editType) { + try { + // Convert URI string to File object for preference store + File catalogFile = new File(URI.create(catalogUri)); + IPreferenceStore store = Activator.getDefault().getPreferenceStore(); + Set catalogs = XMLCatalogs.getUserCatalogs(store); + + // Add or remove catalog from user preferences based on edit type + if (editType == ConfigurationItemEditType.Add) { + catalogs.add(catalogFile); + } else if (editType == ConfigurationItemEditType.Delete) { + catalogs.remove(catalogFile); + } + // Persist the updated set of catalogs back to preference store + XMLCatalogs.storeUserCatalogs(store, catalogs); + } catch (Exception e) { + ILog.get().error("Failed to update XML catalog configuration for URI: " + catalogUri, e); + } + } +}