use of org.eclipse.lsp4j.MarkedString in project eclipse.jdt.ls by eclipse.
the class HoverInfoProvider method computeHover.
public List<Either<String, MarkedString>> computeHover(int line, int column, IProgressMonitor monitor) {
List<Either<String, MarkedString>> res = new LinkedList<>();
try {
IJavaElement[] elements = JDTUtils.findElementsAtSelection(unit, line, column, this.preferenceManager, monitor);
if (elements == null || elements.length == 0) {
res.add(Either.forLeft(""));
return res;
}
IJavaElement curr = null;
if (elements.length != 1) {
// they could be package fragments.
// We need to select the one that matches the package fragment of the current unit
IPackageFragment packageFragment = (IPackageFragment) unit.getParent();
IJavaElement found = Stream.of(elements).filter(e -> e.equals(packageFragment)).findFirst().orElse(null);
if (found == null) {
// this would be a binary package fragment
curr = elements[0];
} else {
curr = found;
}
} else {
curr = elements[0];
}
boolean resolved = isResolved(curr, monitor);
if (resolved) {
MarkedString signature = this.computeSignature(curr);
if (signature != null) {
res.add(Either.forRight(signature));
}
String javadoc = computeJavadocHover(curr);
if (javadoc != null) {
res.add(Either.forLeft(javadoc));
}
}
} catch (Exception e) {
JavaLanguageServerPlugin.logException("Error computing hover", e);
}
return res;
}
use of org.eclipse.lsp4j.MarkedString in project eclipse.jdt.ls by eclipse.
the class HoverHandlerTest method testHoverStandalone.
@Test
public void testHoverStandalone() throws Exception {
// given
// Hovers on the System.out
URI standalone = Paths.get("projects", "maven", "salut", "src", "main", "java", "java", "Foo.java").toUri();
String payload = createHoverRequest(standalone, 10, 71);
TextDocumentPositionParams position = getParams(payload);
// when
Hover hover = handler.hover(position, monitor);
// then
assertNotNull(hover);
assertNotNull(hover.getContents());
MarkedString signature = hover.getContents().get(0).getRight();
assertEquals("Unexpected hover " + signature, "java", signature.getLanguage());
assertEquals("Unexpected hover " + signature, "java.Foo", signature.getValue());
String doc = hover.getContents().get(1).getLeft();
assertEquals("Unexpected hover " + doc, "This is foo", doc);
}
use of org.eclipse.lsp4j.MarkedString in project ballerina by ballerina-lang.
the class HoverUtil method getDocumentationContent.
/**
* Get documentation content.
*
* @param docAnnotation list of doc annotation
* @return {@link Hover} hover object.
*/
private static Hover getDocumentationContent(List<BLangDocumentation> docAnnotation) {
Hover hover = new Hover();
StringBuilder content = new StringBuilder();
BLangDocumentation bLangDocumentation = docAnnotation.get(0);
Map<String, List<BLangDocumentationAttribute>> filterAttributes = filterDocumentationAttributes(docAnnotation.get(0));
if (!bLangDocumentation.documentationText.isEmpty()) {
content.append(getFormattedHoverDocContent(ContextConstants.DESCRIPTION, bLangDocumentation.documentationText));
}
if (filterAttributes.get(ContextConstants.DOC_RECEIVER) != null) {
content.append(getFormattedHoverDocContent(ContextConstants.DOC_RECEIVER, getDocAttributes(filterAttributes.get(ContextConstants.DOC_RECEIVER))));
}
if (filterAttributes.get(ContextConstants.DOC_PARAM) != null) {
content.append(getFormattedHoverDocContent(ContextConstants.DOC_PARAM, getDocAttributes(filterAttributes.get(ContextConstants.DOC_PARAM))));
}
if (filterAttributes.get(ContextConstants.DOC_FIELD) != null) {
content.append(getFormattedHoverDocContent(ContextConstants.DOC_FIELD, getDocAttributes(filterAttributes.get(ContextConstants.DOC_FIELD))));
}
if (filterAttributes.get(ContextConstants.DOC_RETURN) != null) {
content.append(getFormattedHoverDocContent(ContextConstants.DOC_RETURN, getDocAttributes(filterAttributes.get(ContextConstants.DOC_RETURN))));
}
if (filterAttributes.get(ContextConstants.DOC_VARIABLE) != null) {
content.append(getFormattedHoverDocContent(ContextConstants.DOC_VARIABLE, getDocAttributes(filterAttributes.get(ContextConstants.DOC_VARIABLE))));
}
List<Either<String, MarkedString>> contents = new ArrayList<>();
contents.add(Either.forLeft(content.toString()));
hover.setContents(contents);
return hover;
}
use of org.eclipse.lsp4j.MarkedString in project sts4 by spring-projects.
the class Editor method hoverString.
public String hoverString(Hover hover) {
StringBuilder buf = new StringBuilder();
boolean first = true;
for (Either<String, MarkedString> block : hover.getContents()) {
if (!first) {
buf.append("\n\n");
}
if (block.isLeft()) {
String s = block.getLeft();
buf.append(s);
} else if (block.isRight()) {
MarkedString ms = block.getRight();
buf.append("```" + ms.getLanguage() + "\n");
buf.append(ms.getValue());
buf.append("\n```");
}
first = false;
}
return buf.toString();
}
use of org.eclipse.lsp4j.MarkedString in project xtext-core by eclipse.
the class HoverService method getContents.
protected List<Either<String, MarkedString>> getContents(final HoverContext it) {
final String language = this.getLanguage(it);
final Function1<String, Either<String, MarkedString>> _function = (String value) -> {
return this.toContents(language, value);
};
return ListExtensions.<String, Either<String, MarkedString>>map(this.getContents(it.getElement()), _function);
}
Aggregations