Skip to content

Commit

Permalink
Using String as return type for Hover paricipants
Browse files Browse the repository at this point in the history
  • Loading branch information
Seiphon committed Oct 23, 2019
1 parent 9ed1770 commit 2f2dab0
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/
package org.eclipse.lsp4xml.extensions.contentmodel.participants;

import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.MarkupKind;
import org.eclipse.lsp4xml.dom.DOMAttr;
Expand All @@ -33,15 +32,15 @@
public class ContentModelHoverParticipant extends HoverParticipantAdapter {

@Override
public Hover onTag(IHoverRequest hoverRequest) throws Exception {
public String onTag(IHoverRequest hoverRequest) throws Exception {
try {
ContentModelManager contentModelManager = hoverRequest.getComponent(ContentModelManager.class);
DOMElement node = (DOMElement) hoverRequest.getNode();
CMElementDeclaration cmElement = contentModelManager.findCMElement(node);
if (cmElement != null) {
MarkupContent content = XMLGenerator.createMarkupContent(cmElement, hoverRequest);
if (content != null) {
return new Hover(content, hoverRequest.getTagRange());
return content.getValue();
}
}
} catch (CacheResourceDownloadingException e) {
Expand All @@ -51,7 +50,7 @@ public Hover onTag(IHoverRequest hoverRequest) throws Exception {
}

@Override
public Hover onAttributeName(IHoverRequest hoverRequest) throws Exception {
public String onAttributeName(IHoverRequest hoverRequest) throws Exception {
DOMAttr attribute = (DOMAttr) hoverRequest.getNode();
try {
ContentModelManager contentModelManager = hoverRequest.getComponent(ContentModelManager.class);
Expand All @@ -62,7 +61,7 @@ public Hover onAttributeName(IHoverRequest hoverRequest) throws Exception {
if (cmAttribute != null) {
MarkupContent content = XMLGenerator.createMarkupContent(cmAttribute, cmElement, hoverRequest);
if (content != null) {
return new Hover(content, hoverRequest.getTagRange());
return content.getValue();
}
}
}
Expand All @@ -73,13 +72,13 @@ public Hover onAttributeName(IHoverRequest hoverRequest) throws Exception {
}

@Override
public Hover onAttributeValue(IHoverRequest hoverRequest) throws Exception {
public String onAttributeValue(IHoverRequest hoverRequest) throws Exception {
DOMAttr attribute = (DOMAttr) hoverRequest.getNode();

// Attempts to compute specifically for XSI related attributes since
// the XSD itself does not have enough information. Should create a mock XSD
// eventually.
Hover temp = XSISchemaModel.computeHoverResponse(attribute, hoverRequest);
String temp = XSISchemaModel.computeHoverResponse(attribute, hoverRequest);
if (temp != null) {
return temp;
}
Expand All @@ -96,7 +95,7 @@ public Hover onAttributeValue(IHoverRequest hoverRequest) throws Exception {
MarkupContent content = XMLGenerator.createMarkupContent(cmAttribute, attributeValue, cmElement,
hoverRequest);
if (content != null) {
return new Hover(content, hoverRequest.getTagRange());
return content.getValue();
}
}
}
Expand All @@ -106,11 +105,11 @@ public Hover onAttributeValue(IHoverRequest hoverRequest) throws Exception {
return null;
}

private static Hover getCacheWarningHover(CacheResourceDownloadingException e, IMarkupKindSupport support) {
private static String getCacheWarningHover(CacheResourceDownloadingException e, IMarkupKindSupport support) {
// Here cache is enabled and some XML Schema, DTD, etc are loading
MarkupContent content = MarkupContentFactory.createMarkupContent(
"Cannot process " + (e.isDTD() ? "DTD" : "XML Schema") + " hover: " + e.getMessage(),
MarkupKind.MARKDOWN, support);
return new Hover(content);
return content.getValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

package org.eclipse.lsp4xml.extensions.xsi;

import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4xml.dom.DOMAttr;
import org.eclipse.lsp4xml.services.extensions.HoverParticipantAdapter;
import org.eclipse.lsp4xml.services.extensions.IHoverRequest;
Expand All @@ -21,14 +20,14 @@
public class XSIHoverParticipant extends HoverParticipantAdapter{

@Override
public Hover onAttributeName(IHoverRequest request) throws Exception {
public String onAttributeName(IHoverRequest request) throws Exception {

DOMAttr attribute = (DOMAttr) request.getNode();
return XSISchemaModel.computeHoverResponse(attribute, request);
}

@Override
public Hover onAttributeValue(IHoverRequest request) throws Exception {
public String onAttributeValue(IHoverRequest request) throws Exception {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.MarkupKind;
import org.eclipse.lsp4j.Range;
Expand All @@ -29,7 +28,6 @@
import org.eclipse.lsp4xml.services.extensions.ICompletionRequest;
import org.eclipse.lsp4xml.services.extensions.ICompletionResponse;
import org.eclipse.lsp4xml.services.extensions.IHoverRequest;
import org.eclipse.lsp4xml.settings.SharedSettings;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
import org.eclipse.lsp4xml.utils.StringUtils;

Expand Down Expand Up @@ -206,7 +204,7 @@ private static boolean hasAttribute(DOMElement root, String name) {
return hasAttribute(root, null, name);
}

public static Hover computeHoverResponse(DOMAttr attribute, IHoverRequest request) {
public static String computeHoverResponse(DOMAttr attribute, IHoverRequest request) {

String name = attribute.getName();
if(!name.startsWith(request.getXMLDocument().getSchemaInstancePrefix() + ":")) {
Expand Down Expand Up @@ -239,10 +237,7 @@ else if(name.endsWith(":type")) {
return null;
}
}

MarkupContent content = new MarkupContent();
content.setKind(MarkupKind.MARKDOWN);
content.setValue(doc);
return new Hover(content);

return doc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package org.eclipse.lsp4xml.services;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -28,6 +30,7 @@
import org.eclipse.lsp4xml.services.extensions.IHoverParticipant;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lsp4xml.settings.XMLHoverSettings;
import org.eclipse.lsp4xml.utils.MarkupContentFactory;

/**
* XML hover support.
Expand Down Expand Up @@ -95,17 +98,18 @@ public Hover doHover(DOMDocument xmlDocument, Position position, XMLHoverSetting
private Hover getTagHover(HoverRequest hoverRequest, Range tagRange, boolean open) {
hoverRequest.setTagRange(tagRange);
hoverRequest.setOpen(open);
List<String> contentValues = new ArrayList<String>();
for (IHoverParticipant participant : extensionsRegistry.getHoverParticipants()) {
try {
Hover hover = participant.onTag(hoverRequest);
if (hover != null) {
return hover;
String contentValue = participant.onTag(hoverRequest);
if (contentValue != null) {
contentValues.add(contentValue);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "While performing IHoverParticipant#onTag", e);
}
}
return null;
return creatHover(contentValues, hoverRequest);
}

private Range getTagNameRange(TokenType tokenType, int startOffset, int offset, DOMDocument document) {
Expand Down Expand Up @@ -137,17 +141,18 @@ private Range getTagNameRange(TokenType tokenType, int startOffset, int offset,
private Hover getAttrNameHover(HoverRequest hoverRequest, Range attrRange) {
// hoverRequest.setTagRange(tagRange);
// hoverRequest.setOpen(open);
List<String> contentValues = new ArrayList<String>();
for (IHoverParticipant participant : extensionsRegistry.getHoverParticipants()) {
try {
Hover hover = participant.onAttributeName(hoverRequest);
if (hover != null) {
return hover;
String contentValue = participant.onAttributeName(hoverRequest);
if (contentValue != null) {
contentValues.add(contentValue);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "While performing IHoverParticipant#onTag", e);
}
}
return null;
return creatHover(contentValues, hoverRequest);
}

/**
Expand All @@ -160,16 +165,31 @@ private Hover getAttrNameHover(HoverRequest hoverRequest, Range attrRange) {
private Hover getAttrValueHover(HoverRequest hoverRequest, Range attrRange) {
// hoverRequest.setTagRange(tagRange);
// hoverRequest.setOpen(open);
List<String> contentValues = new ArrayList<String>();
for (IHoverParticipant participant : extensionsRegistry.getHoverParticipants()) {
try {
Hover hover = participant.onAttributeValue(hoverRequest);
if (hover != null) {
return hover;
String contentValue = participant.onAttributeValue(hoverRequest);
if (contentValue != null) {
contentValues.add(contentValue);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "While performing IHoverParticipant#onTag", e);
}
}
return creatHover(contentValues, hoverRequest);
}

/**
* Returns the aggregated LSP hover from the value list.
*
* @param contentValues the content values.
* @param hoverRequest the hover request.
* @return the aggregated LSP hover from the value list.
*/
private static Hover creatHover(List<String> contentValues, HoverRequest hoverRequest) {
if (!contentValues.isEmpty()) {
return new Hover(MarkupContentFactory.creatMarkupContent(contentValues, hoverRequest), hoverRequest.getTagRange());
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
public class HoverParticipantAdapter implements IHoverParticipant {

@Override
public Hover onTag(IHoverRequest request) throws Exception {
public String onTag(IHoverRequest request) throws Exception {
return null;
}

@Override
public Hover onAttributeName(IHoverRequest request) throws Exception {
public String onAttributeName(IHoverRequest request) throws Exception {
return null;
}

@Override
public Hover onAttributeValue(IHoverRequest request) throws Exception {
public String onAttributeValue(IHoverRequest request) throws Exception {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,34 @@
*/
package org.eclipse.lsp4xml.services.extensions;

import org.eclipse.lsp4j.Hover;

/**
* Hover participant API.
*
*/
public interface IHoverParticipant {

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*******************************************************************************/
package org.eclipse.lsp4xml.utils;

import java.util.List;

import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.MarkupKind;

Expand Down Expand Up @@ -57,4 +59,29 @@ public static MarkupContent createMarkupContent(String value, String preferredKi
}
return content;
}

/**
* Create the markup content according the given markup kind and the capability
* of the client.
*
* @param values the list of documentation values
* @return the markup content according the given markup kind and the capability
* of the client.
*/
public static MarkupContent creatMarkupContent(List<String> values, IMarkupKindSupport request) {
String kind = request.canSupportMarkupKind(MarkupKind.MARKDOWN) ? MarkupKind.MARKDOWN : MarkupKind.PLAINTEXT;
if (values.size() ==1) {
return new MarkupContent(kind, values.get(0));
}
StringBuilder retValue = new StringBuilder();
for (String value : values) {
retValue.append(value);
if (kind.equals(MarkupKind.MARKDOWN)) {
retValue.append("___");
}
retValue.append(System.lineSeparator() );
retValue.append(System.lineSeparator() );
}
return new MarkupContent(kind, retValue.toString());
}
}
Loading

0 comments on commit 2f2dab0

Please sign in to comment.