Search in sources :

Example 41 with BInvokableSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol in project ballerina by ballerina-lang.

the class SignatureTreeVisitor method populateSymbols.

/**
 * Populate the symbols.
 * @param symbolEntries symbol entries
 */
private void populateSymbols(Map<Name, Scope.ScopeEntry> symbolEntries) {
    // TODO: Populate only the visible functions
    this.terminateVisitor = true;
    String identifierAgainst = documentServiceContext.get(SignatureKeys.IDENTIFIER_AGAINST);
    List<SymbolInfo> visibleSymbols = new ArrayList<>();
    /*
          During the first iteration we filter out the functions and if there is, the variable reference against which
          the function is called.
         */
    symbolEntries.forEach((k, v) -> {
        if (v.symbol instanceof BInvokableSymbol && !(v.symbol instanceof BOperatorSymbol) && !v.symbol.getName().getValue().contains("<init>")) {
            SymbolInfo symbolInfo = new SymbolInfo(k.getValue(), v);
            visibleSymbols.add(symbolInfo);
        } else if (v.symbol instanceof BVarSymbol && k.getValue().equals(identifierAgainst)) {
            documentServiceContext.put(SignatureKeys.IDENTIFIER_TYPE, v.symbol.type.toString());
        } else if (v.symbol instanceof BPackageSymbol && k.getValue().equals(identifierAgainst)) {
            documentServiceContext.put(SignatureKeys.IDENTIFIER_PKGID, v.symbol.pkgID.toString());
            documentServiceContext.put(SignatureKeys.IDENTIFIER_TYPE, v.symbol.type.toString());
            visibleSymbols.addAll(this.getInvokableSymbolsInPackage((BPackageSymbol) v.symbol));
        }
    });
    /*
          In this iteration we filter out the functions either having a receiver or otherwise.
          If the identifier against value is a valid value, then check whether the receiver type equals to identifier
          type. If there is no identifier, filter out functions without the receiver
         */
    List<SymbolInfo> filteredSymbols = new ArrayList<>();
    visibleSymbols.forEach(symbolInfo -> {
        BVarSymbol receiver = ((BInvokableSymbol) symbolInfo.getScopeEntry().symbol).receiverSymbol;
        String[] nameTokens = symbolInfo.getSymbolName().split("\\.");
        String funcNameFromSymbol = nameTokens[nameTokens.length - 1];
        String functionName = documentServiceContext.get(SignatureKeys.CALLABLE_ITEM_NAME);
        String identifierPkgName = documentServiceContext.get(SignatureKeys.IDENTIFIER_PKGID);
        boolean onIdentifierTypePkg = "package".equals(documentServiceContext.get(SignatureKeys.IDENTIFIER_TYPE)) && symbolInfo.getScopeEntry().symbol.pkgID.toString().equals(identifierPkgName);
        boolean onReceiverTypeMatchIdentifier = receiver != null && receiver.type.toString().equals(documentServiceContext.get(SignatureKeys.IDENTIFIER_TYPE));
        boolean onIdentifierAgainstNull = (receiver == null && (identifierAgainst == null || identifierAgainst.equals("")));
        if ((onIdentifierTypePkg || onReceiverTypeMatchIdentifier || onIdentifierAgainstNull) && funcNameFromSymbol.equals(functionName)) {
            filteredSymbols.add(symbolInfo);
        }
    });
    documentServiceContext.put(SignatureKeys.FILTERED_FUNCTIONS, filteredSymbols);
}
Also used : BPackageSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol) ArrayList(java.util.ArrayList) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BOperatorSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo)

Aggregations

BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)31 BInvokableType (org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType)14 ArrayList (java.util.ArrayList)12 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)11 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)10 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)9 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)8 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)7 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)7 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)7 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)6 BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)6 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 HashMap (java.util.HashMap)4 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)4 BAttachedFunction (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol.BAttachedFunction)4 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)4 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)4 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)4