Search in sources :

Example 86 with BSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol 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 87 with BSymbol

use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol 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 88 with BSymbol

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

the class AbstractItemResolver method populateBallerinaFunctionCompletionItem.

/**
 * Populate the Ballerina Function Completion Item.
 * @param symbolInfo - symbol information
 * @return completion item
 */
private CompletionItem populateBallerinaFunctionCompletionItem(SymbolInfo symbolInfo) {
    String insertText;
    String label;
    CompletionItem completionItem = new CompletionItem();
    if (symbolInfo.isIterableOperation()) {
        insertText = symbolInfo.getIterableOperationSignature().getInsertText();
        label = symbolInfo.getIterableOperationSignature().getLabel();
    } else {
        BSymbol bSymbol = symbolInfo.getScopeEntry().symbol;
        if (!(bSymbol instanceof BInvokableSymbol)) {
            return null;
        }
        BInvokableSymbol bInvokableSymbol = (BInvokableSymbol) bSymbol;
        if (bInvokableSymbol.getName().getValue().contains("<") || bInvokableSymbol.getName().getValue().contains("<") || bInvokableSymbol.getName().getValue().equals("main")) {
            return null;
        }
        FunctionSignature functionSignature = getFunctionSignature(bInvokableSymbol);
        insertText = functionSignature.getInsertText();
        label = functionSignature.getLabel();
    }
    completionItem.setInsertTextFormat(InsertTextFormat.Snippet);
    completionItem.setLabel(label);
    completionItem.setInsertText(insertText);
    completionItem.setDetail(ItemResolverConstants.FUNCTION_TYPE);
    completionItem.setKind(CompletionItemKind.Function);
    return 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)

Example 89 with BSymbol

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

Example 90 with BSymbol

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

the class PositionTreeVisitor method visit.

public void visit(BLangFunction funcNode) {
    // Check for native functions
    BSymbol funcSymbol = funcNode.symbol;
    if (Symbols.isNative(funcSymbol)) {
        return;
    }
    addTopLevelNodeToContext(funcNode, funcNode.name.getValue(), funcNode.symbol.pkgID, funcNode.symbol.kind.name(), funcNode.symbol.kind.name(), funcNode.symbol.owner.name.getValue(), funcNode.symbol.owner.pkgID);
    setPreviousNode(funcNode);
    this.addToNodeStack(funcNode);
    if (funcNode.receiver != null) {
        this.acceptNode(funcNode.receiver);
    }
    if (!funcNode.requiredParams.isEmpty()) {
        funcNode.requiredParams.forEach(this::acceptNode);
    }
    if (!funcNode.retParams.isEmpty()) {
        funcNode.retParams.forEach(this::acceptNode);
    }
    if (funcNode.body != null) {
        this.acceptNode(funcNode.body);
    }
    if (funcNode.endpoints != null && !funcNode.endpoints.isEmpty()) {
        funcNode.endpoints.forEach(this::acceptNode);
    }
    // Process workers
    if (!funcNode.workers.isEmpty()) {
        funcNode.workers.forEach(this::acceptNode);
    }
    if (!funcNode.defaultableParams.isEmpty()) {
        funcNode.defaultableParams.forEach(this::acceptNode);
    }
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)

Aggregations

BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)78 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)29 Name (org.wso2.ballerinalang.compiler.util.Name)23 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)19 ArrayList (java.util.ArrayList)16 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)14 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)13 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)13 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)12 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)11 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)11 List (java.util.List)10 BLangAnnotationAttachmentPoint (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachmentPoint)10 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)10 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)10 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)10 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)10 Collectors (java.util.stream.Collectors)9 Arrays (java.util.Arrays)8 Collections (java.util.Collections)7