Search in sources :

Example 1 with TextDocumentServiceContext

use of org.ballerinalang.langserver.TextDocumentServiceContext in project ballerina by ballerina-lang.

the class SignatureHelpUtil method getSignatureInformation.

/**
 * Get the signature information for the given Ballerina function.
 *
 * @param bInvokableSymbol BLang Invokable symbol
 * @param signatureContext Signature operation context
 * @return {@link SignatureInformation}     Signature information for the function
 */
private static SignatureInformation getSignatureInformation(BInvokableSymbol bInvokableSymbol, TextDocumentServiceContext signatureContext) {
    List<ParameterInformation> parameterInformationList = new ArrayList<>();
    SignatureInformation signatureInformation = new SignatureInformation();
    SignatureInfoModel signatureInfoModel = getSignatureInfoModel(bInvokableSymbol, signatureContext);
    String functionName = bInvokableSymbol.getName().getValue();
    // Join the function parameters to generate the function's signature
    String paramsJoined = signatureInfoModel.getParameterInfoModels().stream().map(parameterInfoModel -> {
        // For each of the parameters, create a parameter info instance
        ParameterInformation parameterInformation = new ParameterInformation(parameterInfoModel.paramValue, parameterInfoModel.description);
        parameterInformationList.add(parameterInformation);
        return parameterInfoModel.toString();
    }).collect(Collectors.joining(", "));
    signatureInformation.setLabel(functionName + "(" + paramsJoined + ")");
    signatureInformation.setParameters(parameterInformationList);
    signatureInformation.setDocumentation(signatureInfoModel.signatureDescription);
    return signatureInformation;
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) Stack(java.util.Stack) ArrayList(java.util.ArrayList) DocTag(org.ballerinalang.model.elements.DocTag) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) Map(java.util.Map) Position(org.eclipse.lsp4j.Position) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) ParameterInformation(org.eclipse.lsp4j.ParameterInformation) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangDocumentation(org.wso2.ballerinalang.compiler.tree.BLangDocumentation) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) SignatureInformation(org.eclipse.lsp4j.SignatureInformation) Collectors(java.util.stream.Collectors) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) Objects(java.util.Objects) List(java.util.List) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) UtilSymbolKeys(org.ballerinalang.langserver.common.UtilSymbolKeys) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) SignatureInformation(org.eclipse.lsp4j.SignatureInformation) ArrayList(java.util.ArrayList) ParameterInformation(org.eclipse.lsp4j.ParameterInformation)

Example 2 with TextDocumentServiceContext

use of org.ballerinalang.langserver.TextDocumentServiceContext in project ballerina by ballerina-lang.

the class DefinitionUtil method getDefinitionPosition.

/**
 * Get definition position for the given definition context.
 *
 * @param definitionContext   context of the definition.
 * @param lSPackageCache package context for language server.
 * @return position
 */
