Search in sources :

Example 11 with BTypeSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol 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)

Example 12 with BTypeSymbol

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

the class ParserRuleMatchStatementContextResolver method resolveItems.

@Override
public ArrayList<CompletionItem> resolveItems(TextDocumentServiceContext completionContext) {
    ArrayList<CompletionItem> completionItems = new ArrayList<>();
    int currentTokenIndex = completionContext.get(DocumentServiceKeys.TOKEN_INDEX_KEY);
    List<SymbolInfo> visibleSymbols = completionContext.get(CompletionKeys.VISIBLE_SYMBOLS_KEY);
    TokenStream tokenStream = completionContext.get(DocumentServiceKeys.TOKEN_STREAM_KEY);
    while (true) {
        if (currentTokenIndex < 0) {
            // Ideally should not come to this point
            return completionItems;
        }
        Token token = CommonUtil.getPreviousDefaultToken(tokenStream, currentTokenIndex);
        if (token.getText().equals(UtilSymbolKeys.MATCH_KEYWORD_KEY)) {
            currentTokenIndex = token.getTokenIndex();
            break;
        } else {
            currentTokenIndex = token.getTokenIndex();
        }
    }
    String identifierMatched = CommonUtil.getNextDefaultToken(tokenStream, currentTokenIndex).getText();
    SymbolInfo identifierSymbol = visibleSymbols.stream().filter(symbolInfo -> symbolInfo.getScopeEntry().symbol.getName().getValue().equals(identifierMatched)).findFirst().orElseGet(null);
    if (identifierSymbol == null) {
        return completionItems;
    } else if (identifierSymbol.getScopeEntry().symbol.type instanceof BUnionType) {
        Set<BType> memberTypes = ((BUnionType) identifierSymbol.getScopeEntry().symbol.type).getMemberTypes();
        memberTypes.forEach(bType -> {
            completionItems.add(this.populateCompletionItem(bType.toString(), ItemResolverConstants.B_TYPE, bType.toString()));
        });
    } else if (identifierSymbol.getScopeEntry().symbol.type instanceof BJSONType) {
        ArrayList<Integer> typeTagsList = new ArrayList<>(Arrays.asList(TypeTags.INT, TypeTags.FLOAT, TypeTags.BOOLEAN, TypeTags.STRING, TypeTags.NULL, TypeTags.JSON));
        List<SymbolInfo> filteredBasicTypes = visibleSymbols.stream().filter(symbolInfo -> {
            BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
            return bSymbol instanceof BTypeSymbol && typeTagsList.contains(bSymbol.getType().tag);
        }).collect(Collectors.toList());
        this.populateCompletionItemList(filteredBasicTypes, completionItems);
    } else if (identifierSymbol.getScopeEntry().symbol.type instanceof BStructType) {
        List<SymbolInfo> structSymbols = visibleSymbols.stream().filter(symbolInfo -> {
            BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
            return bSymbol instanceof BStructSymbol && !bSymbol.getName().getValue().startsWith(UtilSymbolKeys.ANON_STRUCT_CHECKER);
        }).collect(Collectors.toList());
        this.populateCompletionItemList(structSymbols, completionItems);
    }
    return completionItems;
}
Also used : CommonUtil(org.ballerinalang.langserver.common.utils.CommonUtil) Arrays(java.util.Arrays) TextDocumentServiceContext(org.ballerinalang.langserver.TextDocumentServiceContext) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) TokenStream(org.antlr.v4.runtime.TokenStream) Token(org.antlr.v4.runtime.Token) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) Set(java.util.Set) ItemResolverConstants(org.ballerinalang.langserver.completions.util.ItemResolverConstants) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) CompletionItem(org.eclipse.lsp4j.CompletionItem) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) List(java.util.List) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) UtilSymbolKeys(org.ballerinalang.langserver.common.UtilSymbolKeys) CompletionKeys(org.ballerinalang.langserver.completions.CompletionKeys) AbstractItemResolver(org.ballerinalang.langserver.completions.resolvers.AbstractItemResolver) DocumentServiceKeys(org.ballerinalang.langserver.DocumentServiceKeys) TokenStream(org.antlr.v4.runtime.TokenStream) Set(java.util.Set) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BJSONType(org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType) ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BStructSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol) SymbolInfo(org.ballerinalang.langserver.completions.SymbolInfo) BUnionType(org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType) BStructType(org.wso2.ballerinalang.compiler.semantics.model.types.BStructType) CompletionItem(org.eclipse.lsp4j.CompletionItem) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with BTypeSymbol

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

the class AbstractItemResolver method populateCompletionItemList.

/**
 * Populate the completion item list by considering the.
 * @param symbolInfoList - list of symbol information
 * @param completionItems - completion item list to populate
 */
protected void populateCompletionItemList(List<SymbolInfo> symbolInfoList, List<CompletionItem> completionItems) {
    symbolInfoList.forEach(symbolInfo -> {
        CompletionItem completionItem = null;
        BSymbol bSymbol = symbolInfo.getScopeEntry() != null ? symbolInfo.getScopeEntry().symbol : null;
        if ((bSymbol instanceof BInvokableSymbol && ((BInvokableSymbol) bSymbol).kind != null && !((BInvokableSymbol) bSymbol).kind.equals(SymbolKind.WORKER)) || symbolInfo.isIterableOperation()) {
            completionItem = this.populateBallerinaFunctionCompletionItem(symbolInfo);
        } else if (!(bSymbol instanceof BInvokableSymbol) && bSymbol instanceof BVarSymbol) {
            completionItem = this.populateVariableDefCompletionItem(symbolInfo);
        } else if (bSymbol instanceof BTypeSymbol && !bSymbol.getName().getValue().equals(UtilSymbolKeys.NOT_FOUND_TYPE) && !(bSymbol instanceof BAnnotationSymbol)) {
            completionItem = this.populateBTypeCompletionItem(symbolInfo);
        }
        if (completionItem != null) {
            completionItems.add(completionItem);
        }
    });
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) CompletionItem(org.eclipse.lsp4j.CompletionItem) BInvokableSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol) BTypeSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol) BVarSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol) BAnnotationSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol)

Aggregations

BTypeSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol)13 ArrayList (java.util.ArrayList)7 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)6 SymbolInfo (org.ballerinalang.langserver.completions.SymbolInfo)5 List (java.util.List)4 CompletionItem (org.eclipse.lsp4j.CompletionItem)4 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)4 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)4 BStructType (org.wso2.ballerinalang.compiler.semantics.model.types.BStructType)4 Collectors (java.util.stream.Collectors)3 TokenStream (org.antlr.v4.runtime.TokenStream)3 DocumentServiceKeys (org.ballerinalang.langserver.DocumentServiceKeys)3 TextDocumentServiceContext (org.ballerinalang.langserver.TextDocumentServiceContext)3 CompletionKeys (org.ballerinalang.langserver.completions.CompletionKeys)3 ItemResolverConstants (org.ballerinalang.langserver.completions.util.ItemResolverConstants)3 BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)3 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)3 Arrays (java.util.Arrays)2 Token (org.antlr.v4.runtime.Token)2 UtilSymbolKeys (org.ballerinalang.langserver.common.UtilSymbolKeys)2