Skip to content

Commit

Permalink
Autocompletion of attribute values won't display documentation
Browse files Browse the repository at this point in the history
Fixes redhat-developer/vscode-xml#736

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Jul 26, 2022
1 parent 18e3bdf commit 7f9514a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,19 @@ public static List<String> getDocumentation(XSObjectList annotations, String val
List<String> result = new ArrayList<>();
for (Object object : annotations) {
XSAnnotation annotation = getXSAnnotation((XSObject) object, value);
XSDAnnotationModel annotationModel = XSDAnnotationModel.load(annotation);
if (annotationModel != null) {
List<String> documentation = annotationModel.getDocumentation();
if (documentation.size() > 0) {
result.addAll(documentation);
} else {
String annotationString = annotation.getAnnotationString();
if (!isEmpty(annotationString)) {
String docFromPattern = getDocumentation(annotationString);
if (!isEmpty(docFromPattern)) {
result.add(docFromPattern);
if (annotation != null) {
XSDAnnotationModel annotationModel = XSDAnnotationModel.load(annotation);
if (annotationModel != null) {
List<String> documentation = annotationModel.getDocumentation();
if (documentation.size() > 0) {
result.addAll(documentation);
} else {
String annotationString = annotation.getAnnotationString();
if (!isEmpty(annotationString)) {
String docFromPattern = getDocumentation(annotationString);
if (!isEmpty(docFromPattern)) {
result.add(docFromPattern);
}
}
}
}
Expand Down Expand Up @@ -138,9 +140,11 @@ public static List<String> getAppInfo(XSObjectList annotations, String value) {
List<String> appinfo = new ArrayList<>();
for (Object object : annotations) {
XSAnnotation annotation = getXSAnnotation((XSObject) object, value);
XSDAnnotationModel annotationModel = XSDAnnotationModel.load(annotation);
if (annotationModel != null) {
appinfo.addAll(annotationModel.getAppInfo());
if (annotation != null) {
XSDAnnotationModel annotationModel = XSDAnnotationModel.load(annotation);
if (annotationModel != null) {
appinfo.addAll(annotationModel.getAppInfo());
}
}
}
return appinfo;
Expand All @@ -159,8 +163,8 @@ public static String getPrefix(XSObjectList annotations) {
/**
* Returns the prefix (ie. xs) from the provided collection of annotations
*
* Prerequisite: <code>value</code> should be provided if <code>annotations</code>
* is a collection of attribute value annotations
* Prerequisite: <code>value</code> should be provided if
* <code>annotations</code> is a collection of attribute value annotations
*
* @param annotations the collection of annotations
* @param value the attribute value
Expand All @@ -172,16 +176,17 @@ public static String getPrefix(XSObjectList annotations, String value) {
}
for (Object object : annotations) {
XSAnnotation annotation = getXSAnnotation((XSObject) object, value);
String content = annotation.getAnnotationString();
int index = content.indexOf(":");
if (index != -1) {
return content.substring(1, index);
if (annotation != null) {
String content = annotation.getAnnotationString();
int index = content.indexOf(":");
if (index != -1) {
return content.substring(1, index);
}
}
}
return "";
}


public static XSDAnnotationModel load(XSAnnotation annotation) {
try {
SAXParserFactory factory = DOMUtils.newSAXParserFactory();
Expand All @@ -193,39 +198,39 @@ public static XSDAnnotationModel load(XSAnnotation annotation) {
return null;
}
}

/**
* Returns the <code>XSAnnotation</code> instance for the
* provided <code>annotation</code>
* Returns the <code>XSAnnotation</code> instance for the provided
* <code>annotation</code>
*
* If <code>value</code> is provided, the <code>XSAnnotation</code> for
* an attribute value will be searched for
* If <code>value</code> is provided, the <code>XSAnnotation</code> for an
* attribute value will be searched for
*
* If not provided, the <code>XSAnnotation</code> for
* an attribute or element will be searched for
* If not provided, the <code>XSAnnotation</code> for an attribute or element
* will be searched for
*
* @param annotation the annotation object
* @param annotation the annotation object
* @param value the attribute value
* @return the <code>XSAnnotation</code> instance for the
* provided <code>annotation</code>
* @return the <code>XSAnnotation</code> instance for the provided
* <code>annotation</code>
*/
private static XSAnnotation getXSAnnotation(XSObject annotation, String value) {
if (annotation instanceof XSMultiValueFacet && value != null) {
XSMultiValueFacet multiValueFacet = (XSMultiValueFacet) annotation;
ObjectList enumerationValues = multiValueFacet.getEnumerationValues();
XSObjectList annotationValues = multiValueFacet.getAnnotations();
for (int i = 0; i < enumerationValues.getLength(); i++) {
Object enumValue = enumerationValues.get(i);
ObjectList enumerationValues = multiValueFacet.getEnumerationValues();
XSObjectList annotationValues = multiValueFacet.getAnnotations();
for (int i = 0; i < enumerationValues.getLength(); i++) {
Object enumValue = enumerationValues.get(i);

// Assuming always ValidatedInfo
String enumString = ((ValidatedInfo) enumValue).stringValue();
// Assuming always ValidatedInfo
String enumString = ((ValidatedInfo) enumValue).stringValue();

if (value.equals(enumString)) {
return (XSAnnotation) annotationValues.get(i);
}
if (value.equals(enumString)) {
return (XSAnnotation) annotationValues.get(i);
}
}
} else if (annotation instanceof XSAnnotation) {
return (XSAnnotation) annotation;
return (XSAnnotation) annotation;
}
return null;
}
Expand All @@ -247,7 +252,8 @@ public XSDAnnotationModel getModel() {
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
super.startElement(uri, localName, qName, attributes);
if (qName.endsWith(DOCUMENTATION_ELEMENT) || qName.endsWith(APPINFO_ELEMENT)) {
current = new StringBuilder();
Expand Down Expand Up @@ -285,7 +291,7 @@ private static void addIfNonEmptyString(List<String> list, String str) {

public static String getDocumentation(String xml) {
Matcher m = DOCUMENTATION_CONTENT.matcher(xml);
if(m.find()) {
if (m.find()) {
return m.group(1);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,34 @@ public void hoverTextWithUnion3() throws BadLocationException, MalformedURIExcep
r(2, 52, 3, 9));
}

@Test
public void attrValueEnumWithDoc() throws BadLocationException, MalformedURIException {
String schemaURI = getXMLSchemaFileURI("attr-enum.xsd");
String xml = "<root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \r\n" + //
" xsi:noNamespaceSchemaLocation=\"xsd/attr-enum.xsd\"\r\n" + //
" attribute=\"va|lue1\" />";
XMLAssert.assertHover(new XMLLanguageService(), xml, null, "src/test/resources/attr-enum.xml", //
"Documentation for attribute value1" + //
System.lineSeparator() + //
System.lineSeparator() + //
"Source: [attr-enum.xsd](" + schemaURI + ")",
r(2, 16, 2, 24));
}

@Test
public void attrValueEnumWithNoDoc() throws BadLocationException, MalformedURIException {
String schemaURI = getXMLSchemaFileURI("attr-enum.xsd");
String xml = "<root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \r\n" + //
" xsi:noNamespaceSchemaLocation=\"xsd/attr-enum.xsd\"\r\n" + //
" attribute=\"va|lue2\" />";
XMLAssert.assertHover(new XMLLanguageService(), xml, null, "src/test/resources/attr-enum.xml", //
"Documentation for attribute" + //
System.lineSeparator() + //
System.lineSeparator() + //
"Source: [attr-enum.xsd](" + schemaURI + ")",
r(2, 16, 2, 24));
}

@Test
public void hoverWithNullParentNode() throws Exception {
ContentModelHoverParticipant hoverParticipant = new ContentModelHoverParticipant();
Expand Down
25 changes: 25 additions & 0 deletions org.eclipse.lemminx/src/test/resources/xsd/attr-enum.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:annotation>
<xs:documentation>Documentation for root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:attribute name="attribute">
<xs:annotation>
<xs:documentation>Documentation for attribute</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="value1">
<xs:annotation>
<xs:documentation>Documentation for attribute value1</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="value2" /> <!-- value2 has no documentation. -->
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>

0 comments on commit 7f9514a

Please sign in to comment.