public static List<Location> getDefinitionPosition(TextDocumentServiceContext definitionContext, LSPackageCache lSPackageCache) {
    List<Location> contents = new ArrayList<>();
    if (definitionContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY) == null) {
        return contents;
    }
    String nodeKind = definitionContext.get(NodeContextKeys.SYMBOL_KIND_OF_NODE_KEY);
    BLangPackage bLangPackage = getPackageOfTheOwner(definitionContext.get(NodeContextKeys.NODE_OWNER_PACKAGE_KEY), definitionContext, lSPackageCache);
    BLangNode bLangNode = null;
    switch(nodeKind) {
        case ContextConstants.FUNCTION:
            bLangNode = bLangPackage.functions.stream().filter(function -> function.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.STRUCT:
            bLangNode = bLangPackage.structs.stream().filter(struct -> struct.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.OBJECT:
            bLangNode = bLangPackage.objects.stream().filter(object -> object.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.ENUM:
            bLangNode = bLangPackage.enums.stream().filter(enm -> enm.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            // Fixing the position issue with enum node.
            bLangNode.getPosition().eLine = bLangNode.getPosition().sLine;
            bLangNode.getPosition().eCol = bLangNode.getPosition().sCol;
            break;
        case ContextConstants.CONNECTOR:
            bLangNode = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.ACTION:
            bLangNode = bLangPackage.connectors.stream().filter(bConnector -> bConnector.name.getValue().equals(((BLangInvocation) definitionContext.get(NodeContextKeys.PREVIOUSLY_VISITED_NODE_KEY)).symbol.owner.name.getValue())).flatMap(connector -> connector.actions.stream()).filter(bAction -> bAction.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.TRANSFORMER:
            bLangNode = bLangPackage.transformers.stream().filter(bTransformer -> bTransformer.name.getValue().equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            break;
        case ContextConstants.ENDPOINT:
            bLangNode = bLangPackage.globalEndpoints.stream().filter(globalEndpoint -> globalEndpoint.name.value.equals(definitionContext.get(NodeContextKeys.NAME_OF_NODE_KEY))).findAny().orElse(null);
            if (bLangNode == null) {
                DefinitionTreeVisitor definitionTreeVisitor = new DefinitionTreeVisitor(definitionContext);
                definitionContext.get(NodeContextKeys.NODE_STACK_KEY).pop().accept(definitionTreeVisitor);
                if (definitionContext.get(NodeContextKeys.NODE_KEY) != null) {
                    bLangNode = definitionContext.get(NodeContextKeys.NODE_KEY);
                }
            }
            break;
        case ContextConstants.VARIABLE:
            bLangNode = bLangPackage.globalVars.stream().filter(globalVar -> globalVar.name.getValue().equals(definitionContext.get(NodeContextKeys.VAR_NAME_OF_NODE_KEY))).findAny().orElse(null);
            // BLangNode is null only when node at the cursor position is a local variable.
            if (bLangNode == null) {
                DefinitionTreeVisitor definitionTreeVisitor = new DefinitionTreeVisitor(definitionContext);
                definitionContext.get(NodeContextKeys.NODE_STACK_KEY).pop().accept(definitionTreeVisitor);
                if (definitionContext.get(NodeContextKeys.NODE_KEY) != null) {
                    bLangNode = definitionContext.get(NodeContextKeys.NODE_KEY);
                    break;
                }
            }
            break;
        default:
            break;
    }
    if (bLangNode == null) {
        return contents;
    }
    Location l = new Location();
    TextDocumentPositionParams position = definitionContext.get(DocumentServiceKeys.POSITION_KEY);
    Path parentPath = CommonUtil.getPath(new LSDocument(position.getTextDocument().getUri())).getParent();
    if (parentPath != null) {
        String fileName = bLangNode.getPosition().getSource().getCompilationUnitName();
        Path filePath = Paths.get(CommonUtil.getPackageURI(definitionContext.get(NodeContextKeys.PACKAGE_OF_NODE_KEY).name.getValue(), parentPath.toString(), definitionContext.get(NodeContextKeys.PACKAGE_OF_NODE_KEY).name.getValue()), fileName);
        l.setUri(filePath.toUri().toString());
        Range r = new Range();
        // Subtract 1 to convert the token lines and char positions to zero based indexing
        r.setStart(new Position(bLangNode.getPosition().getStartLine() - 1, bLangNode.getPosition().getStartColumn() - 1));
        r.setEnd(new Position(bLangNode.getPosition().getEndLine() - 1, bLangNode.getPosition().getEndColumn() - 1));
        l.setRange(r);
        contents.add(l);
    }
    return contents;
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) PackageID(org.ballerinalang.model.elements.PackageID) Range(org.eclipse.lsp4j.Range) LSPackageCache(org.ballerinalang.langserver.LSPackageCache) NodeContextKeys(org.ballerinalang.langserver.common.constants.NodeContextKeys) ArrayList(java.util.ArrayList) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) DefinitionTreeVisitor(org.ballerinalang.langserver.definition.DefinitionTreeVisitor) LSDocument(org.ballerinalang.langserver.common.LSDocument) List(java.util.List) Paths(java.nio.file.Paths) ContextConstants(org.ballerinalang.langserver.common.constants.ContextConstants) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) Location(org.eclipse.lsp4j.Location) Position(org.eclipse.lsp4j.Position) Path(java.nio.file.Path) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) Path(java.nio.file.Path) Position(org.eclipse.lsp4j.Position) ArrayList(java.util.ArrayList) Range(org.eclipse.lsp4j.Range) DefinitionTreeVisitor(org.ballerinalang.langserver.definition.DefinitionTreeVisitor) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) LSDocument(org.ballerinalang.langserver.common.LSDocument) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) Location(org.eclipse.lsp4j.Location)

