Skip to content

Commit

Permalink
No hover message if no docs
Browse files Browse the repository at this point in the history
If a declared element doesn't have associated `xs:documentation`
or `xs:appinfo`, then no hover information is displayed.
This also applies to the documentation displayed by the completion as
well.

Closes redhat-developer/vscode-xml#258

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 authored and angelozerr committed Jun 2, 2020
1 parent 2f94c30 commit 2cbdd5a
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.lemminx.services.extensions.ISharedSettingsRequest;
import org.eclipse.lemminx.settings.SharedSettings;
import org.eclipse.lemminx.utils.MarkupContentFactory;
import org.eclipse.lemminx.utils.StringUtils;
import org.eclipse.lemminx.utils.XMLBuilder;
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.MarkupKind;
Expand Down Expand Up @@ -226,12 +227,15 @@ public static String generateAttributeValue(String defaultValue, Collection<Stri
* @return
*/
public static String generateDocumentation(String documentation, String schemaURI, boolean html) {
StringBuilder doc = new StringBuilder(documentation != null ? documentation : "");
if (StringUtils.isBlank(documentation)) {
return null;
}

StringBuilder doc = new StringBuilder(documentation);

if (schemaURI != null) {
if (doc.length() != 0) {
doc.append(System.lineSeparator());
doc.append(System.lineSeparator());
}
doc.append(System.lineSeparator());
doc.append(System.lineSeparator());
if (html) {
doc.append("<p>");
}
Expand All @@ -247,7 +251,7 @@ public static String generateDocumentation(String documentation, String schemaUR
doc.append("</p>");
}
}
return doc.length() > 0 ? doc.toString() : null;
return doc.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,17 @@ private static String getFormatted(String prefix, String elementName, List<Strin
result.append(applyPrefix(prefix, elementName, html));
}
for (String doc: content) {
if (html) {
result.append("<p>");
}
result.append(doc);
if (html) {
result.append("</p>");
} else {
result.append(System.lineSeparator());
result.append(System.lineSeparator());
if (!StringUtils.isBlank(doc)) {
if (html) {
result.append("<p>");
}
result.append(doc);
if (html) {
result.append("</p>");
} else {
result.append(System.lineSeparator());
result.append(System.lineSeparator());
}
}
}
return result.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ public static boolean isWhitespace(String value) {
return isWhitespace(value, 0);
}

/**
* Checks if a string is null or consists of only whitespace characters.
*
* @param value The string to check
* @return <code>true</code> if any of the below hold, and false otherwise:
* <ul>
* <li>The String is <code>null</code></li>
* <li>The String is of length 0</li>
* <li>The String contains only whitespace characters</li>
* </ul>
*/
public static boolean isBlank(String value) {
return isEmpty(value) || isWhitespace(value);
}

/**
* Normalizes the whitespace characters of a given string and applies it to the
* given string builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,9 @@ public void issue214WithMarkdown() throws BadLocationException, MalformedURIExce
" |";
testCompletionMarkdownSupportFor(xml,
c("Annotation", te(6, 1, 6, 1, "<Annotation Term=\"\"></Annotation>"), "Annotation",
"Source: [edm.xsd](" + edmURI + ")", MarkupKind.MARKDOWN), //
null, null), //
c("edmx:Include", te(6, 1, 6, 1, "<edmx:Include Namespace=\"\"></edmx:Include>"), "edmx:Include",
"Source: [edmx.xsd](" + edmxURI + ")", MarkupKind.MARKDOWN), //
null, null), //
c("edmx:IncludeAnnotations", "<edmx:IncludeAnnotations TermNamespace=\"\" />"));

xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
Expand Down Expand Up @@ -850,11 +850,10 @@ public void xsAnyDuplicate() throws IOException, BadLocationException {
" | ";
// testCompletionFor checks the duplicate label
XMLAssert.testCompletionFor(xml, null, "src/test/resources/tns.xml", null,
c("Page", te(2, 1, 2, 1, "<Page></Page>"), "Page", "Source: tns.xsd", MarkupKind.PLAINTEXT),
c("Page", te(2, 1, 2, 1, "<Page></Page>"), "Page", null, null),
c("AbsoluteLayout", te(2, 1, 2, 1, "<AbsoluteLayout></AbsoluteLayout>"), "AbsoluteLayout",
"Source: tns.xsd", MarkupKind.PLAINTEXT),
c("DockLayout", te(2, 1, 2, 1, "<DockLayout></DockLayout>"), "DockLayout", "Source: tns.xsd",
MarkupKind.PLAINTEXT));
null, null),
c("DockLayout", te(2, 1, 2, 1, "<DockLayout></DockLayout>"), "DockLayout", null, null));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,30 @@ public void testHoverMultipleBothPlainText() throws BadLocationException, Malfor
assertElementMultipleBothHover(null, SchemaDocumentationType.none, false);
};

@Test
public void testHoverNoAnnotation() throws BadLocationException, MalformedURIException {
assertElementHoverNoAnnotation(SchemaDocumentationType.all, true);
assertElementHoverNoAnnotation(SchemaDocumentationType.appinfo, true);
assertElementHoverNoAnnotation(SchemaDocumentationType.documentation, true);
assertElementHoverNoAnnotation(SchemaDocumentationType.none, true);
assertElementHoverNoAnnotation(SchemaDocumentationType.all, false);
assertElementHoverNoAnnotation(SchemaDocumentationType.appinfo, false);
assertElementHoverNoAnnotation(SchemaDocumentationType.documentation, false);
assertElementHoverNoAnnotation(SchemaDocumentationType.none, false);
}

@Test
public void testHoverWhitespaceAnnotation() throws BadLocationException, MalformedURIException {
assertElementHoverWhitespaceAnnotation(SchemaDocumentationType.all, true);
assertElementHoverWhitespaceAnnotation(SchemaDocumentationType.appinfo, true);
assertElementHoverWhitespaceAnnotation(SchemaDocumentationType.documentation, true);
assertElementHoverWhitespaceAnnotation(SchemaDocumentationType.none, true);
assertElementHoverWhitespaceAnnotation(SchemaDocumentationType.all, false);
assertElementHoverWhitespaceAnnotation(SchemaDocumentationType.appinfo, false);
assertElementHoverWhitespaceAnnotation(SchemaDocumentationType.documentation, false);
assertElementHoverWhitespaceAnnotation(SchemaDocumentationType.none, false);
}

private void assertAttributeNameDocHover(String expected, SchemaDocumentationType docSource,
boolean markdownSupported) throws BadLocationException {
String xml =
Expand Down Expand Up @@ -301,17 +325,37 @@ private void assertElementMultipleBothHover(String expected, SchemaDocumentation
assertHover(xml, expected, docSource, markdownSupported);
}

private void assertElementHoverNoAnnotation(SchemaDocumentationType docSource, boolean markdownSupported) throws BadLocationException {
String xml =
"<root\n" + //
" xmlns=\"http://docAppinfo\"\n" + //
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + //
" xsi:schemaLocation=\"http://docAppinfo xsd/" + schemaName + "\"\n" + //
" <e|lementNoAnnotation></elementNoAnnotation>\n" + //
"</root>\n";
assertHover(xml, null, docSource, markdownSupported);
}

private void assertElementHoverWhitespaceAnnotation(SchemaDocumentationType docSource, boolean markdownSupported) throws BadLocationException {
String xml =
"<root\n" + //
" xmlns=\"http://docAppinfo\"\n" + //
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + //
" xsi:schemaLocation=\"http://docAppinfo xsd/" + schemaName + "\"\n" + //
" <e|lementWhitespaceAnnotation></elementWhitespaceAnnotation>\n" + //
"</root>\n";
assertHover(xml, null, docSource, markdownSupported);
}

private void assertHover(String xml, String expected, SchemaDocumentationType docSource, boolean markdownSupported) throws BadLocationException {
String currSource = markdownSupported ? source : plainTextSource;

if (expected != null) {
String currSource = markdownSupported ? source : plainTextSource;
StringBuilder content = new StringBuilder(expected);
content.append(System.lineSeparator());
content.append(System.lineSeparator());
content.append(currSource);
expected = content.toString();
} else {
expected = currSource;
}

XMLAssert.assertHover(new XMLLanguageService(), xml, null, schemaPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public void completionWithSourceDocumentation() throws BadLocationException {
"|";
testCompletionFor(xml,
c("xs:annotation", te(2, 0, 2, 0, "<xs:annotation></xs:annotation>"), "xs:annotation",
"Source: XMLSchema.xsd", MarkupKind.PLAINTEXT),
null, null),
c("xs:attribute", te(2, 0, 2, 0, "<xs:attribute name=\"\"></xs:attribute>"), "xs:attribute",
"Source: XMLSchema.xsd", MarkupKind.PLAINTEXT));
null, null));
}

@Test
Expand Down
16 changes: 16 additions & 0 deletions org.eclipse.lemminx/src/test/resources/xsd/docAppinfo.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@
<xs:appinfo>third element appinfo</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name='elementNoAnnotation'></xs:element>
<xs:element name='elementWhitespaceAnnotation'>
<xs:annotation>
<xs:documentation>




</xs:documentation>
<xs:appinfo>


</xs:appinfo>
<xs:appinfo> </xs:appinfo>
</xs:annotation>
</xs:element>
</xs:sequence>

<xs:attribute name='attributeNameOnlyDocumentation' type='attributeValue' use='required'>
Expand Down

0 comments on commit 2cbdd5a

Please sign in to comment.