forked from eclipse/lemminx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes eclipse#663 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
b0dd7ce
commit bd7d58d
Showing
16 changed files
with
421 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
...n/java/org/eclipse/lemminx/extensions/entities/participants/EntitiesHoverParticipant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/** | ||
* Copyright (c) 2020 Red Hat, Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.lemminx.extensions.entities.participants; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.eclipse.lemminx.dom.DOMDocument; | ||
import org.eclipse.lemminx.dom.DOMDocumentType; | ||
import org.eclipse.lemminx.dom.DOMNode; | ||
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; | ||
import org.eclipse.lemminx.services.extensions.HoverParticipantAdapter; | ||
import org.eclipse.lemminx.services.extensions.IHoverRequest; | ||
import org.eclipse.lemminx.utils.XMLPositionUtility; | ||
import org.eclipse.lemminx.utils.XMLPositionUtility.EntityReferenceRange; | ||
import org.eclipse.lsp4j.Hover; | ||
import org.eclipse.lsp4j.MarkupContent; | ||
import org.eclipse.lsp4j.MarkupKind; | ||
import org.eclipse.lsp4j.Range; | ||
import org.w3c.dom.Entity; | ||
import org.w3c.dom.NamedNodeMap; | ||
|
||
/** | ||
* Entities hover used in a text node (ex : &). | ||
* | ||
*/ | ||
public class EntitiesHoverParticipant extends HoverParticipantAdapter { | ||
|
||
@Override | ||
public Hover onText(IHoverRequest request) throws Exception { | ||
DOMNode node = request.getNode(); | ||
if (!node.isText()) { | ||
return null; | ||
} | ||
// Hover is done in a text node, check if it's a entity reference | ||
DOMDocument document = request.getXMLDocument(); | ||
int offset = request.getOffset(); | ||
EntityReferenceRange entityRange = XMLPositionUtility.selectEntityReference(offset, document); | ||
if (entityRange == null) { | ||
return null; | ||
} | ||
// The hovered text follows the entity reference syntax (ex : &) | ||
String entityName = entityRange.getName(); | ||
Range range = entityRange.getRange(); | ||
// Try to find the entity | ||
MarkupContent entityContents = searchInEntities(entityName, range, document, request); | ||
if (entityContents != null) { | ||
return new Hover(entityContents, range); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Returns the markup content of the given entity name in the predefined, | ||
* internal and external entities and null otherwise. | ||
* | ||
* @param entityName the entity name to search. | ||
* @param range the hovered range. | ||
* @param document the DOM document | ||
* @param request the hover request. | ||
* @return the markup content of the given entity name in the predefined, | ||
* internal and external entities and null otherwise. | ||
*/ | ||
private static MarkupContent searchInEntities(String entityName, Range range, DOMDocument document, | ||
IHoverRequest request) { | ||
MarkupContent entityContents = searchInPredefinedEntities(entityName, range, document, request); | ||
if (entityContents != null) { | ||
return entityContents; | ||
} | ||
entityContents = searchInInternalEntities(entityName, range, document, request); | ||
if (entityContents != null) { | ||
return entityContents; | ||
} | ||
return searchInExternalEntities(entityName, range, document, request); | ||
} | ||
|
||
/** | ||
* Returns the markup content of the given entity name in the predefined | ||
* entities and null otherwise. | ||
* | ||
* @param entityName the entity name to search. | ||
* @param range the hovered range. | ||
* @param document the DOM document | ||
* @param request the hover request. | ||
* @return the markup content of the given entity name in the predefined | ||
* entities and null otherwise. | ||
*/ | ||
private static MarkupContent searchInPredefinedEntities(String entityName, Range range, DOMDocument document, | ||
IHoverRequest request) { | ||
// TODO Auto-generated method stub | ||
return null; | ||
} | ||
|
||
/** | ||
* Search the given entity name in the internal entities. | ||
* | ||
* @param document the DOM document. | ||
* @param entityName the entity name. | ||
* @param entityRange the entity range. | ||
* @param locations the location links | ||
*/ | ||
private static MarkupContent searchInInternalEntities(String entityName, Range entityRange, DOMDocument document, | ||
IHoverRequest request) { | ||
DOMDocumentType docType = document.getDoctype(); | ||
if (docType == null) { | ||
return null; | ||
} | ||
// Loop for entities declared in the DOCTYPE of the document | ||
NamedNodeMap entities = docType.getEntities(); | ||
for (int i = 0; i < entities.getLength(); i++) { | ||
DTDEntityDecl entity = (DTDEntityDecl) entities.item(i); | ||
if (entityName.equals(entity.getName())) { | ||
boolean markdown = request.canSupportMarkupKind(MarkupKind.MARKDOWN); | ||
return EntitiesDocumentationUtils.getDocumentation(entity.getName(), entity.getNotationName(), true, | ||
false, markdown); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Search the given entity name in the external entities. | ||
* | ||
* @param document the DOM document. | ||
* @param entityName the entity name. | ||
* @param entityRange the entity range. | ||
* @param locations the location links | ||
* @param request the definition request. | ||
* @return | ||
*/ | ||
private static MarkupContent searchInExternalEntities(String entityName, Range entityRange, DOMDocument document, | ||
IHoverRequest request) { | ||
ContentModelManager contentModelManager = request.getComponent(ContentModelManager.class); | ||
Collection<CMDocument> cmDocuments = contentModelManager.findCMDocument(document, null, false); | ||
for (CMDocument cmDocument : cmDocuments) { | ||
List<Entity> entities = cmDocument.getEntities(); | ||
for (Entity ent : entities) { | ||
DTDEntityDecl entity = (DTDEntityDecl) ent; | ||
if (entityName.equals(entity.getName())) { | ||
boolean markdown = request.canSupportMarkupKind(MarkupKind.MARKDOWN); | ||
return EntitiesDocumentationUtils.getDocumentation(entity.getName(), entity.getNotationName(), true, | ||
false, markdown); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.