Example 3 with TextDocumentServiceContext

use of org.ballerinalang.langserver.TextDocumentServiceContext 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 4 with TextDocumentServiceContext

use of org.ballerinalang.langserver.TextDocumentServiceContext in project ballerina by ballerina-lang.

the class ParserRuleVariableDefinitionStatementContextResolver method resolveItems.

@Override
@SuppressWarnings("unchecked")
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
    ArrayList<CompletionItem> completionItems = new ArrayList<>();
    PackageActionFunctionAndTypesFilter actionFunctionTypeFilter = new PackageActionFunctionAndTypesFilter();
    ConnectorInitExpressionItemFilter connectorInitItemFilter = new ConnectorInitExpressionItemFilter();
    // action invocation or worker invocation
    if (isInvocationOrFieldAccess(completionContext)) {
        ArrayList<SymbolInfo> actionAndFunctions = new ArrayList<>();
        actionAndFunctions.addAll(actionFunctionTypeFilter.filterItems(completionContext));
        this.populateCompletionItemList(actionAndFunctions, completionItems);
    } else {
        // Fill completions if user is writing a connector init
        List<SymbolInfo> filteredConnectorInitSuggestions = connectorInitItemFilter.filterItems(completionContext);
        if (!filteredConnectorInitSuggestions.isEmpty()) {
            populateCompletionItemList(filteredConnectorInitSuggestions, completionItems);
        }
        // Add the create keyword
        CompletionItem createKeyword = new CompletionItem();
        createKeyword.setInsertText(Snippet.CREATE_KEYWORD_SNIPPET.toString());
        createKeyword.setLabel(ItemResolverConstants.CREATE_KEYWORD);
        createKeyword.setDetail(ItemResolverConstants.KEYWORD_TYPE);
        List<SymbolInfo> filteredList = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY).stream().filter(symbolInfo -> {
            BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
            SymbolKind symbolKind = bSymbol.kind;
            // Here we return false if the BType is not either a package symbol or ENUM
            return !((bSymbol instanceof BTypeSymbol) && !(bSymbol instanceof BPackageSymbol || SymbolKind.ENUM.equals(symbolKind)));
        }).collect(Collectors.toList());
        // Remove the functions without a receiver symbol
        filteredList.removeIf(symbolInfo -> {
            BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
            return bSymbol instanceof BInvokableSymbol && ((BInvokableSymbol) bSymbol).receiverSymbol != null;
        });
        populateCompletionItemList(filteredList, completionItems);
        completionItems.add(createKeyword);
    }
    Class sorterKey = completionContext.get(DocumentServiceKeys.PARSER_RULE_CONTEXT_KEY).getClass();
    CompletionItemSorter itemSorter = ItemSorters.getSorterByClass(sorterKey);
    itemSorter.sortItems(completionContext, completionItems);
    return completionItems;
}
Also used : ConnectorInitExpressionItemFilter(org.ballerinalang.langserver.completions.util.filters.ConnectorInitExpressionItemFilter) CompletionItemSorter(org.ballerinalang.langserver.completions.util.sorters.CompletionItemSorter) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) ItemResolverConstants(org.ballerinalang.langserver.completions.util.ItemResolverConstants) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ItemSorters(org.ballerinalang.langserver.completions.util.sorters.ItemSorters) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) CompletionItem(org.eclipse.lsp4j.CompletionItem) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) List(java.util.List) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) Snippet(org.ballerinalang.langserver.completions.util.Snippet) SymbolKind(org.ballerinalang.model.symbols.SymbolKind) CompletionKeys(org.ballerinalang.langserver.completions.CompletionKeys) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) AbstractItemResolver(org.ballerinalang.langserver.completions.resolvers.AbstractItemResolver) PackageActionFunctionAndTypesFilter(org.ballerinalang.langserver.completions.util.filters.PackageActionFunctionAndTypesFilter) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) SymbolKind(org.ballerinalang.model.symbols.SymbolKind) ArrayList(java.util.ArrayList) CompletionItemSorter(org.ballerinalang.langserver.completions.util.sorters.CompletionItemSorter) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ConnectorInitExpressionItemFilter(org.ballerinalang.langserver.completions.util.filters.ConnectorInitExpressionItemFilter) PackageActionFunctionAndTypesFilter(org.ballerinalang.langserver.completions.util.filters.PackageActionFunctionAndTypesFilter) CompletionItem(org.eclipse.lsp4j.CompletionItem)

