Skip to content

Commit

Permalink
Entity documentation has no value for entities declared with SYSTEM OR
Browse files Browse the repository at this point in the history
PUBLIC

Fixes #741

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed May 29, 2020
1 parent 291c6fa commit e79b08e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.xerces.impl.dtd.DTDGrammar;
import org.apache.xerces.impl.dtd.XMLDTDLoader;
import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.XMLResourceIdentifier;
import org.apache.xerces.xni.XMLString;
import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.grammars.Grammar;
Expand Down Expand Up @@ -101,12 +102,25 @@ private static class ScannedDTDEntityDecl extends DTDEntityDecl {

private final String entityName;
private final String value;

private final DTDDeclParameter nameParameter;

private final XMLResourceIdentifier identifier;

public ScannedDTDEntityDecl(String entityName, String value, ScannedEntity scannedEntity) {
this(entityName, value, null, scannedEntity);
}

public ScannedDTDEntityDecl(String name, XMLResourceIdentifier identifier, ScannedEntity scannedEntity) {
this(name, name, null, scannedEntity);
}

private ScannedDTDEntityDecl(String entityName, String value, XMLResourceIdentifier identifier,
ScannedEntity scannedEntity) {
super(-1, -1);
this.entityName = entityName;
this.value = value;
this.identifier = identifier;
this.nameParameter = createNameParameter(entityName, scannedEntity);
}

Expand All @@ -130,6 +144,16 @@ public String getNotationName() {
return value;
}

@Override
public String getSystemId() {
return identifier != null ? identifier.getLiteralSystemId() : null;
}

@Override
public String getPublicId() {
return identifier != null ? identifier.getPublicId() : null;
}

private static DTDDeclParameter createNameParameter(String name, ScannedEntity scannedEntity) {
String systemId = scannedEntity.entityLocation.getExpandedSystemId();
int lineNumber = scannedEntity.lineNumber - 1;
Expand Down Expand Up @@ -170,7 +194,8 @@ private static int getEntityNameStartColumnNumber(String entityName, ScannedEnti
char[] ch = scannedEntity.ch;
int wordIndex = entityName.length(); //
int startEntityNameIndex = -1;
// Loop for characters from the end of the entity (>) to search the entity name start offset
// Loop for characters from the end of the entity (>) to search the entity name
// start offset
// <!ENTITY name .....> |
for (int i = endEntityIndex; i >= 0; i--) {
char c = ch[i];
Expand Down Expand Up @@ -292,6 +317,17 @@ public void internalEntityDecl(String name, XMLString text, XMLString nonNormali
}
}

@Override
public void externalEntityDecl(String name, XMLResourceIdentifier identifier, Augmentations augs)
throws XNIException {
super.externalEntityDecl(name, identifier, augs);
try {
entities.add(new ScannedDTDEntityDecl(name, identifier, fEntityManager.getCurrentEntity()));
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while extracting information for the external entity '" + name + "'", e);
}
}

@Override
public void startContentModel(String elementName, Augmentations augs) throws XNIException {
if (hierarchiesMap == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.eclipse.lemminx.extensions.entities;

import org.eclipse.lemminx.dom.DTDEntityDecl;
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.MarkupKind;

Expand Down Expand Up @@ -64,6 +65,19 @@ public String getValue() {
private EntitiesDocumentationUtils() {
}

public static MarkupContent getDocumentation(String entityName, String entityValue, EntityOriginType type,
boolean markdown) {
return getDocumentation(entityName, entityValue, null, null, null, type, markdown);
}

public static MarkupContent getDocumentation(DTDEntityDecl entity, EntityOriginType type, boolean markdown) {
String systemID = entity.getSystemId();
String publicID = entity.getPublicId();
String targetURI = entity.getNameParameter().getTargetURI();
return getDocumentation(entity.getName(), entity.getNotationName(), systemID, publicID, targetURI,
EntityOriginType.LOCAL, markdown);
}

/**
* Returns the entity documentation.
*
Expand All @@ -74,8 +88,8 @@ private EntitiesDocumentationUtils() {
* false otherwise.
* @return the entity documentation.
*/
public static MarkupContent getDocumentation(String entityName, String entityValue, EntityOriginType type,
boolean markdown) {
public static MarkupContent getDocumentation(String entityName, String entityValue, String systemID,
String publicID, String targetURI, EntityOriginType type, boolean markdown) {
StringBuilder documentation = new StringBuilder();

// Title
Expand All @@ -88,15 +102,32 @@ public static MarkupContent getDocumentation(String entityName, String entityVal
documentation.append("**");
}

if (entityValue != null && !entityValue.isEmpty()) {
addParameter("Value", entityValue, documentation, markdown);
}
addParameter("Value", entityValue, documentation, markdown);
addParameter("Type", type.getLabel(), documentation, markdown);
addParameter("Public ID", publicID, documentation, markdown);
addParameter("System ID", systemID, documentation, markdown);
if (targetURI != null) {
documentation.append(System.lineSeparator());
if (markdown) {
documentation.append(" * ");
}
documentation.append("Source: ");
if (markdown) {
documentation.append("[");
documentation.append(getFileName(targetURI));
documentation.append("]");
documentation.append("(");
}
documentation.append(targetURI);
if (markdown) {
documentation.append(")");
}
}
return new MarkupContent(markdown ? MarkupKind.MARKDOWN : MarkupKind.PLAINTEXT, documentation.toString());
}

private static void addParameter(String name, String value, StringBuilder documentation, boolean markdown) {
if (value != null) {
if (value != null && !value.isEmpty()) {
documentation.append(System.lineSeparator());
if (markdown) {
documentation.append(" * ");
Expand All @@ -113,4 +144,20 @@ private static void addParameter(String name, String value, StringBuilder docume
}
}

/**
* Returns the file name from the given schema URI
*
* @param schemaURI the schema URI
* @return the file name from the given schema URI
*/
private static String getFileName(String schemaURI) {
int index = schemaURI.lastIndexOf('/');
if (index == -1) {
index = schemaURI.lastIndexOf('\\');
}
if (index == -1) {
return schemaURI;
}
return schemaURI.substring(index + 1, schemaURI.length());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMDocumentType;
import org.eclipse.lemminx.dom.DTDEntityDecl;
import org.eclipse.lemminx.extensions.contentmodel.model.CMDocument;
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager;
import org.eclipse.lemminx.extensions.entities.EntitiesDocumentationUtils;
Expand All @@ -30,6 +31,7 @@
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;
import org.eclipse.lsp4j.InsertTextFormat;
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.MarkupKind;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextEdit;
Expand Down Expand Up @@ -78,8 +80,9 @@ private static void collectLocalEntityProposals(DOMDocument document, Range enti
Entity entity = (Entity) entities.item(i);
if (entity.getNodeName() != null) {
// provide completion for the locally declared entity
fillCompletion(entity.getNodeName(), entity.getNotationName(), EntityOriginType.LOCAL, entityRange,
markdown, response);
MarkupContent documentation = EntitiesDocumentationUtils.getDocumentation((DTDEntityDecl) entity,
EntityOriginType.LOCAL, markdown);
fillCompletion(entity.getNodeName(), documentation, entityRange, response);
}
}
}
Expand All @@ -103,8 +106,9 @@ private static void collectExternalEntityProposals(DOMDocument document, Range e
for (Entity entity : entities) {
if (entity.getNodeName() != null) {
// provide completion for the external declared entity
fillCompletion(entity.getNodeName(), entity.getNotationName(), EntityOriginType.EXTERNAL,
entityRange, markdown, response);
MarkupContent documentation = EntitiesDocumentationUtils.getDocumentation((DTDEntityDecl) entity,
EntityOriginType.EXTERNAL, markdown);
fillCompletion(entity.getNodeName(), documentation, entityRange, response);
}
}
}
Expand All @@ -123,13 +127,14 @@ private static void collectExternalEntityProposals(DOMDocument document, Range e
private void collectPredefinedEntityProposals(Range entityRange, boolean markdown, ICompletionResponse response) {
PredefinedEntity[] entities = PredefinedEntity.values();
for (PredefinedEntity entity : entities) {
fillCompletion(entity.getName(), entity.getValue(), EntityOriginType.PREDEFINED, entityRange, markdown,
response);
MarkupContent documentation = EntitiesDocumentationUtils.getDocumentation(entity.getName(),
entity.getValue(), EntityOriginType.PREDEFINED, markdown);
fillCompletion(entity.getName(), documentation, entityRange, response);
}
}

private static void fillCompletion(String name, String entityValue, EntityOriginType type, Range entityRange,
boolean markdown, ICompletionResponse response) {
private static void fillCompletion(String name, MarkupContent documentation, Range entityRange,
ICompletionResponse response) {
String entityName = "&" + name + ";";
CompletionItem item = new CompletionItem();
item.setLabel(entityName);
Expand All @@ -138,7 +143,7 @@ private static void fillCompletion(String name, String entityValue, EntityOrigin
String insertText = entityName;
item.setFilterText(insertText);
item.setTextEdit(new TextEdit(entityRange, insertText));
item.setDocumentation(EntitiesDocumentationUtils.getDocumentation(name, entityValue, type, markdown));
item.setDocumentation(documentation);
response.addCompletionItem(item);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ private static MarkupContent searchInLocalEntities(String entityName, Range enti
DTDEntityDecl entity = (DTDEntityDecl) entities.item(i);
if (entityName.equals(entity.getName())) {
boolean markdown = request.canSupportMarkupKind(MarkupKind.MARKDOWN);
return EntitiesDocumentationUtils.getDocumentation(entity.getName(), entity.getNotationName(),
EntityOriginType.LOCAL, markdown);
return EntitiesDocumentationUtils.getDocumentation(entity, EntityOriginType.LOCAL, markdown);
}
}
return null;
Expand All @@ -164,8 +163,7 @@ private static MarkupContent searchInExternalEntities(String entityName, Range e
DTDEntityDecl entity = (DTDEntityDecl) ent;
if (entityName.equals(entity.getName())) {
boolean markdown = request.canSupportMarkupKind(MarkupKind.MARKDOWN);
return EntitiesDocumentationUtils.getDocumentation(entity.getName(), entity.getNotationName(),
EntityOriginType.EXTERNAL, markdown);
return EntitiesDocumentationUtils.getDocumentation(entity, EntityOriginType.LOCAL, markdown);
}
}
}
Expand Down

0 comments on commit e79b08e

Please sign in to comment.