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
+ *
+ * @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);
+ }
+ }
+}