Skip to content

Commit

Permalink
'No definition found' when using 'Go to Definition' for types defined in
Browse files Browse the repository at this point in the history
imported XSD

Fixes redhat-developer/vscode-xml#632

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
azerr authored and fbricon committed Jan 4, 2022
1 parent f9a795d commit 29530bc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
Expand Up @@ -157,13 +157,20 @@ public static void searchXSTargetAttributes(DOMAttr originAttr, BindingType bind
if (matchAttr && StringUtils.isEmpty(originAttrValue)) {
return;
}

// <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
// xmlns:tns="http://camel.apache.org/schema/spring"
// targetNamespace="http://camel.apache.org/schema/spring" version="1.0">
String targetNamespace = documentElement.getAttribute(TARGET_NAMESPACE_ATTR); // ->
// http://camel.apache.org/schema/spring
String targetNamespacePrefix = documentElement.getPrefix(targetNamespace); // -> tns
String targetNamespacePrefix = null;
int index = originAttrValue.indexOf(':');
if (index != -1) {
// ex : jakartaee:applicationType
targetNamespacePrefix = originAttrValue.substring(0, index);
} else {
// <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
// xmlns:tns="http://camel.apache.org/schema/spring"
// targetNamespace="http://camel.apache.org/schema/spring" version="1.0">
String targetNamespace = documentElement.getAttribute(TARGET_NAMESPACE_ATTR); // ->
// http://camel.apache.org/schema/spring
targetNamespacePrefix = documentElement.getPrefix(targetNamespace); //
// -> tns
}

String originName = null;
if (matchAttr) {
Expand Down Expand Up @@ -197,7 +204,7 @@ private static void searchXSTargetAttributes(DOMAttr originAttr, BindingType bin
if (targetAttr != null && (!matchAttr || Objects.equal(originName, targetAttr.getValue()))) {
collector.accept(targetNamespacePrefix, targetAttr);
}
} else if (isXSInclude(targetElement)) {
} else if (isXSInclude(targetElement) || isXSImport(targetElement)) {
// collect xs:include XML Schema location
String schemaLocation = targetElement.getAttribute(SCHEMA_LOCATION_ATTR);
if (schemaLocation != null) {
Expand Down Expand Up @@ -498,7 +505,8 @@ public static DOMAttr findSchemaLocationAttrByURI(DOMDocument document, String g
DOMAttr schemaLocationAttr = XSDUtils.getSchemaLocation(xsdElement);
if (schemaLocationAttr != null) {
String attrValue = schemaLocationAttr.getValue();
if (grammarURI.equals(attrValue) || ((grammarURI.endsWith(attrValue) && grammarURI.equals(getResolvedLocation(document.getDocumentURI(), attrValue))))) {
if (grammarURI.equals(attrValue) || ((grammarURI.endsWith(attrValue)
&& grammarURI.equals(getResolvedLocation(document.getDocumentURI(), attrValue))))) {
return schemaLocationAttr;
}
}
Expand All @@ -508,7 +516,7 @@ public static DOMAttr findSchemaLocationAttrByURI(DOMDocument document, String g
}
return null;
}

/**
* Returns the expanded system location
*
Expand Down
Expand Up @@ -186,10 +186,29 @@ public void definitionWithXSInclude() throws BadLocationException {
" </xs:sequence>\r\n" + //
" </xs:complexType>";
String schemaCPath = Paths.get("src/test/resources/xsd/SchemaC.xsd").toUri().toString();
testDefinitionFor(xml, ll(schemaCPath, r(6,19,6,30), r(3, 18, 3, 29)));
testDefinitionFor(xml, ll(schemaCPath, r(6, 19, 6, 30), r(3, 18, 3, 29)));

}

@Test
public void definitionWithXSImport() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + //
"<xs:schema targetNamespace=\"SomeNamespace\"\r\n" + //
" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\r\n" + //
" xmlns:ct=\"ChildTypes\"\r\n" + //
" xmlns=\"SomeNamespace\"\r\n" + //
" elementFormDefault=\"unqualified\">\r\n" + //
" <xs:import schemaLocation=\"src/test/resources/xsd/Child.xsd\" namespace=\"ChildTypes\"/>\r\n"
+ " \r\n" + //
" <xs:complexType name=\"SpecialType\">\r\n" + " <xs:complexContent>\r\n" + //
" <xs:extension base=\"ct:Som|eGenericType\">\r\n" + " <xs:sequence>\r\n" + //
" <xs:element name=\"AdditionalField\" type=\"xs:string\" />\r\n" + //
" </xs:sequence>\r\n" + " </xs:extension>\r\n" + " </xs:complexContent>\r\n" + //
" </xs:complexType>\r\n" + "</xs:schema>";
String childPath = Paths.get("src/test/resources/xsd/Child.xsd").toUri().toString();
testDefinitionFor(xml, ll(childPath, r(10, 25, 10, 45), r(5, 23, 5, 40)));
}

private static void testDefinitionFor(String xml, LocationLink... expectedItems) throws BadLocationException {
XMLAssert.testDefinitionFor(xml, "test.xsd", expectedItems);
}
Expand Down
13 changes: 13 additions & 0 deletions org.eclipse.lemminx/src/test/resources/xsd/Child.xsd
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="ChildTypes"
xmlns="ChildTypes"
elementFormDefault="unqualified">
<xs:complexType name="SomeGenericType">
<xs:sequence>
<xs:element name="FirstName" type="xs:string" minOccurs="0"/>
<xs:element name="LastName" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>

</xs:schema>

0 comments on commit 29530bc

Please sign in to comment.