Skip to content

Commit

Permalink
Add support for hovering on tag text
Browse files Browse the repository at this point in the history
Add onTagText to IHoverParticipant, fix eclipse#609

Signed-off-by: Andrew Obuchowicz <[email protected]>
  • Loading branch information
AObuchow committed Mar 12, 2020
1 parent d8556f4 commit 537f587
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.dom.DOMText;
import org.eclipse.lemminx.dom.parser.Scanner;
import org.eclipse.lemminx.dom.parser.TokenType;
import org.eclipse.lemminx.dom.parser.XMLScanner;
Expand Down Expand Up @@ -84,10 +85,36 @@ public Hover doHover(DOMDocument xmlDocument, Position position, XMLHoverSetting
}
// Attribute is hover
return getAttrNameHover(hoverRequest, null);
} else if (node.isText()) {
// Text is hover
DOMText text = (DOMText) node;
try {
Range textRange = new Range(xmlDocument.positionAt(text.getStart()),
xmlDocument.positionAt(text.getEnd()));
return getTextHover(hoverRequest, textRange);
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "While creating Range in XMLHover the text's Offset was a BadLocation", e);
}
}
return null;
}

private Hover getTextHover(HoverRequest hoverRequest, Range textRange) {
hoverRequest.setTagRange(textRange);
List<String> contentValues = new ArrayList<String>();
for (IHoverParticipant participant : extensionsRegistry.getHoverParticipants()) {
try {
String contentValue = participant.onTagText(hoverRequest);
if (contentValue != null) {
contentValues.add(contentValue);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "While performing IHoverParticipant#onTagText", e);
}
}
return createHover(contentValues, hoverRequest);
}

/**
* Returns the LSP hover from the hovered element.
*
Expand All @@ -111,7 +138,7 @@ private Hover getTagHover(HoverRequest hoverRequest, Range tagRange, boolean ope
LOGGER.log(Level.SEVERE, "While performing IHoverParticipant#onTag", e);
}
}
return creatHover(contentValues, hoverRequest);
return createHover(contentValues, hoverRequest);
}

private Range getTagNameRange(TokenType tokenType, int startOffset, int offset, DOMDocument document) {
Expand Down Expand Up @@ -154,7 +181,7 @@ private Hover getAttrNameHover(HoverRequest hoverRequest, Range attrRange) {
LOGGER.log(Level.SEVERE, "While performing IHoverParticipant#onTag", e);
}
}
return creatHover(contentValues, hoverRequest);
return createHover(contentValues, hoverRequest);
}

/**
Expand All @@ -178,7 +205,7 @@ private Hover getAttrValueHover(HoverRequest hoverRequest, Range attrRange) {
LOGGER.log(Level.SEVERE, "While performing IHoverParticipant#onTag", e);
}
}
return creatHover(contentValues, hoverRequest);
return createHover(contentValues, hoverRequest);
}

/**
Expand All @@ -188,7 +215,7 @@ private Hover getAttrValueHover(HoverRequest hoverRequest, Range attrRange) {
* @param hoverRequest the hover request.
* @return the aggregated LSP hover from the value list.
*/
private static Hover creatHover(List<String> contentValues, HoverRequest hoverRequest) {
private static Hover createHover(List<String> contentValues, HoverRequest hoverRequest) {
if (!contentValues.isEmpty()) {
return new Hover(MarkupContentFactory.creatMarkupContent(contentValues, hoverRequest), hoverRequest.getTagRange());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public String onAttributeValue(IHoverRequest request) throws Exception {
return null;
}

@Override
public String onTagText(IHoverRequest resquest) throws Exception {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,13 @@ public interface IHoverParticipant {
* @return the Value of MarkupContent {@link String}
*/
String onAttributeValue(IHoverRequest request) throws Exception;

/**
* onTagText method
*
* @param hoverRequest the hover request.
* @return the Value of MarkupContent {@link String}
*/
String onTagText(IHoverRequest resquest) throws Exception;

}

0 comments on commit 537f587

Please sign in to comment.