Repository navigation
Implement xml.update.configuration command to register/unregister catalogs #2309
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
Merged
akurtakov
merged 1 commit into
eclipse-wildwebdeveloper:master
from
angelozerr:xml_register_catalog
Nov 30, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...bdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/ArgumentsUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <T> 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> T getArgAt(Command command, int index, Class<T> 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<Object> arguments, int index) { | ||
| if (arguments == null || index >= arguments.size()) { | ||
| return null; | ||
| } | ||
| return arguments.get(index); | ||
| } | ||
|
|
||
| private static <T> T toModel(Object object, Class<T> 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()); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...ml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/UpdateConfigurationHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
| * | ||
| * <p> | ||
| * The command carries a {@code ConfigurationItemEdit} argument specifying: | ||
| * <ul> | ||
| * <li>{@code section} — the setting to update (e.g. | ||
| * {@code "xml.catalogs"})</li> | ||
| * <li>{@code value} — the catalog file URI to add or remove</li> | ||
| * <li>{@code editType} — {@code "Add"} or {@code "Delete"}</li> | ||
| * </ul> | ||
| * Currently only {@code "xml.catalogs"} is supported. | ||
| * </p> | ||
| * | ||
| * @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)); | ||
|
akurtakov marked this conversation as resolved.
Resolved
|
||
| IPreferenceStore store = Activator.getDefault().getPreferenceStore(); | ||
| Set<File> 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); | ||
|
akurtakov marked this conversation as resolved.
Resolved
|
||
| } | ||
| // 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); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.