Search in sources :

Example 31 with Hover

use of org.eclipse.lsp4j.Hover in project ballerina by ballerina-lang.

the class HoverUtil method getHoverInformation.

/**
 * Get the hover information for the given hover context.
 *
 * @param bLangPackage resolved bLangPackage for the hover context.
 * @param hoverContext context of the hover.
 * @return hover content.
 */
public static Hover getHoverInformation(BLangPackage bLangPackage, TextDocumentServiceContext hoverContext) {
    Hover hover;
    switch(hoverContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_PARENT_KEY)) {
        case ContextConstants.FUNCTION:
            BLangFunction bLangFunction = bLangPackage.functions.stream().filter(function -> function.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangFunction != null) {
                if (bLangFunction.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangFunction.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangFunction.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.STRUCT:
            BLangStruct bLangStruct = bLangPackage.structs.stream().filter(struct -> struct.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangStruct != null) {
                if (bLangStruct.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangStruct.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangStruct.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.OBJECT:
            BLangObject bLangObject = bLangPackage.objects.stream().filter(object -> object.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangObject != null) {
                if (bLangObject.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangObject.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangObject.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.ENUM:
            BLangEnum bLangEnum = bLangPackage.enums.stream().filter(bEnum -> bEnum.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangEnum != null) {
                if (bLangEnum.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangEnum.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangEnum.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.TRANSFORMER:
            BLangTransformer bLangTransformer = bLangPackage.transformers.stream().filter(bTransformer -> bTransformer.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangTransformer != null) {
                if (bLangTransformer.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangTransformer.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangTransformer.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.CONNECTOR:
            BLangConnector bLangConnector = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangConnector != null) {
                if (bLangConnector.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangConnector.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangConnector.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.ACTION:
            BLangAction bLangAction = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(((BLangInvocation) hoverContext.get(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY)).symbol.owner.name.getValue())).flatMap(connector -> connector.actions.stream()).filter(bAction -> bAction.name.getValue().equals(hoverContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangAction != null) {
                if (bLangAction.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangAction.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangAction.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.ENDPOINT:
            BLangEndpoint bLangEndpoint = bLangPackage.globalEndpoints.stream().filter(globalEndpoint -> globalEndpoint.name.value.equals(hoverContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangEndpoint != null) {
                hover = getAnnotationContent(bLangEndpoint.annAttachments);
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        case ContextConstants.VARIABLE:
            BLangVariable bLangVariable = bLangPackage.globalVars.stream().filter(globalVar -> globalVar.name.getValue().equals(hoverContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangVariable != null) {
                if (bLangVariable.docAttachments.size() > 0) {
                    hover = getDocumentationContent(bLangVariable.docAttachments);
                } else {
                    hover = getAnnotationContent(bLangVariable.annAttachments);
                }
            } else {
                hover = getDefaultHoverObject();
            }
            break;
        default:
            hover = new Hover();
            List<Either<String, MarkedString>> contents = new ArrayList<>();
            contents.add(Either.forLeft(""));
            hover.setContents(contents);
            break;
    }
    return hover;
}
Also used : BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) HashMap(java.util.HashMap) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) Hover(org.eclipse.lsp4j.Hover) NodeContextKeys(org.ballerinalang.langserver.common.constants.NodeContextKeys) ArrayList(java.util.ArrayList) PositionTreeVisitor(org.ballerinalang.langserver.common.position.PositionTreeVisitor) ContextConstants(org.ballerinalang.langserver.common.constants.ContextConstants) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) MarkedString(org.eclipse.lsp4j.MarkedString) LSPackageCache(org.ballerinalang.langserver.LSPackageCache) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangTransformer(org.wso2.ballerinalang.compiler.tree.BLangTransformer) List(java.util.List) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector) BLangDocumentationAttribute(org.wso2.ballerinalang.compiler.tree.expressions.BLangDocumentationAttribute) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) ArrayList(java.util.ArrayList) BLangStruct(org.wso2.ballerinalang.compiler.tree.BLangStruct) BLangEnum(org.wso2.ballerinalang.compiler.tree.BLangEnum) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) BLangAction(org.wso2.ballerinalang.compiler.tree.BLangAction) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) Hover(org.eclipse.lsp4j.Hover) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) BLangConnector(org.wso2.ballerinalang.compiler.tree.BLangConnector)

Example 32 with Hover

use of org.eclipse.lsp4j.Hover in project ballerina by ballerina-lang.

the class HoverUtil method getAnnotationContent.

/**
 * get concatenated annotation value.
 *
 * @param annAttachments annotation attachments list
 * @return Hover object with hover content.
 */
private static Hover getAnnotationContent(List<BLangAnnotationAttachment> annAttachments) {
    Hover hover = new Hover();
    StringBuilder content = new StringBuilder();
    if (!getAnnotationValue(ContextConstants.DESCRIPTION, annAttachments).isEmpty()) {
        content.append(getFormattedHoverContent(ContextConstants.DESCRIPTION, getAnnotationValue(ContextConstants.DESCRIPTION, annAttachments)));
    }
    if (!getAnnotationValue(ContextConstants.PARAM, annAttachments).isEmpty()) {
        content.append(getFormattedHoverContent(ContextConstants.PARAM, getAnnotationValue(ContextConstants.PARAM, annAttachments)));
    }
    if (!getAnnotationValue(ContextConstants.FIELD, annAttachments).isEmpty()) {
        content.append(getFormattedHoverContent(ContextConstants.FIELD, getAnnotationValue(ContextConstants.FIELD, annAttachments)));
    }
    if (!getAnnotationValue(ContextConstants.RETURN, annAttachments).isEmpty()) {
        content.append(getFormattedHoverContent(ContextConstants.RETURN, getAnnotationValue(ContextConstants.RETURN, annAttachments)));
    }
    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)

Example 33 with Hover

use of org.eclipse.lsp4j.Hover in project ballerina by ballerina-lang.

the class BallerinaTextDocumentService method hover.

@Override
public CompletableFuture<Hover> hover(TextDocumentPositionParams position) {
    return CompletableFuture.supplyAsync(() -> {
        TextDocumentServiceContext hoverContext = new TextDocumentServiceContext();
        hoverContext.put(DocumentServiceKeys.FILE_URI_KEY, position.getTextDocument().getUri());
        hoverContext.put(DocumentServiceKeys.POSITION_KEY, position);
        Hover hover;
        try {
            BLangPackage currentBLangPackage = TextDocumentServiceUtil.getBLangPackage(hoverContext, documentManager, false, LSCustomErrorStrategy.class, false).get(0);
            hoverContext.put(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY, currentBLangPackage.symbol.getName().getValue());
            lSPackageCache.addPackage(currentBLangPackage);
            hover = HoverUtil.getHoverContent(hoverContext, currentBLangPackage, lSPackageCache);
        } catch (Exception | AssertionError e) {
            hover = new Hover();
            List<Either<String, MarkedString>> contents = new ArrayList<>();
            contents.add(Either.forLeft(""));
            hover.setContents(contents);
        }
        return hover;
    });
}
Also used : BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Hover(org.eclipse.lsp4j.Hover) MarkedString(org.eclipse.lsp4j.MarkedString) List(java.util.List) ArrayList(java.util.ArrayList) CompletionList(org.eclipse.lsp4j.CompletionList) MarkedString(org.eclipse.lsp4j.MarkedString) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy)

Example 34 with Hover

use of org.eclipse.lsp4j.Hover in project sts4 by spring-projects.

the class VscodeHoverEngineAdapter method handle.

@Override
public Hover handle(TextDocumentPositionParams params) {
    // a trivial pre-resolved future.
    try {
        SimpleTextDocumentService documents = server.getTextDocumentService();
        TextDocument doc = documents.get(params);
        if (doc != null) {
            int offset = doc.toOffset(params.getPosition());
            Tuple2<Renderable, IRegion> hoverTuple = hoverInfoProvider.getHoverInfo(doc, offset);
            if (hoverTuple != null) {
                Renderable hoverInfo = hoverTuple.getT1();
                IRegion region = hoverTuple.getT2();
                Range range = doc.toRange(region.getOffset(), region.getLength());
                String rendered = render(hoverInfo, type);
                if (StringUtil.hasText(rendered)) {
                    Hover hover = new Hover(ImmutableList.of(Either.forLeft(rendered)), range);
                    return hover;
                }
            }
        }
    } catch (Exception e) {
        logger.error("error computing hover", e);
    }
    return SimpleTextDocumentService.NO_HOVER;
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) Renderable(org.springframework.ide.vscode.commons.util.Renderable) Hover(org.eclipse.lsp4j.Hover) SimpleTextDocumentService(org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService) Range(org.eclipse.lsp4j.Range) IRegion(org.springframework.ide.vscode.commons.util.text.IRegion)

Example 35 with Hover

use of org.eclipse.lsp4j.Hover in project sts4 by spring-projects.

the class RequestMappingHoverProvider method provideHover.

private Hover provideHover(Annotation annotation, TextDocument doc, SpringBootApp[] runningApps) {
    try {
        List<Either<String, MarkedString>> hoverContent = new ArrayList<>();
        List<Tuple2<RequestMapping, SpringBootApp>> val = getRequestMappingMethodFromRunningApp(annotation, runningApps);
        if (!val.isEmpty()) {
            addHoverContent(val, hoverContent);
        }
        Range hoverRange = doc.toRange(annotation.getStartPosition(), annotation.getLength());
        Hover hover = new Hover();
        hover.setContents(hoverContent);
        hover.setRange(hoverRange);
        return hover;
    } catch (Exception e) {
        Log.log(e);
    }
    return null;
}
Also used : Tuple2(reactor.util.function.Tuple2) Hover(org.eclipse.lsp4j.Hover) ArrayList(java.util.ArrayList) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Aggregations

Hover (org.eclipse.lsp4j.Hover)41 MarkedString (org.eclipse.lsp4j.MarkedString)14 Test (org.junit.Test)12 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)11 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)11 ArrayList (java.util.ArrayList)10 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)10 Range (org.eclipse.lsp4j.Range)5 SpringBootApp (org.springframework.ide.vscode.commons.boot.app.cli.SpringBootApp)5 List (java.util.List)4 Position (org.eclipse.lsp4j.Position)3 LiveBean (org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBean)3 LiveBeansModel (org.springframework.ide.vscode.commons.boot.app.cli.livebean.LiveBeansModel)3 File (java.io.File)2 URI (java.net.URI)2 IDefinition (org.apache.flex.compiler.definitions.IDefinition)2 PositionTreeVisitor (org.ballerinalang.langserver.common.position.PositionTreeVisitor)2 IClassFile (org.eclipse.jdt.core.IClassFile)2 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)2 SimpleTextDocumentService (org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService)2