Example 5 with TextDocumentServiceContext

use of org.ballerinalang.langserver.TextDocumentServiceContext in project ballerina by ballerina-lang.

the class PackageActionFunctionAndTypesFilter method invocationsAndFieldsOnIdentifier.

/**
 * Get the invocations and fields against an identifier (functions, struct fields and types including the enums).
 * @param context     Text Document Service context (Completion Context)
 * @param delimiterIndex        delimiter index (index of either . or :)
 * @return {@link ArrayList}    List of filtered symbol info
 */
private ArrayList<SymbolInfo> invocationsAndFieldsOnIdentifier(TextDocumentServiceContext context, int delimiterIndex) {
    ArrayList<SymbolInfo> actionFunctionList = new ArrayList<>();
    TokenStream tokenStream = context.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
    List<SymbolInfo> symbols = context.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
    SymbolTable symbolTable = context.get(DocumentServiceKeys.SYMBOL_TABLE_KEY);
    String variableName = CommonUtil.getPreviousDefaultToken(tokenStream, delimiterIndex).getText();
    SymbolInfo variable = this.getVariableByName(variableName, symbols);
    String builtinPkgName = symbolTable.builtInPackageSymbol.name.getValue();
    Map<Name, Scope.ScopeEntry> entries = new HashMap<>();
    String currentPkgName = context.get(DocumentServiceKeys.CURRENT_PACKAGE_NAME_KEY);
    if (variable == null) {
        return actionFunctionList;
    }
    String packageID;
    BType bType = variable.getScopeEntry().symbol.getType();
    String bTypeValue;
    if (variable.getScopeEntry().symbol instanceof BEndpointVarSymbol) {
        BType getClientFuncType = ((BEndpointVarSymbol) variable.getScopeEntry().symbol).getClientFunction.type;
        if (!UtilSymbolKeys.ACTION_INVOCATION_SYMBOL_KEY.equals(tokenStream.get(delimiterIndex).getText()) || !(getClientFuncType instanceof BInvokableType)) {
            return actionFunctionList;
        }
        BType boundType = ((BInvokableType) getClientFuncType).retTypes.get(0);
        packageID = boundType.tsymbol.pkgID.toString();
        bTypeValue = boundType.toString();
    } else if (bType instanceof BArrayType) {
        packageID = ((BArrayType) bType).eType.tsymbol.pkgID.toString();
        bTypeValue = bType.toString();
    } else {
        packageID = bType.tsymbol.pkgID.toString();
        bTypeValue = bType.toString();
    }
    // Extract the package symbol. This is used to extract the entries of the particular package
    SymbolInfo packageSymbolInfo = symbols.stream().filter(item -> {
        Scope.ScopeEntry scopeEntry = item.getScopeEntry();
        return (scopeEntry.symbol instanceof BPackageSymbol) && scopeEntry.symbol.pkgID.toString().equals(packageID);
    }).findFirst().orElse(null);
    if (packageSymbolInfo == null && packageID.equals(builtinPkgName)) {
        // If the packageID is ballerina.builtin, we extract entries of builtin package
        entries = symbolTable.builtInPackageSymbol.scope.entries;
    } else if (packageSymbolInfo == null && packageID.equals(currentPkgName)) {
        entries = this.getScopeEntries(bType, context);
    } else if (packageSymbolInfo != null) {
        // If the package exist, we extract particular entries from package
        entries = packageSymbolInfo.getScopeEntry().symbol.scope.entries;
    }
    entries.forEach((name, scopeEntry) -> {
        if (scopeEntry.symbol instanceof BInvokableSymbol && ((BInvokableSymbol) scopeEntry.symbol).receiverSymbol != null) {
            String symbolBoundedName = ((BInvokableSymbol) scopeEntry.symbol).receiverSymbol.getType().toString();
            if (symbolBoundedName.equals(bTypeValue)) {
                // TODO: Need to handle the name in a proper manner
                String[] nameComponents = name.toString().split("\\.");
                SymbolInfo actionFunctionSymbol = new SymbolInfo(nameComponents[nameComponents.length - 1], scopeEntry);
                actionFunctionList.add(actionFunctionSymbol);
            }
        } else if ((scopeEntry.symbol instanceof BTypeSymbol) && bTypeValue.equals(scopeEntry.symbol.type.toString())) {
            // Get the struct fields
            Map<Name, Scope.ScopeEntry> fields = scopeEntry.symbol.scope.entries;
            fields.forEach((fieldName, fieldScopeEntry) -> {
                actionFunctionList.add(new SymbolInfo(fieldName.getValue(), fieldScopeEntry));
            });
        }
    });
    // Populate possible iterable operators over the variable
    populateIterableOperations(variable, actionFunctionList);
    return actionFunctionList;
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) Arrays(java.util.Arrays) BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) TokenStream(org.antlr.v4.runtime.TokenStream) Token(org.antlr.v4.runtime.Token) BIntermediateCollectionType(org.wso2.ballerinalang.compiler.semantics.model.types.BIntermediateCollectionType) ItemResolverConstants(org.ballerinalang.langserver.completions.util.ItemResolverConstants) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BXMLType(org.wso2.ballerinalang.compiler.semantics.model.types.BXMLType) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) Map(java.util.Map) Snippet(org.ballerinalang.langserver.completions.util.Snippet) SymbolKind(org.ballerinalang.model.symbols.SymbolKind) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) CompletionKeys(org.ballerinalang.langserver.completions.CompletionKeys) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) Collectors(java.util.stream.Collectors) Name(org.wso2.ballerinalang.compiler.util.Name) List(java.util.List) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) UtilSymbolKeys(org.ballerinalang.langserver.common.UtilSymbolKeys) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) TokenStream(org.antlr.v4.runtime.TokenStream) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SymbolTable(org.wso2.ballerinalang.compiler.semantics.model.SymbolTable) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BInvokableType(org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) Name(org.wso2.ballerinalang.compiler.util.Name) BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) BEndpointVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BEndpointVarSymbol) Scope(org.wso2.ballerinalang.compiler.semantics.model.Scope) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

TextDocumentServiceContext (org.ballerinalang.langserver.TextDocumentServiceContext)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 DocumentServiceKeys (org.ballerinalang.langserver.DocumentServiceKeys)7 Collectors (java.util.stream.Collectors)5 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)5 CompletionKeys (org.ballerinalang.langserver.completions.CompletionKeys)4 CompletionItem (org.eclipse.lsp4j.CompletionItem)4 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)4 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 UtilSymbolKeys (org.ballerinalang.langserver.common.UtilSymbolKeys)3 CommonUtil (org.ballerinalang.langserver.common.utils.CommonUtil)3 ItemResolverConstants (org.ballerinalang.langserver.completions.util.ItemResolverConstants)3 Position (org.eclipse.lsp4j.Position)3 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)3 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)3 Token (org.antlr.v4.runtime.Token)2 TokenStream (org.antlr.v4.runtime.TokenStream)2