From 1e681fd9c68a42846e87a3d086257e8724657be2 Mon Sep 17 00:00:00 2001 From: azerr Date: Thu, 26 Nov 2026 03:07:31 +0100 Subject: [PATCH] Implement xml.open.uri and xml.show.references LSP commands Adds OpenUriHandler and ShowReferencesHandler to handle the custom LemMinX LSP commands, registers them in plugin.xml and XMLLanguageServer, and fixes a missing java.net.URI import in SelectionUtils. The jar: branch in ShowReferencesHandler is removed: the "N references" lens always carries the URI of the document the lens is attached to, and documents opened from a JAR (JarEditorInput) deliberately do not implement IURIEditorInput so LemMinX never attaches a language server to them, making a jar: URI in this handler unreachable. openJarEditor() is made private again since it is no longer called from outside OpenUriHandler. Signed-off-by: azerr --- .../META-INF/MANIFEST.MF | 3 +- org.eclipse.wildwebdeveloper.xml/plugin.xml | 8 + .../xml/internal/XMLLanguageServer.java | 24 +- .../xml/internal/commands/OpenUriHandler.java | 212 ++++++++++++++++++ .../commands/ShowReferencesHandler.java | 94 ++++++++ .../debug/SelectionUtils.java | 10 +- 6 files changed, 345 insertions(+), 6 deletions(-) create mode 100644 org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/OpenUriHandler.java create mode 100644 org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/ShowReferencesHandler.java diff --git a/org.eclipse.wildwebdeveloper.xml/META-INF/MANIFEST.MF b/org.eclipse.wildwebdeveloper.xml/META-INF/MANIFEST.MF index 44e65dc777..7b8bbe2a86 100644 --- a/org.eclipse.wildwebdeveloper.xml/META-INF/MANIFEST.MF +++ b/org.eclipse.wildwebdeveloper.xml/META-INF/MANIFEST.MF @@ -25,7 +25,8 @@ Require-Bundle: org.eclipse.tm4e.registry;bundle-version="0.3.0", org.eclipse.jface.text;bundle-version="3.20.100", com.google.gson, org.eclipse.tm4e.language_pack, - org.eclipse.lemminx.uber-jar;bundle-version="[0.29.0,1.0.0)" + org.eclipse.lemminx.uber-jar;bundle-version="[0.29.0,1.0.0)", + org.eclipse.ui.workbench.texteditor;bundle-version="3.20.200" Bundle-ActivationPolicy: lazy Bundle-Activator: org.eclipse.wildwebdeveloper.xml.internal.Activator Export-Package: org.eclipse.wildwebdeveloper.xml;x-friends:="org.eclipse.m2e.editor.lemminx", diff --git a/org.eclipse.wildwebdeveloper.xml/plugin.xml b/org.eclipse.wildwebdeveloper.xml/plugin.xml index ae6aa85e83..748d6a3344 100644 --- a/org.eclipse.wildwebdeveloper.xml/plugin.xml +++ b/org.eclipse.wildwebdeveloper.xml/plugin.xml @@ -230,5 +230,13 @@ class="org.eclipse.wildwebdeveloper.xml.internal.commands.AssociateGrammarHandler" commandId="xml.open.binding.wizard"> + + + + diff --git a/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/XMLLanguageServer.java b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/XMLLanguageServer.java index 5338e9e457..253f868b9c 100644 --- a/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/XMLLanguageServer.java +++ b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/XMLLanguageServer.java @@ -64,7 +64,23 @@ public class XMLLanguageServer extends ProcessStreamConnectionProvider { private static final String BINDING_WIZARD_SUPPORT_KEY = "bindingWizardSupport"; private static enum CodeLensKind { - association; + association, + references, + openUri("open.uri"); + + private final String name; + + private CodeLensKind() { + this(null); + } + + private CodeLensKind(String name) { + this.name = name; + } + + public String getName() { + return name != null ? name : name(); + } } private static final XMLExtensionRegistry extensionJarRegistry = new XMLExtensionRegistry(); @@ -208,12 +224,16 @@ public Object getInitializationOptions(URI rootUri) { private static Object createExtendedClientCapabilities() { Map extendedClientCapabilities = new HashMap<>(); + // XML Command for CodeLens support Map codeLens = new HashMap<>(); extendedClientCapabilities.put(CODE_LENS_KEY, codeLens); Map codeLensKind = new HashMap<>(); codeLens.put(CODE_LENS_KIND_KEY, codeLensKind); - List valueSet = Arrays.asList(CodeLensKind.association.name()); + List valueSet = Arrays.asList(CodeLensKind.association.getName(), // + CodeLensKind.references.getName(), // + CodeLensKind.openUri.getName()); codeLensKind.put(VALUE_SET_KEY, valueSet); + // Binding wizard support extendedClientCapabilities.put(BINDING_WIZARD_SUPPORT_KEY, Boolean.TRUE); return extendedClientCapabilities; } 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 new file mode 100644 index 0000000000..d39417620d --- /dev/null +++ b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/OpenUriHandler.java @@ -0,0 +1,212 @@ +/******************************************************************************* + * 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.ByteArrayInputStream; +import java.io.InputStream; +import java.net.URI; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.resources.IStorage; +import org.eclipse.core.runtime.ILog; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.lsp4e.command.LSPCommandHandler; +import org.eclipse.lsp4j.Command; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IPersistableElement; +import org.eclipse.ui.IStorageEditorInput; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.ide.IDE; + +/** + * Handles the {@code xml.open.uri} command sent by LemMinX via a code lens, + * opening the referenced grammar file in the editor. + * + *

+ * Supports two URI schemes: + *

    + *
  • {@code file:} — opened directly via EFS (e.g. cached XSD on disk)
  • + *
  • {@code jar:} — content read from the JAR entry and opened read-only via + * {@link IStorageEditorInput} (e.g. embedded xml.xsd inside a plugin JAR)
  • + *
+ * + * @author Angelo ZERR + */ +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; + } + try { + if (uri.startsWith("jar:")) { + var url = URI.create(uri).toURL(); + String entryPath = url.getPath(); + String entryName = entryPath.substring(entryPath.lastIndexOf('/') + 1); + // Read JAR bytes in a background Job to avoid blocking the workbench thread. + Job.create("Opening " + entryName, _monitor -> { + try { + byte[] content; + try (InputStream in = url.openStream()) { + content = in.readAllBytes(); + } + PlatformUI.getWorkbench().getDisplay().asyncExec(() -> { + try { + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow() + .getActivePage(); + openJarEditor(entryName, content, uri, page); + } catch (Exception e) { + ILog.get().error("Error opening URI: " + uri, e); + } + }); + } catch (Exception e) { + ILog.get().error("Error opening URI: " + uri, e); + } + }).schedule(); + } else if (uri.startsWith("http:") || uri.startsWith("https:")) { + // Remote schemas are not cached when downloadExternalResources is disabled; + // EFS cannot open them, so open in the browser instead. + PlatformUI.getWorkbench().getDisplay().asyncExec(() -> { + try { + PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser() + .openURL(URI.create(uri).toURL()); + } catch (Exception e) { + ILog.get().error("Error opening URI in browser: " + uri, e); + } + }); + } else { + IFileStore fileStore = EFS.getStore(URI.create(uri)); + PlatformUI.getWorkbench().getDisplay().asyncExec(() -> { + try { + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + IDE.openEditorOnFileStore(page, fileStore); + } catch (Exception e) { + ILog.get().error("Error opening URI: " + uri, e); + } + }); + } + } catch (Exception e) { + ILog.get().error("Error opening URI: " + uri, e); + } + return null; + } + + private static IEditorPart openJarEditor(String entryName, byte[] content, String uri, IWorkbenchPage page) + throws Exception { + IStorage storage = new IStorage() { + @Override + public InputStream getContents() { + return new ByteArrayInputStream(content); + } + + @Override + public IPath getFullPath() { + return null; + } + + @Override + public String getName() { + return entryName; + } + + @Override + public boolean isReadOnly() { + return true; + } + + @Override + public T getAdapter(Class adapter) { + return null; + } + }; + String editorId = IDE.getEditorDescriptor(entryName, true, false).getId(); + return page.openEditor(new JarEditorInput(storage, entryName, uri), editorId); + } + + /** + * Editor input for JAR entries, opened read-only via + * {@link IStorageEditorInput}. + *

+ * Note: {@code jar:} URIs are intentionally not exposed via + * {@code IURIEditorInput}. Eclipse EFS has no file-system provider for the + * {@code jar:} scheme, so LSP4E would fail (e.g. {@code EFS.getStore(jar:...)} + * in its outline adapter) if it tried to treat this editor input as a + * URI-backed resource. + *

+ */ + static final class JarEditorInput implements IStorageEditorInput { + + private final IStorage storage; + private final String name; + private final URI uri; + + JarEditorInput(IStorage storage, String name, String rawUri) { + this.storage = storage; + this.name = name; + this.uri = URI.create(rawUri); + } + + @Override + public IStorage getStorage() { + return storage; + } + + @Override + public boolean exists() { + return true; + } + + @Override + public ImageDescriptor getImageDescriptor() { + return null; + } + + @Override + public String getName() { + return name; + } + + @Override + public IPersistableElement getPersistable() { + return null; + } + + @Override + public String getToolTipText() { + return uri.toString(); + } + + @Override + public T getAdapter(Class adapter) { + return null; + } + + @Override + public boolean equals(Object obj) { + return obj instanceof JarEditorInput other && uri.equals(other.uri); + } + + @Override + public int hashCode() { + return uri.hashCode(); + } + } + +} 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 new file mode 100644 index 0000000000..8c40adefc1 --- /dev/null +++ b/org.eclipse.wildwebdeveloper.xml/src/org/eclipse/wildwebdeveloper/xml/internal/commands/ShowReferencesHandler.java @@ -0,0 +1,94 @@ +/******************************************************************************* + * 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.net.URI; + +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.runtime.ILog; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jface.text.IDocument; +import org.eclipse.lsp4e.LSPEclipseUtils; +import org.eclipse.lsp4e.command.LSPCommandHandler; +import org.eclipse.lsp4j.Command; +import org.eclipse.lsp4j.Position; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.handlers.IHandlerService; +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 + * given file. + * + *
+ * "command": {
+ *   "title": "1 reference",
+ *   "command": "xml.show.references",
+ *   "arguments": [
+ *     "file:///c%3A/Users/AngeloZerr/git/vscode-xml/foo.xsd",
+ *     { "line": 3, "character": 20 }
+ *   ]
+ * }
+ * 
+ * + * @author Angelo ZERR + */ +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); + try { + IFileStore fileStore = EFS.getStore(URI.create(uri)); + PlatformUI.getWorkbench().getDisplay().asyncExec(() -> { + try { + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + // openEditorOnFileStore returns existing editor if already open. + IEditorPart editor = IDE.openEditorOnFileStore(page, fileStore); + revealAndFindReferences(editor, position); + } catch (Exception e) { + ILog.get().error("Error showing references for URI: " + uri, e); + } + }); + } catch (Exception e) { + ILog.get().error("Error showing references for URI: " + uri, e); + } + return null; + } + + private static void revealAndFindReferences(IEditorPart editor, Position position) throws Exception { + if (editor instanceof ITextEditor textEditor) { + IDocument document = LSPEclipseUtils.getDocument(textEditor); + if (document != null) { + int offset = LSPEclipseUtils.toOffset(position, document); + textEditor.selectAndReveal(offset, 0); + IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class); + handlerService.executeCommand(FIND_REFERENCES_COMMAND, null); + } + } + } + +} diff --git a/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/debug/SelectionUtils.java b/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/debug/SelectionUtils.java index 96da4e7322..8159a2a2d4 100644 --- a/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/debug/SelectionUtils.java +++ b/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/debug/SelectionUtils.java @@ -10,6 +10,7 @@ package org.eclipse.wildwebdeveloper.debug; import java.io.File; +import java.net.URI; import java.util.function.Predicate; import org.eclipse.core.resources.IFile; @@ -117,9 +118,12 @@ public static File getFile(IEditorInput editorInput, Predicate condition) } } if (editorInput instanceof IURIEditorInput input) { - File file = new File(input.getURI()); - if (condition == null || condition.test(file)) { - return file; + URI uri = input.getURI(); + if ("file".equals(uri.getScheme())) { + File file = new File(uri); + if (condition == null || condition.test(file)) { + return file; + } } } // see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=550804