Skip to content
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
4 changes: 4 additions & 0 deletions org.eclipse.wildwebdeveloper.xml/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,9 @@
class="org.eclipse.wildwebdeveloper.xml.internal.commands.ShowReferencesHandler"
commandId="xml.show.references">
</handler>
<handler
class="org.eclipse.wildwebdeveloper.xml.internal.commands.UpdateConfigurationHandler"
commandId="xml.update.configuration">
</handler>
</extension>
</plugin>
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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

@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);
Comment thread
akurtakov marked this conversation as resolved.
Resolved
// Open the association grammar dialog.
AssociateGrammarDialog dialog = new AssociateGrammarDialog(HandlerUtil.getActiveShell(event), path);
if (dialog.open() == IDialogConstants.OK_ID) {
Expand Down Expand Up @@ -88,11 +88,11 @@
private static CompletableFuture<Object> executeServerCommand(String commandId, Object... params) throws Exception {
List<LanguageServer> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(() -> {
Expand Down
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));
Comment thread
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);
Comment thread
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);
}
}
}
Loading