From b27cdba6c5ff20029747aad2e5b82c96bfa13532 Mon Sep 17 00:00:00 2001 From: azerr Date: Fri, 29 May 2020 18:00:44 +0200 Subject: [PATCH] Entity documentation has no value for entities declared with SYSTEM OR PUBLIC Fixes #741 Signed-off-by: azerr --- .../dtd/contentmodel/CMDTDDocument.java | 38 +++++++++++- .../entities/EntitiesDocumentationUtils.java | 59 +++++++++++++++++-- .../EntitiesHoverParticipant.java | 6 +- 3 files changed, 92 insertions(+), 11 deletions(-) diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/dtd/contentmodel/CMDTDDocument.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/dtd/contentmodel/CMDTDDocument.java index 9da84dd67e..1b73ec022a 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/dtd/contentmodel/CMDTDDocument.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/dtd/contentmodel/CMDTDDocument.java @@ -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; @@ -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); } @@ -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; @@ -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 // | for (int i = endEntityIndex; i >= 0; i--) { char c = ch[i]; @@ -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) { diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/entities/EntitiesDocumentationUtils.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/entities/EntitiesDocumentationUtils.java index ba1d022db7..94a2c8c601 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/entities/EntitiesDocumentationUtils.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/entities/EntitiesDocumentationUtils.java @@ -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; @@ -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. * @@ -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 @@ -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(" * "); @@ -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()); + } } diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/entities/participants/EntitiesHoverParticipant.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/entities/participants/EntitiesHoverParticipant.java index 33fa67b11f..018e9a00df 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/entities/participants/EntitiesHoverParticipant.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/entities/participants/EntitiesHoverParticipant.java @@ -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; @@ -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); } } }