Search in sources :

Example 1 with MarkedString

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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) MarkedString(org.eclipse.lsp4j.MarkedString) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) MarkedString(org.eclipse.lsp4j.MarkedString) LinkedList(java.util.LinkedList) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException)

Example 2 with MarkedString

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);
}
Also used : Hover(org.eclipse.lsp4j.Hover) MarkedString(org.eclipse.lsp4j.MarkedString) MarkedString(org.eclipse.lsp4j.MarkedString) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) URI(java.net.URI) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 3 with MarkedString

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;
}
Also used : Hover(org.eclipse.lsp4j.Hover) ArrayList(java.util.ArrayList) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) ArrayList(java.util.ArrayList) List(java.util.List) MarkedString(org.eclipse.lsp4j.MarkedString)

Example 4 with MarkedString

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();
}
Also used : MarkedString(org.eclipse.lsp4j.MarkedString) MarkedString(org.eclipse.lsp4j.MarkedString)

Example 5 with MarkedString

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);
}
Also used : Either(org.eclipse.lsp4j.jsonrpc.messages.Either) MarkedString(org.eclipse.lsp4j.MarkedString)

Aggregations

MarkedString (org.eclipse.lsp4j.MarkedString)12 Hover (org.eclipse.lsp4j.Hover)9 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)4 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)4 Test (org.junit.Test)4 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)2 ImmutableList (com.google.common.collect.ImmutableList)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1