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
d3dfc02
commit fca0abf
Showing
16 changed files
with
381 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
124 changes: 124 additions & 0 deletions
124
...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,124 @@ | ||
/** | ||
* 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.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.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 { | ||
DOMDocument document = request.getXMLDocument(); | ||
int offset = request.getOffset(); | ||
String text = document.getText(); | ||
int entityReferenceStart = XMLPositionUtility.getEntityReferenceStartOffset(text, offset); | ||
if (entityReferenceStart == -1) { | ||
return null; | ||
} | ||
// the start offset is the amp character, ignore it | ||
entityReferenceStart++; | ||
int entityReferenceEnd = XMLPositionUtility.getEntityReferenceEndOffset(text, offset); | ||
// Find hover of the entity | ||
// abcd &loc|al; --> in this case search local entity | ||
if (text.charAt(entityReferenceEnd - 1) == ';') { | ||
entityReferenceEnd--; | ||
} | ||
String entityName = text.substring(entityReferenceStart, entityReferenceEnd); | ||
Range entityRange = XMLPositionUtility.createRange(entityReferenceStart, entityReferenceEnd, document); | ||
MarkupContent entityContents = searchInInternalEntities(entityName, entityRange, document, request); | ||
if (entityContents == null) { | ||
entityContents = searchInExternalEntities(entityName, entityRange, document, request); | ||
} | ||
if (entityContents != null) { | ||
return new Hover(entityContents, entityRange); | ||
} | ||
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
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.