-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
textDocument/typeDefinition
from XML to XMLSchema/DTD
Fix #371 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
0c018a2
commit 24a79dc
Showing
13 changed files
with
307 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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
87 changes: 87 additions & 0 deletions
87
...e/lsp4xml/extensions/contentmodel/participants/ContentModelTypeDefinitionParticipant.java
This file contains 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,87 @@ | ||
package org.eclipse.lsp4xml.extensions.contentmodel.participants; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.charset.Charset; | ||
import java.util.List; | ||
|
||
import org.eclipse.lsp4j.LocationLink; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
import org.eclipse.lsp4xml.dom.DOMAttr; | ||
import org.eclipse.lsp4xml.dom.DOMDocument; | ||
import org.eclipse.lsp4xml.dom.DOMElement; | ||
import org.eclipse.lsp4xml.dom.DOMNode; | ||
import org.eclipse.lsp4xml.dom.DOMParser; | ||
import org.eclipse.lsp4xml.extensions.contentmodel.model.CMDocument; | ||
import org.eclipse.lsp4xml.extensions.contentmodel.model.CMElementDeclaration; | ||
import org.eclipse.lsp4xml.extensions.contentmodel.model.ContentModelManager; | ||
import org.eclipse.lsp4xml.extensions.xsd.utils.XSDUtils; | ||
import org.eclipse.lsp4xml.services.extensions.ITypeDefinitionParticipant; | ||
import org.eclipse.lsp4xml.services.extensions.ITypeDefinitionRequest; | ||
import org.eclipse.lsp4xml.uriresolver.URIResolverExtensionManager; | ||
import org.eclipse.lsp4xml.utils.URIUtils; | ||
import org.eclipse.lsp4xml.utils.XMLPositionUtility; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
|
||
import com.google.common.io.Files; | ||
|
||
public class ContentModelTypeDefinitionParticipant implements ITypeDefinitionParticipant { | ||
|
||
@Override | ||
public void findTypeDefinition(ITypeDefinitionRequest request, List<LocationLink> locations, | ||
CancelChecker cancelChecker) { | ||
ContentModelManager contentModelManager = request.getComponent(ContentModelManager.class); | ||
DOMNode node = request.getNode(); | ||
if (node == null) { | ||
return; | ||
} | ||
if (node.isElement()) { | ||
DOMElement element = (DOMElement) node; | ||
CMDocument cmDocument = contentModelManager.findCMDocument(element.getOwnerDocument(), | ||
element.getNamespaceURI()); | ||
if (cmDocument != null) { | ||
CMElementDeclaration elementDeclaration = cmDocument.findCMElement(element, element.getNamespaceURI()); | ||
if (elementDeclaration != null) { | ||
String documentURI = cmDocument.getURI(); | ||
if (URIUtils.isFileResource(documentURI)) { | ||
URIResolverExtensionManager resolverExtensionManager = request | ||
.getComponent(URIResolverExtensionManager.class); | ||
try { | ||
|
||
DOMDocument schema = DOMParser.getInstance().parse( | ||
Files.asCharSource(new File(new URI(documentURI).getPath()), Charset.defaultCharset()).read(), | ||
documentURI, resolverExtensionManager); | ||
NodeList children = schema.getDocumentElement().getChildNodes(); | ||
for (int i = 0; i < children.getLength(); i++) { | ||
Node n = children.item(i); | ||
if (n.getNodeType() == Node.ELEMENT_NODE) { | ||
Element elt = (Element) n; | ||
if (XSDUtils.isXSElement(elt)) { | ||
if (element.getLocalName().equals(elt.getAttribute("name"))) { | ||
DOMAttr targetAttr = (DOMAttr) elt.getAttributeNode("name"); | ||
LocationLink location = XMLPositionUtility.createLocationLink(element, | ||
targetAttr.getNodeAttrValue()); | ||
locations.add(location); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} catch (Exception e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
} | ||
System.err.println(elementDeclaration); | ||
} | ||
} | ||
|
||
} else if (node.isAttribute()) { | ||
DOMAttr attr = (DOMAttr) node; | ||
} | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/TypeDefinitionRequest.java
This file contains 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,50 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lsp4xml.services; | ||
|
||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4xml.commons.BadLocationException; | ||
import org.eclipse.lsp4xml.dom.DOMAttr; | ||
import org.eclipse.lsp4xml.dom.DOMDocument; | ||
import org.eclipse.lsp4xml.dom.DOMNode; | ||
import org.eclipse.lsp4xml.services.extensions.ITypeDefinitionRequest; | ||
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry; | ||
|
||
/** | ||
* Type definition request implementation. | ||
* | ||
*/ | ||
class TypeDefinitionRequest extends AbstractPositionRequest implements ITypeDefinitionRequest { | ||
|
||
private final XMLExtensionsRegistry extensionsRegistry; | ||
|
||
public TypeDefinitionRequest(DOMDocument xmlDocument, Position position, XMLExtensionsRegistry extensionsRegistry) | ||
throws BadLocationException { | ||
super(xmlDocument, position); | ||
this.extensionsRegistry = extensionsRegistry; | ||
} | ||
|
||
@Override | ||
protected DOMNode findNodeAt(DOMDocument xmlDocument, int offset) { | ||
DOMNode node = xmlDocument.findNodeAt(offset); | ||
if (node != null && node.isElement()) { | ||
DOMAttr attr = xmlDocument.findAttrAt(node, offset); | ||
if (attr != null) { | ||
return attr; | ||
} | ||
} | ||
return node; | ||
} | ||
|
||
@Override | ||
public <T> T getComponent(Class clazz) { | ||
return extensionsRegistry.getComponent(clazz); | ||
} | ||
} |
This file contains 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
57 changes: 57 additions & 0 deletions
57
org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XMLTypeDefinition.java
This file contains 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,57 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lsp4xml.services; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import org.eclipse.lsp4j.LocationLink; | ||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
import org.eclipse.lsp4xml.commons.BadLocationException; | ||
import org.eclipse.lsp4xml.dom.DOMDocument; | ||
import org.eclipse.lsp4xml.services.extensions.ITypeDefinitionParticipant; | ||
import org.eclipse.lsp4xml.services.extensions.ITypeDefinitionRequest; | ||
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry; | ||
|
||
/** | ||
* XML type definition support. | ||
* | ||
*/ | ||
class XMLTypeDefinition { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(XMLTypeDefinition.class.getName()); | ||
|
||
private final XMLExtensionsRegistry extensionsRegistry; | ||
|
||
public XMLTypeDefinition(XMLExtensionsRegistry extensionsRegistry) { | ||
this.extensionsRegistry = extensionsRegistry; | ||
} | ||
|
||
public List<? extends LocationLink> findTypeDefinition(DOMDocument document, Position position, | ||
CancelChecker cancelChecker) { | ||
ITypeDefinitionRequest request = null; | ||
try { | ||
request = new TypeDefinitionRequest(document, position, extensionsRegistry); | ||
} catch (BadLocationException e) { | ||
LOGGER.log(Level.SEVERE, "Failed creating TypeDefinitionRequest", e); | ||
return Collections.emptyList(); | ||
} | ||
List<LocationLink> locations = new ArrayList<>(); | ||
for (ITypeDefinitionParticipant participant : extensionsRegistry.getTypeDefinitionParticipants()) { | ||
participant.findTypeDefinition(request, locations, cancelChecker); | ||
} | ||
return locations; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...xml/src/main/java/org/eclipse/lsp4xml/services/extensions/ITypeDefinitionParticipant.java
This file contains 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,33 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2019 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lsp4xml.services.extensions; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.lsp4j.LocationLink; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
|
||
/** | ||
* Type Definition participant API. | ||
* | ||
*/ | ||
public interface ITypeDefinitionParticipant { | ||
|
||
/** | ||
* Find type definition. | ||
* | ||
* @param document | ||
* @param position | ||
* @param locations | ||
* @param cancelChecker | ||
*/ | ||
void findTypeDefinition(ITypeDefinitionRequest request, List<LocationLink> locations, CancelChecker cancelChecker); | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...lsp4xml/src/main/java/org/eclipse/lsp4xml/services/extensions/ITypeDefinitionRequest.java
This file contains 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,5 @@ | ||
package org.eclipse.lsp4xml.services.extensions; | ||
|
||
public interface ITypeDefinitionRequest extends IPositionRequest { | ||
|
||
} |
Oops, something went wrong.